2

在 Symfony 3.3 项目中,我定义了一个类

../AppBundle/Helper/ProgramHelper

像这样

class ProgramHelper implements ContainerAwareInterface
{
    use ContainerAwareTrait;
    protected $em;
    public function __construct(EntityManager $em)
    {
        $this->em = $em;
    }
}

在 services.yml 我这样添加

services:
    ...
    app.helper.program_helper:
        class: AppBundle\Helper\ProgramHelper
        tags:
             - { name: app.helper, alias: 'container_aware' }
        arguments: [ "@doctrine.orm.entity_manager" ]
        calls:
            - [setContainer, ['@service_container']]

现在 - 尝试从控制器访问类

$ph = $this->get('app.helper.program_helper');

导致此错误

ServiceNotFoundException
You have requested a non-existent service "app.helper.program_helper".

非常感谢有关该问题的任何提示。

4

1 回答 1

4

@Cerad 回答了这个问题 - 谢谢!

将 public: true 添加到服务定义中。服务现在默认是私有的,这意味着您不能再通过 get 访问它们。bin/console debug:container 将确认服务可用。

于 2018-02-14T17:00:05.407 回答