簡體   English   中英

Symfony2 - 使用Controller實現自動裝配服務

[英]Symfony2 - Autowiring Services with the 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);
    }
}

這是拋出的錯誤:

可捕獲致命錯誤:傳遞給AppBundle \\ Controller \\ HomeController :: __ construct()的參數1必須是AppBundle \\ Services \\ ServiceConsumerDemo的實例,沒有給出,

如何訪問Controller中的服務? 我知道我需要將控制器聲明為服務。 所以我在services.yml文件中提到了控制器。 還有什么我需要做的嗎?

Symfony 3.3+ 2017年5月更新!

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)
    {
        // ...
    }
}

不需要更多。

默認情況下,控制器不被視為服務,因此您無法使用它們進行自動裝配。

但是,這是一個簡單的修復 - 使用服務定義在類上添加@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

您沒有將服務傳遞給services.yml中的控制器。 將其更改為:

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"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM