eZ Community » Forums » eZ Platform » How to get container into a controller

How to get container into a controller

How to get container into a controller

Thursday 18 February 2016 9:59:00 am - 3 replies

Which is the way to use the container intro a Controller by extending corresponding class

Which class should be extended?

Which method should I use?

Here I have examples just for commands


But what's the way for controllers?

Thursday 18 February 2016 11:37:32 pm

Typically you would extend eZ\Bundle\EzPublishCoreBundle\Controller because this provides eZ Publish methods as well as the basic Symfony ones:

namespace Acme\MyBundle\Controller;
use eZ\Bundle\EzPublishCoreBundle\Controller;
class DefaultController extends Controller
    public function someAction( ... )
        $someService = $this->get( 'some.service' );
        $someParameter = $this->container->getParameter( 'some.parameter' )

Hope this helps! It's worth reading the Symfony book because you sometimes may only want to extend Symfony's base controller class.

Friday 19 February 2016 12:55:17 pm

I'm coming back with more explanation

I've did like you mentioned, but I'm getting a 503 error service unavailable.

I did some research and it seems that if a dump $this->container, I'm receiving a NULL.

So my container is NULL.

I've tested too with a fresh copy of project from here https://github.com/ezsystems/ezplatform-demo, and got same results.

Any clues?

Modified on Friday 19 February 2016 12:56:06 pm by Andrei Moraru

Wednesday 24 February 2016 9:39:04 am

Figure it out.

Controller Class can extend eZ\Bundle\EzPublishCoreBundle\Controller, but can't use $this->container if the Controller Class is defined in view provider as a service. This work only if the Controller Class is defined as default controller, like: AppBundle:Home:show



You must be logged in to post messages in this topic!

36 542 Users on board!

Forums menu

Proudly Developed with from