Symfony的2 InvalidArgumentException创建新服务
问题描述:
我想在symfony的2应用程序中创建我的第一个服务的时候,我得到这个错误:Symfony的2 InvalidArgumentException创建新服务
InvalidArgumentException: There is no extension able to load the
configuration for "my_app.myservice" (in
/path/to/src/MyApp/MyBundle/DependencyInjection/../Resources/config/services.yml).
Looked for namespace "my_app.myservice", found none.
似乎有在我的配置有问题,但我不看看它是什么。
这里是我的services.yml
services:
my_app.myservice:
class: MyApp\MyBundle\Service\MyService
我的服务看起来像这样
<?php
namespace MyApp\MyBundle\Service;
class MyService
{
public function run()
{
echo "hello world";
}
}
感谢您的帮助!
答
只是可以肯定 - 你有正确的缩进在services.yml中?
它应该是:
services:
my_app.myservice:
class: MyApp\MyBundle\Service\MyService
不是:
services:
my_app.myservice:
class: MyApp\MyBundle\Service\MyService
答
你在服务的构造函数中有任何参数吗?
例如,我在所有服务中传递记录器。
它的结果是我不得不把它传给我的service.yml
service.yml
services:
blog:
class:Site\Backend\BlogBundle\Service\BlogService
arguments: [@logger]
tags:
- { name: monolog.logger, channel: blog}
BlogService
public function __construct(LoggerInterface $logger){
$this->logger = $logger;
}
+0
都能跟得上我没有在构造函数中的任何说法。事实上,我在服务中根本没有任何构造函数。我尝试添加一个,但它不能更好地工作。 – moins52 2013-02-21 13:32:33
就是这样! Arf!它非常明显而且简单。我不敢相信我在这样的事情上浪费了太多时间。感谢您的帮助。 – moins52 2013-02-21 14:59:03