1

MyService当然是一件愚蠢的事情,但我无法注册MyBundle

src/我/MyBundle/

$ ls -R src/Me/MyBundle/

DependencyInjection/
    MeMyBundleExtension.php
    Configuration.php
Resources/
    config/
        services.yml
Services/
    MyService.php
MyBundle.php

src/Me/MyBundle/DependencyInjection/MeMyBundleExtension.php

namespace Me\MyBundle\DependencyInjection;

// standard stuff

class MeMyBundleExtension extends Extension
{
    public function load(array $configs, ContainerBuilder $container)
    {
        // standard stuff loading the yml file
    }
}

src/Me/MyBundle/DependencyInjection/Configuration.php

namespace Me\MyBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

class Configuration implements ConfigurationInterface
{
    public function getConfigTreeBuilder()
    {
        $treeBuilder = new TreeBuilder();
        $rootNode = $treeBuilder->root('my_services');

        return $treeBuilder;
    }
}

src/Me/MyBundle/Resources/config/services.yml

services:
    me.myservice:
        class: Me\MyBundle\Services\MyService

src/Me/MyBundle/Services/MyService.php

namespace Me\MyBundle\Services;

class MyService
{
    public function __construct()
    {
        die('test');
    }
}

当我尝试从控制器实例化它时,我可以看到我的服务没有注册

$test = $this->get('me.myservice');

您请求了一个不存在的服务“me.myservice”

这里有什么问题吗?


$ php app/console container:debug | grep me.mybundle

没有匹配项

4

2 回答 2

2

src/Me/MyBundle/Resources/config/services.yml

services:
    me.myservice:
        class: Me\MyBundle\Services\MyService.yml

这应该引用一个类而不是 yml 文件。所以应该是class: Me\MyBundle\Services\MyService

您还应该检查您的命名空间。如果你的包不是子包(getParent('SonataUserBundle')),你应该把你的类放在你自己的命名空间中。

src/Me/MyBundle/Services/MyService.php

namespace Me\MyBundle\Services;

src/Me/MyBundle/DependencyInjection/Configuration.php

namespace Me\MyBundle\DependencyInjection;

你也应该MyBundle.php根据MeMyBundle.phpsymfony 的命名策略。

于 2013-08-05T10:45:53.057 回答
0

有一个自动映射的事情正在进行,其中扩展类名称是从包名称派生的。前段时间我遇到了麻烦。所以我通常只是在 Bundle 类中明确指定扩展名。就像是:

namespace Cerad\Bundle\AppBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

use Cerad\Bundle\AppBundle\DependencyInjection\AppExtension;

class CeradAppBundle extends Bundle
{
    public function getContainerExtension()
    {
        return new AppExtension();
    }
}   
于 2013-08-05T14:25:05.147 回答