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

时间:2017-06-25 18:25:14

标签: php symfony symfony-3.3

我在我的symfony 3.2。(8?)项目中有2个工作服务,并且必须达到3.3(目前为3.3.2)。我的一项服务工作得很好,第二项是给我错误:
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 \服务\ 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);
        }
    }
}  

错误: 您已请求不存在的服务&#34; picture_upload&#34;。
这样称呼: $uploadService = $this->get('picture_upload');

1 个答案:

答案 0 :(得分:5)

您还没有写过如何注入/调用您的服务,但是调用$this->get()听起来像是来自控制器内的呼叫。我想这与Symfony的新变化和public属性的默认服务配置有关。

请检查配置中的以下注释行:

# 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

您需要将服务标记为公共,默认情况下(不推荐)或在服务定义中明确标记。