Symfony服务文件上传程序未自动安装

2ic8powd  于 2023-01-05  发布在  其他
关注(0)|答案(5)|浏览(131)

我已经按照Symfony 5.2教程添加了一个FileUploader作为服务(https://symfony.com/doc/current/controller/upload_file.html)。
这就是我的服务yaml

parameters:
  targetDirectory: '%kernel.project_dir%/public/uploads/'
  previews_video: '%kernel.project_dir%/public/uploads/previews'
  brochures_directory: '%kernel.project_dir%/public/uploads/brochures'
services:
  App\Service\FileUploader:
      arguments:
          $targetDirectory: '%previews_video%'

这是我的文件上传器.php

<?php

namespace App\Service;
use Symfony\Component\HttpFoundation\File\Exception\FileException;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\String\Slugger\SluggerInterface;

class FileUploader
{
    private $targetDirectory;
    private $slugger;

    public function __construct($targetDirectory, SluggerInterface $slugger)
    {
        $this->targetDirectory = $targetDirectory;
        $this->slugger = $slugger;
    }

    public function upload(UploadedFile $file)
    {
        $originalFilename = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME);
        $safeFilename = $this->slugger->slug($originalFilename);
        $fileName = $safeFilename.'-'.uniqid().'.'.$file->guessExtension();

        try {
            $file->move($this->getTargetDirectory(), $fileName);
        } catch (FileException $e) {
            // ... handle exception if something happens during file upload
        }

        return $fileName;
    }

    public function getTargetDirectory()
    {
        return $this->targetDirectory;
    }
}

但我有一个常见的错误:
无法解析“应用程序\控制器\视频控制器::edit()"的参数$fileUploader:无法自动连接服务“应用程序\服务\文件上传程序”:方法“__construct()”的参数“$targetDirectory”没有类型提示,您应该显式配置其值。
由该控制器调用:

/**
 * @Route("/{id}/edit", name="video_edit", methods={"GET","POST"})
 * @param Request $request
 * @param Video $video
 * @param FileUploader $fileUploader
 * @return Response
 */
public function edit(Request $request, Video $video, FileUploader $fileUploader): Response
{...}

我该如何解决这个问题?我尝试删除字符串类型,添加字符串类型,从services. yaml中的targetDirectory参数中删除**$**...

j0pj023g

j0pj023g1#

看看我正在工作的services.yaml。我已经更改了名称空间
应用程序\服务

应用程序\服务
而且我还在文件的末尾添加了服务声明。
看起来services中的行的顺序很重要。首先,我在services部分的顶部添加了声明,但是autowiring是在后面声明的,我猜错误就在这里...

# This file is the entry point to configure your own services.
# Files in the packages/ subdirectory configure your dependencies.

# Put parameters here that don't need to change on each machine where the app is deployed
# https://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
parameters:
    previews_directory: '%kernel.project_dir%/public/uploads/previews'
services:
  #i've added my service here at first... 
    app.menu_builder:
        class: App\Menu\MenuBuilder
        arguments: ["@knp_menu.factory"]
        tags:
            - { name: knp_menu.menu_builder, method: createMainMenu, alias: main }
    # default configuration for services in *this* file
    _defaults:
        autowire: true      # Automatically injects dependencies in your services.
        autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.

    # makes classes in src/ available to be used as services
    # this creates a service per class whose id is the fully-qualified class name
    App\:
        resource: '../src/'
        exclude:
            - '../src/DependencyInjection/'
            - '../src/Entity/'
            - '../src/Kernel.php'
            - '../src/Tests/'

    # controllers are imported separately to make sure services can be injected
    # as action arguments even if you don't extend any base controller class
    App\Controller\:
        resource: '../src/Controller/'
        tags: ['controller.service_arguments']

    # add more service definitions when explicit configuration is needed
    # please note that last definitions always *replace* previous ones

    App\Services\FileUploader:
        arguments:
            $targetDirectory: '%previews_directory%'
u4vypkhs

u4vypkhs2#

您应该将自动装配配置添加到服务文件中:

parameters:
  targetDirectory: '%kernel.project_dir%/public/uploads/'
  previews_video: '%kernel.project_dir%/public/uploads/previews'
  brochures_directory: '%kernel.project_dir%/public/uploads/brochures'
services:
  # To add:
  _defaults:
      autowire: true
      autoconfigure: true
  # You service
  App\Service\FileUploader:
      arguments:
          $targetDirectory: '%previews_video%'
gwo2fgha

gwo2fgha3#

在构造函数中为$targetDirectory添加类型提示string

public function __construct(string $targetDirectory, SluggerInterface $slugger)
{
    $this->targetDirectory = $targetDirectory;
    $this->slugger = $slugger;
}
31moq8wy

31moq8wy4#

我也遇到过同样的问题,就是那个服务的缩进问题,我没有遇到缩进错误,但是自动连线也不行,我就加了4个空格作为缩进

App\Service\FileUploader:
        arguments:
            $targetDirectory: '%TEAM_LOGO_DIRECTORY%'
vwhgwdsa

vwhgwdsa5#

是的,我也遇到了这个问题,我设法解决了它,通过在services.yaml文件中用空格替换缩进,我在同一个根中添加了所有这些属性,然后我这样做了,它对我很有效:

services:
    App\Services\FileUploader: #(added 4 spaces, NOT 1 tab)
        arguments: #(added 8 spaces, NOT 2 tabs)
            $targetDirectory: '%cats_directory%' #(added 12 spaces, NOT 3 tabs)

如果你(和其他遇到这个问题的人)很纠结,可以试试这个解决方案,我不保证它100%有效。

相关问题