【问题标题】:ServiceNotFoundException Symfony 3.3ServiceNotFoundException Symfony 3.3
【发布时间】:2018-02-14 12:34:43
【问题描述】:

在一个 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".

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

【问题讨论】:

  • 请确保启用调试模式,如果启用生产模式则清除缓存然后刷新页面
  • @Manmohan 我在使用 cache:clear -e dev 之前确实清除了缓存 - 我在 URL ../app_dev.php/... 上运行应用程序,这应该让我进入开发模式吧?所以 - 缓存似乎不是问题。
  • 将 public: true 添加到服务定义中。服务现在默认是私有的,这意味着您不能再通过 get 访问它们。 bin/console debug:container 将确认服务可用。
  • @Cerad 谢谢!这就是问题所在。如果您将其作为答案发布,您会得到打勾:-)
  • 这不是一个真正的答案。除非这是一个大型遗留项目,否则您应该考虑升级到至少 3.4(不再支持 3.3)并花一些时间查看文档的服务容器部分。在如何使用服务方面有很多很多的变化。

标签: php symfony symfony-3.3 symfony3.x


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2015-03-14
    • 1970-01-01
    • 2016-02-23
    • 1970-01-01
    • 1970-01-01
    • 2017-12-15
    • 2018-04-30
    • 2019-07-01
    • 1970-01-01
    相关资源
    最近更新 更多