Php Symfony服务不存在
我想在symfony2中将控制器用作服务。我在Php Symfony服务不存在,php,symfony,service,controller,Php,Symfony,Service,Controller,我想在symfony2中将控制器用作服务。我在test\testBundle\Resources\services.yml文件中定义了服务: parameters: # user.example.class: test\testBundle\Example services: test.controller: class: test\testBundle\Controller\TestController 并调用控制器中的服务: $this->get('t
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”
您可能忘记了从正在读取的配置文件(通常是app/config/config.yml)导入
services.yml
文件:
imports:
- { resource: '@MyBundle/Resources/config.yml' }
将配置文件放在捆绑包中的Resources/config
目录中是一种惯例,参见Symfony2文档:
imports:
- { resource: '@MyBundle/Resources/config/config.yml' }