Accessing the $ this-> container in the controller throws a Catchable Fatal Error exception?

I have the following controller:

namespace Acme\CompanyBundle\Controller; use Symfony\Component\DependencyInjection\Container; /** * Company controller. * */ class CompanyController extends Controller { protected $container; public function __construct(Container $container) { $this->container = $container; } public function getData() { $userObj = $this->container->get('security.context')->getToken()->getUser(); } } 

In my services.yml file, I introduced the Container class:

 parameters: acme.controller.company.class: Acme\ContainerBundle\Controller\CompanyController services: acme.controller.company: class: %acme.controller.company.class% arguments: [@service_container] 

When loading this controller, the following error appears:

Fatal error being truncated: argument 1 passed to Acme \ CompanyBundle \ Controller \ CompanyController :: __ construct () must be an instance of Symfony \ Component \ DependencyInjection \ Container, none specified, called in C: \ wamp \ www \ symfony \ app \ cache \ dev \ classes.php on line 2785 and C: \ WAMP \ WWW \ Symfony \ SRC \ Acme \ CompanyBundle \ Controller \ CompanyController.php line ...

As you can see, this is a simple injection of the Container object into the controller, but it causes good errors. What is the problem?

A similar issue is posted in another SO thread here .

0
php symfony
Oct 08 '14 at
source share
3 answers

You do not need to inject the container into the controllers if they extend the base class of the Controller that you make.

Just do:

 namespace Acme\CompanyBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; /** * Company controller. * */ class CompanyController extends Controller { public function getData() { $userObj = $this->get('security.context')->getToken()->getUser(); } } 
+2
08 Oct '14 at 18:04
source share

By default, routes look something like this:

 cerad_player_wanabe_list: pattern: /player-request/list defaults: _controller: CeradPlayerWanabeBundle:Player/PlayerList:list 

The Symfony \ Component \ HttpKernel \ HttpKernel :: handle ($ request) method retrieves the _controller attribute from the request object. If the attribute has two colons, it translates the attribute into the class name and creates an instance using the new operator. If the instance implements ContainerAwareInterface, the container is injected into the controller instance. The controller service you defined is not used. Therefore, the error that the argument is not passed to the constructor.

On the other hand, if _controller has only one colon, then the controller is pulled out as a service from the container. Checking ContainerAwareInterface is not performed. It is up to you to inject dependencies through the service definition.

All of this is documented in: http://symfony.com/doc/current/cookbook/controller/service.html

So, for this particular question, your route should look something like this:

 cerad_player_wanabe_list: pattern: /player-request/list defaults: _controller: acme.controller.company:action 

This begs the question of why you are trying to define the controller as a service. The default approach already does exactly what you want you to not type.

The rationale for defining services as containers is that you can precisely control the dependencies that the controller uses. Makes the controller more understandable and tested.

Enabling a full container greatly destroys the value of defining a controller as a service.

+1
Oct 08 '14 at 18:51
source share

Never and never insert a container inside anything (services, a controller or anything else) Instead, try entering securityContext or accessing it through the symfony controller helper method, as suggested above.

The token is not an object, because, probably, the controllerโ€™s route is not under the firewall

+1
09 Oct '14 at 7:42 on
source share



All Articles