【问题标题】:Symfony Service not existSymfony 服务不存在
【发布时间】:2013-12-15 22:32:26
【问题描述】:

我想在 symfony2 中使用控制器作为服务。我在test\testBundle\Resources\services.yml 文件中定义了服务:

parameters:
#    user.example.class: test\testBundle\Example

services:
    test.controller:
        class: test\testBundle\Controller\TestController

并在我的控制器中调用服务:

$this->get('test.controller');

但 symfony 抛出以下异常:

您请求了一个不存在的服务“test.controller”。

【问题讨论】:

    标签: php symfony service controller


    【解决方案1】:

    您可能忘记从已读取的配置文件(通常是 app/config/config.yml)导入 services.yml 文件:

    imports:
        - { resource: '@MyBundle/Resources/config.yml' }
    

     

    将配置文件放在包内的Resources/config 目录中是一种惯例,参见。 Importing Configuration with imports 在 Symfony2 文档中:

    imports:
        - { resource: '@MyBundle/Resources/config/config.yml' }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-23
      • 1970-01-01
      • 2019-02-28
      • 1970-01-01
      • 1970-01-01
      • 2020-03-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多