symfonypleb
symfonypleb

Reputation: 89

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). One of my services is working just fine, and the second one is giving me 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']  

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

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

Upvotes: 4

Views: 1919

Answers (1)

lordrhodos
lordrhodos

Reputation: 2745

You have not written how you inject / call your service, but calling $this->get() sounds like a call from within a controller. I guess this is related to the new change in Symfony and your default service config of the public attribute.

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.

Upvotes: 5

Related Questions