Inject service in symfony2 controller

How can I inject a service (created service) into my controller? Setter injection will do.

<?php namespace MyNamespace; use Symfony\Bundle\FrameworkBundle\Controller\Controller; class MyController extends Controller { public function setMyService(MyService $myService) { $this->myService = $myService; } public function indexAction() { //Here I cannot access $this->myService; //Because the setter is not called magically! } } 

And my route settings:

 // Resources/routing.yml myController_index: pattern: /test defaults: { _controller: "FooBarBundle:MyController:index" } 

I install the service in another package:

 // Resources/services.yml parameters: my.service.class: Path\To\My\Service services: my_service: class: %my.service.class% 

When a route is allowed, the service is not entered (I know that it should not). I assume that somewhere in the yml file, I should install:

  calls: - [setMyService, [@my_service]] 

I do not use this controller as a service, it is a regular controller that serves the request.

Edit: at the moment I am getting a service with $ this-> container-> get ('my_service'); But I need to enter it.

+6
source share
4 answers

If you want to integrate services into your controllers, you must define the controllers as services .

You can also take a look at the JMSDiExtraBundle special handling of controllers - if that solves your problem. But since I define my controllers as services, I have not tried this.

+7
source

When using the JMSDiExtraBundle, you DO NOT have to define your controller as a service (unlike @elnur said), and the code will be:

 <?php namespace MyNamespace; use JMS\DiExtraBundle\Annotation as DI; use Path\To\My\Service; use Symfony\Bundle\FrameworkBundle\Controller\Controller; class MyController extends Controller { /** * @var $myService Service * * @DI\Inject("my_service") */ protected $myService; public function indexAction() { // $this->myService->method(); } } 

I find this approach very enjoyable because you avoid writing the __construct() method.

+6
source

Since it ends in 2017 and there is no tag for Symfony 3 or the upcoming Symfony 4 (and I think this should not be), this problem can be solved in the native one much better.

If you are still struggling and somehow found on this page, and not in Symfony documents, then you should know that you do not need to declare the controller as a service, and it is already registered as one .

What you need to do is check services.yml :

 # app/config/services.yml services: # default configuration for services in *this* file _defaults: # ... public: false 

Change public: false to public:true if you want all services to be publicly available.

Or explicitly add the service and declare it public:

 # app/config/services.yml services: # ... same code as before # explicitly configure the service AppBundle\Service\MessageGenerator: public: true 

And then in your controller you can get the service:

 use AppBundle\Service\MessageGenerator; // accessing services like this only works if you extend Controller class ProductController extends Controller { public function newAction() { // only works if your service is public $messageGenerator = $this->get(MessageGenerator::class); } } 

More details:

+2
source

If you do not want to define your controller as a service, you can add a listener to the kernel.controller event to configure it immediately before its execution. Thus, you can enter the services you need inside your controller using the setters.

http://symfony.com/doc/current/components/http_kernel/introduction.html#component-http-kernel-kernel-controller

0
source

Source: https://habr.com/ru/post/953064/


All Articles