php Symfony 3 - 你请求一个不存在的服务让我发疯

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35683041/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:32:17  来源:igfitidea点击:

Symfony 3 - You have requested a non-existent service is driving me crazy

phpsymfony

提问by Baig

Okay so this is not the first time I am creating the service but I just cant resolve the error

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

You have requested a non-existent service "global_settings".

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

Steps i took to ensure service is properly setup

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

My AppBundleExtension.php

我的 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');
    }
}

My 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>

My GlobalSettingsservice

我的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));
        }


    }

Then inside my controller I am doing is trying to access the service using the following code

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

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

What am i doing wrong? Any help will be really appreciate as i am out of all ideas.

我究竟做错了什么?任何帮助将不胜感激,因为我没有任何想法。

采纳答案by A.L

You wrote:

你写了:

Steps i took to ensure service is properly setup

My AppBundleExtension.php

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

我的 AppBundleExtension.php

And:

和:

i know AppBundleExtension is not loading, what do i need to do to load it? what am i missing?

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

So it was clear that the AppBundleExtensionclass was not loaded.

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

According to the official documentationyou should remove the Bundlein the file name and class name:

根据官方文档,您应该删除Bundle文件名和类名中的:

The name is equal to the bundle name with the Bundlesuffix replaced by Extension(e.g. the Extension class of the AppBundle would be called AppExtensionand the one for AcmeHelloBundle would be called AcmeHelloExtension).

名称为等于与包名称Bundle改为后缀Extension(例如,扩展类的appbundle的将被称为AppExtension和一个用于AcmeHelloBundle将被称为AcmeHelloExtension)。

回答by totas

The reason why I kept getting this error was that my default setting for services was public: false

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

So to fix that I needed to set the publicproperty to truefor my service

所以为了解决这个问题,我需要为我的服务设置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

回答by Shoooryuken

you can update your config.yml file:

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

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

进口:- { 资源:“@AppBundle/Resources/config/services.yml”}