Symfony 服务 FileUploader 不自动装配

问题描述 投票:0回答:6

我按照 Symfony 5.2 教程将 FileUploader 添加为服务 (https://symfony.com/doc/current/controller/upload_file.html)。

所以这是我的 service.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%'

这是我的FileUploader.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;
    }
}

但是我遇到了这个常见错误:

无法解析“App\Controller\VideoController::edit()”的参数 $fileUploader:无法自动装配服务“App\Service\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
{...}

我该如何解决这个问题?我尝试删除 string 类型,添加字符串类型,从 services.yaml 中的 targetDirectory 参数中删除 $...现在已经为此苦苦挣扎了几个小时...

symfony file-upload service
6个回答
3
投票

看看我的工作services.yaml。我更改了命名空间

应用\服务

应用\服务

我还在文件末尾添加了服务声明。

看起来服务中的行顺序很重要。首先,我在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%'

1
投票

在构造函数中为

string
添加类型提示
$targetDirectory

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

0
投票

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

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%'

0
投票

我也有同样的问题。 它与该特定服务的缩进有关。我没有收到任何缩进错误,但自动接线也不起作用。 我所做的是添加 4 个空格作为缩进

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

0
投票

是的,我也遇到了这个问题,我设法通过在 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%有效。


0
投票

我有同样的问题,我终于发布了一个版本,我忘记将 App\Service\FileUploader 类大写,它是 App\Service ileUploader

© www.soinside.com 2019 - 2024. All rights reserved.