bodokaiser
bodokaiser

Reputation: 15742

Symfony2: use parameter defined in config.xml

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

Upvotes: 0

Views: 942

Answers (1)

Mun Mun Das
Mun Mun Das

Reputation: 15002

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.

Upvotes: 2

Related Questions