php – Symfony2 – 使用Controller自动连接服务

我能够使用Symfony2的新autowire功能将服务注入服务.但是,我无法将服务注入控制器.我不做什么/做错了什么?

这是我的services.yml文件:

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

这是我的ServiceConsumerDemo:

namespace AppBundle\Services;

class ServiceConsumerDemo {

    private $serviceDemo;

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

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

这是ServiceDemo:

namespace AppBundle\Services;

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

这是HomeController(可以工作):

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);
    }
}

这是HomeController不起作用

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);
    }
}

这是抛出的错误:

Catchable Fatal Error: Argument 1 passed to
AppBundle\Controller\HomeController::__construct() must be an instance
of AppBundle\Services\ServiceConsumerDemo, none given,

如何访问Controller中的服务?我知道我需要将控制器声明为服务.所以我在services.yml文件中提到了控制器.还有什么我需要做的吗?

最佳答案 默认情况下,控制器不被视为服务,因此您无法使用它们进行自动装配.

但是,这是一个简单的修复 – 使用服务定义在类上添加@Route注释(即不在类中的操作方法上).

从documentation:

The @Route annotation on a controller class can also be used to assign
the controller class to a service so that the controller resolver will
instantiate the controller by fetching it from the DI container
instead of calling new PostController() itself.

例如:

/** 
 * @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
点赞