Symfony4:将服务注入控制器__construct(构造函数)

时间:2018-06-25 18:46:47

标签: php symfony symfony4 php-7.2

我无法在我的一个控制器的构造函数中访问注入的服务。

http://symfony.com/doc/current/service_container/injection_types.html我相信我已经正确完成了注入,但是当我尝试从控制器加载视图时,出现以下错误:

Argument 1 passed to Regions\AnalyticsBundle\Controller\PatternsController::__construct()
must be an instance of Regions\AnalyticsBundle\Controller\PatternCacheService, instance of
Regions\AnalyticsBundle\Service\PatternCacheService given, called
in /var/tmp/symfony/cache/dev/ContainerLoHUcSH/getPatternsControllerService.php on line 9

错误似乎表明构造函数中的类型提示正在尝试使其在*\Controller\*命名空间而不是*\Services\*命名空间中查找实例-我在做什么错或没看到在这里?

我的设置细节如下...

Symfony 4.1.0,PHP 7.2.5

services.yaml

services:
    ...

    pattern_cache_service:
        class: Regions\AnalyticsBundle\Service\PatternCacheService
        public: true

    Regions\AnalyticsBundle\Controller\PatternsController:
        arguments: ['@pattern_cache_service']

控制器:

namespace Regions\AnalyticsBundle\Controller;

class PatternsController extends BaseController
{

    private $pcs;

    public function __construct(PatternCacheService $pcs)
    {
        $this->pcs = $pcs;
    }
}

3 个答案:

答案 0 :(得分:2)

您在控制器中忘记了use,使PHP认为您的服务与控制器在同一命名空间中。

<?php

namespace Regions\AnalyticsBundle\Controller;

use Regions\AnalyticsBundle\Service\PatternCacheService;

class PatternsController extends BaseController
{

    private $pcs;

    public function __construct(PatternCacheService $pcs)
    {
        $this->pcs = $pcs;
    }
}

这实际上是您的错误消息的一部分

  

参数1传递给Regions \ AnalyticsBundle \ Controller \ PatternsController :: __ construct()   必须是Regions \ AnalyticsBundle \ Controller \ PatternCacheService

的实例

当您期望控制器需要一个Regions\AnalyticsBundle\Service\PatternCacheService实例时

答案 1 :(得分:2)

在命名空间PatternCacheService中找不到类Regions\AnalyticsBundle\Controller

添加导入:

<?php

namespace Regions\AnalyticsBundle\Controller;

use Regions\AnalyticsBundle\Service\PatternCacheService;

class PatternsController extends BaseController
{

    private $pcs;

    public function __construct(PatternCacheService $pcs)
    {
        $this->pcs = $pcs;
    }
}

供参考,请参见

答案 2 :(得分:1)

  1. 您不需要pattern_cache_service的服务定义。如果设置了autowire: true,它将自动为您的服务布线。
  2. PatternCacheService应该为private,因为您不想从容器内访问它。建议的做法!
  3. 您也不需要PatternsController的服务定义。

注意:您不应该再在Symfony 4中使用“捆绑包”,这样我就可以摆脱AnalyticsBundle

注意:更好地组织您的配置文件,如下所示:Organising route, service and parameter configuration files in symfony 4 applications

这应该满足:

services.yaml

services:
    _defaults:
        autowire: true
        autoconfigure: true
        public: false

    App\:
        resource: '../src/*'
        exclude: '../src/{Entity,....so on .....,Kernel.php}'

    App\Controller\:
        resource: '../../src/Regions/AnalyticsBundle/Controller'
        tags: ['controller.service_arguments']

PatternsController

namespace Regions\AnalyticsBundle\Controller;

use Regions\AnalyticsBundle\Service\PatternCacheService;

class PatternsController
{
    private $pcs;

    public function __construct(PatternCacheService $pcs)
    {
        $this->pcs = $pcs;
    }
}