【问题标题】:VichUploaderBundle, Form\Type\VichImageType Error ProduccionVichUploaderBundle,Form\Type\VichImageType 错误产生
【发布时间】:2021-03-04 03:54:32
【问题描述】:

首先,我为我基本的英语使用道歉,希望你能理解我。 我正在部署一个项目,开发是在 Symfony 5.1 上,使用 easyadmin-bundle 3.1vich/uploader-bundle 1.15。在我的本地主机上效果很好,但是当我转向生产时,在我的仪表板上,不能在任何内部有图像的实体中 'Create New''Edit' ,它给我这个错误。

解析“Vich\UploaderBundle\Form\Type\VichImageType”表单选项时出错:选项“upload_dir”、“upload_filename”不存在。

https://i.ibb.co/gWRjPLm/Screenshot-2020-11-20-An-error-has-occurred-resolving-the-options-of-the-form-Vich-Uploader-Bundle-F.png

我发现 upload_dir 的唯一位置是在 vendor 文件夹中。

https://i.ibb.co/VHw39z5/Screenshot-2020-11-20-Symfony-Profiler.png

我的实体

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Vich\UploaderBundle\Mapping\Annotation as Vich;

/**
 * @ORM\Entity(repositoryClass=ColoresRepository::class)
 * @Vich\Uploadable()
 */
class Colores
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=30)
     */
    private $nombre;

    /**
     * @ORM\Column(type="string", length=100)
     */
    private $thumbnail;

    /**
     * @Vich\UploadableField(mapping="colores", fileNameProperty="thumbnail")
     */
    private $thumbnailFile;

    /**
     * @ORM\Column(type="datetime")
     */
    private $updatedAt;

    public function __construct()
    {
        $this->updatedAt = new \DateTime();
    }

    /**
     * @return mixed
     */
    public function getThumbnailFile()
    {
        return $this->thumbnailFile;
    }

    /**
     * @param mixed $thumbnailFile
     */
    public function setThumbnailFile($thumbnailFile): void
    {
        $this->thumbnailFile = $thumbnailFile;

        if($thumbnailFile) {
            $this->updatedAt = new \DateTime();
        }
    }

    /**
     * @return mixed
     */
    public function getThumbnail()
    {
        return $this->thumbnail;
    }

    /**
     * @param mixed $thumbnail
     */
    public function setThumbnail($thumbnail): void
    {
        $this->thumbnail = $thumbnail;
    }

    public function getUpdatedAt(): ?\DateTimeInterface
    {
        return $this->updatedAt;
    }

    public function setUpdatedAt(\DateTimeInterface $updatedAt): self
    {
        $this->updatedAt = $updatedAt;

        return $this;
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getNombre(): ?string
    {
        return $this->nombre;
    }

    public function setNombre(string $nombre): self
    {
        $this->nombre = $nombre;

        return $this;
    }

    public function __toString()
    {
        return $this->nombre;
    }

}

我的仪表板

<?php

namespace App\Controller\Admin;

use EasyCorp\Bundle\EasyAdminBundle\Config\Dashboard;
use EasyCorp\Bundle\EasyAdminBundle\Config\MenuItem;
use EasyCorp\Bundle\EasyAdminBundle\Router\CrudUrlGenerator;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractDashboardController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use App\Entity\Colores;




class DashboardController extends AbstractDashboardController
{
    /**
     * @Route("admin", name="admin")
     */
    public function index(): Response
    {
        $routeBuilder = $this->get(CrudUrlGenerator::class)->build();

        return $this->redirect($routeBuilder->setController(ColoresCrudController::class)->generateUrl());
    }

    public function configureDashboard(): Dashboard
    {
        return Dashboard::new()
            ->setTitle('Test Site');
    }

    public function configureMenuItems(): iterable
    {
        yield MenuItem::section('DESTACADOS');
        yield MenuItem::linkToCrud('Colores', 'fa fa-paint-brush', Colores::class);

}

我的 Crud 控制器

<?php

namespace App\Controller\Admin;

use App\Entity\Colores;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
use EasyCorp\Bundle\EasyAdminBundle\Field\ImageField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
use Vich\UploaderBundle\Form\Type\VichImageType;

class ColoresCrudController extends AbstractCrudController
{
    public static function getEntityFqcn(): string
    {
        return Colores::class;
    }

    public function configureFields(string $pageName): iterable
    {
        return [
            TextField::new('nombre'),
            ImageField::new('thumbnailFile')
                ->setFormType(VichImageType::class)->onlyOnForms(),
            ImageField::new('thumbnail')
                ->setBasePath('/images/colores')->hideOnForm()
        ];
    }

}

vich_uploader.yaml

vich_uploader:
    db_driver: orm

    mappings:
        colores:
            uri_prefix: /images/colores
            upload_destination: '%kernel.project_dir%/public/images/colores'
            namer: Vich\UploaderBundle\Naming\UniqidNamer

【问题讨论】:

  • 你的英语很好。至于您的问题:您的应用程序配置中似乎缺少 upload_dirupload_filename 选项。

标签: php symfony vichuploaderbundle


【解决方案1】:

使用 setFormType 是一个"undocumented hack"

有一个issue with the v3.1.8 and the ImageField

你可以试试这个语法:

$filename     = ImageField::new('filename', 'File')
                          ->setBasePath('uploads/contact_message')
                          ->setUploadDir('public/uploads/contact_message/');

如果它不起作用,您可以回滚到 v.3.1.7(强制 composer.json 中的版本)并使用带有 2 个字段(文件和文件名)的 old syntax

$avatar = ImageField::new('avatar')->setBasePath('uploads/images/users')->setLabel('Photo');
$avatarFile = ImageField::new('avatarFile')->setFormType(VichImageType::class);

 if (Crud::PAGE_INDEX === $pageName) {
            return [ $avatar];
     } elseif (Crud::PAGE_EDIT=== $pageName) {
return [$avatarFile];

【讨论】:

  • 是的,我回滚到 v 3.1.7,花了我一段时间才找到解决方案。谢谢你的回答。
【解决方案2】:

您可以像这样创建自己的自定义字段(请参阅https://symfony.com/doc/current/bundles/EasyAdminBundle/fields.html#creating-custom-fields):

namespace App\Admin\Field;

use EasyCorp\Bundle\EasyAdminBundle\Contracts\Field\FieldInterface;
use EasyCorp\Bundle\EasyAdminBundle\Field\FieldTrait;
use Vich\UploaderBundle\Form\Type\VichImageType;

class VichImageField implements FieldInterface
{
    use FieldTrait;

    public static function new(string $propertyName, ?string $label = null): self
    {
        return (new self())
            ->setProperty($propertyName)
            ->setLabel($label)
            ->setFormType(VichImageType::class)
            ->setCustomOption('image_uri', null)
            ->setCustomOption('download_uri', null)
            ;
    }

    public function setImageUri($imageUri): self
    {
        $this->setCustomOption('image_uri', $imageUri);

        return $this;
    }

    public function setDownloadUri($downloadUri): self
    {
        $this->setCustomOption('download_uri', $downloadUri);

        return $this;
    }
}

然后在你的 crud 控制器 configureFileds 方法中使用它:

            VichImageField::new('avatarFile', 'Avatar')
            ->setDownloadUri('public/' . $this->getParameter('app.path.user_avatars'))
            ->setImageUri($this->getParameter('app.path.user_avatars'))
        ,

它适用于最后一个 EasyAdminBundle > v3.1.7

【讨论】:

    猜你喜欢
    • 2019-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多