Error of non-existent service since symfony 3.3

I had 2 work services in my symfony 3.2 project. (8?), And I had to 3.3 (currently 3.3.2). One of my services is working fine, and the second gives me an error:
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']  

CSI \ 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);
        }
    }
}  

Error: . You requested a non-existent service "picture_upload".
Call: $uploadService = $this->get('picture_upload');

+4
source share
1 answer

You did not specify how you enter / call your service, but the call $this->get()sounds like a call from the controller. I think this is due to a new change in Symfony and your attribute default configuration 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

, ( ), .

+4

Source: https://habr.com/ru/post/1680077/


All Articles