【问题标题】:Symfony 3 - You have requested a non-existent service is driving me crazySymfony 3 - 你请求一个不存在的服务让我发疯
【发布时间】:2016-02-28 13:20:51
【问题描述】:

所以,这不是我第一次创建服务,但我无法解决错误

您请求了一个不存在的服务“global_settings”。

我为确保正确设置服务而采取的步骤:

我的AppBundleExtension.php

namespace AppBundle\DependencyInjection;

use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\DependencyInjection\Loader;

class AppBundleExtension extends Extension
{
    public function load(array $configs, ContainerBuilder $container)
    {
        $configuration = new Configuration();
        $config = $this->processConfiguration($configuration, $configs);

        $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
        $loader->load('settings.xml');
    }
}

我的settings.xml

<?xml version="1.0" encoding="UTF-8" ?>
<container
        xmlns="http://symfony.com/schema/dic/services"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
    <services>
        <service id="global_settings" class="AppBundle\Services\GlobalSettings">
            <call method="setEntityManager">
                <argument type="service" id="doctrine.orm.default_entity_manager" />
            </call>
        </service>
    </services>
</container>

我的GlobalSettings 服务

namespace AppBundle\Services;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityRepository;
    class GlobalSettings
    {

        /**
         * @var EntityManager
         */
        protected $em;
        /**
         * @var EntityRepository
         */
        protected $repo;

        public function setEntityManager(EntityManager $em) {
            $this->em = $em;
            $this->repo = null;
        }

        /**
         * @return array with name => value
         */
        public function all() {
            return $this->$this->getRepo()->findAll();
        }


        /**
         * @param string $name Name of the setting.
         * @return string|null Value of the setting.
         * @throws \RuntimeException If the setting is not defined.
         */
        public function get($name) {
            $setting = $this->$this->getRepo()->findOneBy(array(
                'name' => $name,
            ));
            if ($setting === null) {
                throw $this->createNotFoundException($name);
            }
            return $setting->getValue();
        }
        /**
         * @param string $name Name of the setting to update.
         * @param string|null $value New value for the setting.
         * @throws \RuntimeException If the setting is not defined.
         */
        public function set($name, $value) {
            $setting = $this->$this->getRepo()->findOneBy(array(
                'name' => $name,
            ));
            if ($setting === null) {
                throw $this->createNotFoundException($name);
            }
            $setting->setValue($value);
            $this->em->flush($setting);
        }
        /**
         * @return EntityRepository
         */
        protected function getRepo() {
            if ($this->repo === null) {
                $this->repo = $this->em->getRepository('AppBundle:Settings');
            }
            return $this->repo;
        }

        /**
         * @param string $name Name of the setting.
         * @return \RuntimeException
         */
        protected function createNotFoundException($name) {
            return new \RuntimeException(sprintf('Setting "%s" couldn\'t be found.', $name));
        }


    }

然后在我的控制器内部,我尝试使用以下代码访问服务

$data = $this->get('global_settings')->get('paypal_email');

我做错了什么?任何帮助将不胜感激,因为我不知道。

【问题讨论】:

  • 您可以使用php app/console debug:container 列出可用服务。是否列出了您的服务?
  • @A.L 是的,我试过了,但我没有看到我的服务在那里列出,这就是我遇到的问题,我拥有通​​常用来创建服务的一切,但我的服务无法正常工作....
  • 如果在$loader-&gt;load('settings.xml');后面加上throw new \Exception('services loaded');,有例外吗?如果没有异常,说明AppBundleExtension没有加载。
  • @A.L 是的,我试过了,我知道AppBundleExtension 没有加载,我需要怎么做才能加载它?我错过了什么?
  • AppBundleExtension 的名称是否与包的名称匹配?

标签: php symfony


【解决方案1】:

你写道:

我为确保正确设置服务而采取的步骤

我的AppBundleExtension.php

还有:

我知道AppBundleExtension 没有加载,我需要怎么做才能加载它?我错过了什么?

所以很明显AppBundleExtension 类没有加载。

根据official documentation你应该删除文件名和类名中的Bundle

名称等于包名称,将Bundle 后缀替换为Extension(例如,AppBundle 的扩展类将称为AppExtension,AcmeHelloBundle 的扩展类将称为AcmeHelloExtension)。

【讨论】:

  • @Baig 我也没有立即看到它。 Bundle 必须被删除并不明显。
【解决方案2】:

您可以更新您的 config.yml 文件:

imports: 
      - { resource: "@AppBundle/Resources/config/services.yml" }

【讨论】:

    【解决方案3】:

    我不断收到此错误的原因是我的服务默认设置是public: false

    为了解决这个问题,我需要将public 属性设置为true 以供我的服务使用

    services:
        # default configuration for services in *this* file
        _defaults:
            # automatically injects dependencies in your services
            autowire: true
            # automatically registers your services as commands, event subscribers, etc.
            autoconfigure: true
            # this means you cannot fetch services directly from the container via $container->get()
            # if you need to do this, you can override this setting on individual services
            public: false
    
        my_service:
            class: AppBundle\Service\MyService
            public: true
    

    【讨论】:

    • public: true 在新版 symfony 的 services.yml 中是强制的:(
    猜你喜欢
    • 2014-03-17
    • 2016-05-04
    • 1970-01-01
    • 2014-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-18
    • 2017-11-20
    相关资源
    最近更新 更多