Symfony2 - Autowiring services with a controller

I can use the new autwire feature for Symfony2 to inject the service into the service. However, I cannot inject the service into the controller. What am I not doing / doing wrong?

This is my services.yml file:

services:
    home_controller:
        class:  AppBundle\Controller\HomeController
        autowire:  true

This is my ServiceConsumerDemo:

namespace AppBundle\Services;

class ServiceConsumerDemo {

    private $serviceDemo;

    public function __construct(ServiceDemo $serviceDemo) {
        $this->serviceDemo = $serviceDemo;
    }

    public function getMessage(){
        return $this->serviceDemo->helloWorld();
    }
}

This is ServiceDemo:

namespace AppBundle\Services;

class ServiceDemo 
{    
    public function helloWorld(){
        return "hello, world!";
    }
}

This is the HomeController (which works):

namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;


class HomeController extends Controller
{
    /**
     * @Route("/")
     *
     */
    public function indexAction(Request $request)
    {
        $message[0] = $this->get('service_consumer_demo')->getMessage();
        return new \Symfony\Component\HttpFoundation\JsonResponse($message);
    }
}

This is a HomeController that does not work.

namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use AppBundle\Services\ServiceConsumerDemo;

class HomeController extends Controller
{

    private $serviceConsumerDemo;

    public function __construct(ServiceConsumerDemo $serviceConsumerDemo) {

        $this->serviceConsumerDemo = $serviceConsumerDemo;
    }

    /**
     * @Route("/", name="homepage")
     *
     */
    public function indexAction(Request $request)
    {
        $message[0] =  $this->serviceConsumerDemo->getMessage();
        return new \Symfony\Component\HttpFoundation\JsonResponse($message);
    }
}

This is mistake:

Fatal error cropped: argument 1 passed to AppBundle \ Controller \ HomeController :: __ construct () must be an instance from AppBundle \ Services \ ServiceConsumerDemo, not specified

How do I access the services in the controller? I understand that I need to declare the controller as a service. Therefore, I mention the controller in the services.yml file. Is there anything else I need to do?

+4
3

Symfony 3.3+ 2017 UPDATE!

Symfony 3.3 .

# app/config/services.yml
services:
    _defaults:
        autowire: true # all services in this config are now autowired

    App\: # no more manual registration of similar groups of services 
        resource: ../{Controller}

, App\Controller\SomeController app/Controller/SomeController.php.

:


Symfony 2.8+ , : Symplify/ControllerAutowire

.

, :

class YourController
{
    public function __construct(SomeDependency $someDependency)
    {
        // ...
    }
}

.

+1

, Autowiring.

- @Route (.. ) .

:

@Route , , DI PostController().

:

/** 
 * @Route(service="app_controller") 
 */
class AppController extends Controller
{

    private $service;

    public function __construct(SomeService $service)
    {
        $this->service = $service;
    }

    /** @Route("/") */
    public function someAction()
    {
        $this->service->doSomething();
    }

}

:

app_controller:
    class:  AppBundle\Controller\AppController
    autowire: true
+1

You do not transfer the service to your controller in services.yml. Change it to:

services:
    service_demo:
        class:  AppBundle\Services\ServiceDemo
    service_consumer_demo:
        class:  AppBundle\Services\ServiceConsumerDemo
        autowire:   true
    home_controller:
        class:  AppBundle\Controller\HomeController
        autowire:  true
        arguments:
            service: "@service_consumer_demo"
-1
source

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


All Articles