sym*_*leb 4 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']
Run Code Online (Sandbox Code Playgroud)
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
错误: 您已请求不存在的服务"picture_upload".
这样称呼它: $uploadService = $this->get('picture_upload');
您还没有写过如何注入/调用您的服务,但是调用$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
Run Code Online (Sandbox Code Playgroud)
您需要将服务标记为公共,默认情况下(不推荐)或在服务定义中明确标记.
| 归档时间: |
|
| 查看次数: |
1301 次 |
| 最近记录: |