4

我正在尝试在 symfony 2 应用程序中创建我的第一个服务,但出现此错误:

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";
    }
}

感谢帮助 !

4

2 回答 2

2

只是为了确定-您在 services.yml 中有适当的缩进吗?

它应该是:

services:
  my_app.myservice:
    class: MyApp\MyBundle\Service\MyService 

不是:

services:
my_app.myservice:
  class: MyApp\MyBundle\Service\MyService
于 2013-02-21T14:45:31.403 回答
0

您的服务构造函数中有任何参数吗?

例如,我在所有服务中传递记录器。

结果是我必须在我的 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;
}
于 2013-02-21T13:18:48.970 回答