4

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

错误: 您请求了一个不存在的服务“picture_upload”。
像这样称呼它: $uploadService = $this->get('picture_upload');

4

1 回答 1

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

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

于 2017-06-25T18:32:13.213 回答