[英]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.