简体   繁体   English

自symfony 3.3以来不存在的服务错误

[英]Non-existent service error since symfony 3.3

I had 2 working services at my symfony 3.2.(8?) project and had to up to 3.3 (currently 3.3.2). 我在symfony 3.2。(8?)项目中有2个工作服务,不得不达到3.3(目前为3.3.2)。 One of my services is working just fine, and the second one is giving me error: 我的一个服务工作正常,第二个是给我错误:
services.yml services.yml

parameters:
    #parameter_name: value

services:
    _defaults:
        autowire: true
        autoconfigure: true
        public: false
    AppBundle\:
        resource: '../../src/AppBundle/*'
        exclude: '../../src/AppBundle/{Entity,Repository}'
    list_brands:
          class: AppBundle\Service\ListBrands
          arguments: [ '@doctrine.orm.entity_manager' ]
          calls:
           - method: getBrands
    picture_upload:
          class: AppBundle\Service\UploadPicture
          arguments: ['@kernel']  

src\\AppBundle\\Service\\UploadPicture.php SRC \\的appbundle \\服务\\ UploadPicture.php

<?php

namespace AppBundle\Service;

use DateTime;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpKernel\Kernel;

class UploadPicture
{
    protected $kernel;

    public function __construct(Kernel $kernel)
    {
        $this->kernel = $kernel;
    }

    public function uploadPicture($object, string $oldPic, string $path)
    {
        /** @var UploadedFile $image */
        $image = $object->getImage();

        $time = new DateTime('now');

        if ($image) {
            $imgPath = '/../web/' . $path;

            $filename = $time->format('d-m-Y-s') . md5($time->format('s')) . uniqid();

            $image->move($this->kernel->getRootDir() . $imgPath,$filename . '.png');

            $object->setImage($path . $filename . '.png');
        } else {
            $object->setImage($oldPic);
        }
    }
}  

Error: You have requested a non-existent service "picture_upload". 错误: 您已请求不存在的服务“picture_upload”。
Calling it like this: $uploadService = $this->get('picture_upload'); 像这样调用它: $uploadService = $this->get('picture_upload');

You have not written how you inject / call your service, but calling $this->get() sounds like a call from within a controller. 您还没有写过如何注入/调用您的服务,但是调用$this->get()听起来像是来自控制器内的调用。 I guess this is related to the new change in Symfony and your default service config of the public attribute. 我想这与Symfony的新变化和public属性的默认服务配置有关。

Please check the following commented lines in your configuration: 请检查配置中的以下注释行:

# services.yml
services:
    _defaults:
        autowire: true
        autoconfigure: true
        public: false # here you are setting all service per default to be private
    AppBundle\:
        resource: '../../src/AppBundle/*'
        exclude: '../../src/AppBundle/{Entity,Repository}'
    list_brands:
          class: AppBundle\Service\ListBrands
          arguments: [ '@doctrine.orm.entity_manager' ]
          calls:
           - method: getBrands
    picture_upload:
          class: AppBundle\Service\UploadPicture
          arguments: ['@kernel']  
          public: true # you need to explicitly set the service to public

You need to mark the service as public, either per default (not recommended) or explicitly in the service definition. 您需要将服务标记为公共,默认情况下(不推荐)或在服务定义中明确标记。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Symfony 2称不存在的服务“路由器” - Symfony 2 calling non-existent service “router” Symfony2错误:服务“模板”依赖于不存在的服务“ templating.globals” - Symfony2 error: The service “templating” has a dependency on a non-existent service “templating.globals” Symfony 2,ServiceNotFoundException,不存在的服务“ templating.engine.php” - Symfony 2, ServiceNotFoundException, non-existent service “templating.engine.php” 如何在Symfony 4中配置不存在的服务? - How can I configure a non-existent service in Symfony 4? Symfony 4.1:你请求了一个不存在的服务“主义” - Symfony 4.1 : You have requested a non-existent service "doctrine" 您请求了一个不存在的服务“twig”错误 - You have requested a non-existent service "twig" ERROR symfony simple-phpunit错误“您仅在部署脚本中请求了不存在的服务“ test.client”” - symfony simple-phpunit error 'You have requested a non-existent service “test.client”' only in deployment script Symfony2:您请求的服务“ jms_aop.pointcut_container”不存在。 - Symfony2: You have requested a non-existent service “jms_aop.pointcut_container”. Symfony3您已请求不存在的服务“学说”。 MongoDB FOSUserBundle - Symfony3 You have requested a non-existent service “doctrine”. MongoDB FOSUserBundle Symfony:服务...依赖于不存在的参数kernel.secret - Symfony: The service … has a dependency on a non-existent parameter kernel.secret
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM