0

I have a paginator class which should get the limit out of the config.xml and the current page from the controller.

How do I define the limit in config.xml and how can I access it outside the controller?

Regards

1 Answer 1

2

Define your paginator class as DIC service and add service_container as a service e.g

//paginator class 
//namespace definitions
use Symfony\Component\DependencyInjection\ContainerInterface;

class Paginator{
    /**
     * @var Symfony\Component\DependencyInjection\ContainerInterface 
     */
    private $container;

    public function __construct(ContainerInterface $container){
        $this->container = $container;
    }

    public function yourPaginationMethod(){
        $limit = $this->container->getParameter("limit.conf.parameter");
        //rest of the method
    }
}

And then in you services.yml of your bundle.

 #services.yml
 paginator_service:
  class: FQCN\Of\Your\PaginatorClass
  arguments: [@service_container]

And in your controller you can get access of the Paginator in following way.

//in controller method
$paginator = $this->get('paginator_service');

For more info about it you can check Service container section of Symfony Documentation.

Sign up to request clarification or add additional context in comments.

1 Comment

Passing the entire container might not be such a good idea if you just need a parameter. You can pass the parameter itself directly to your service with %limit.conf.parameter%

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.