首先,我为我的基本英语用法道歉,我希望你能理解我。我正在进行一个项目的部署,开发是在Symfony 5.1上进行的,使用了easyadmin-bundle 3.1和vich/uploader 1.15。在我的本地主机上工作得很好,但是当我在仪表板上进行生产时,不能在任何有图像的实体中'Create‘或'Edit',这会给我带来这个错误。
解析表单"Vich\UploaderBundle\Form\Type\VichImageType":的选项发生错误--选项"upload_dir“、"upload_filename”不存在。
我发现upload_dir的唯一地方是在供应商文件夹中。
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发布于 2020-11-23 12:16:44
使用setFormType是一个“无证黑客”
您可以尝试以下语法:
$filename = ImageField::new('filename', 'File')
->setBasePath('uploads/contact_message')
->setUploadDir('public/uploads/contact_message/');如果不能工作,可以回滚到v.3.1.7 (强制在composer.json中使用版本),并使用带有2字段的旧语法 (文件和文件名)。
$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];发布于 2021-01-08 17:05:17
您可以创建自己的自定义字段(请参见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
https://stackoverflow.com/questions/64934444
复制相似问题