【发布时间】:2021-03-04 03:54:32
【问题描述】:
首先,我为我基本的英语使用道歉,希望你能理解我。 我正在部署一个项目,开发是在 Symfony 5.1 上,使用 easyadmin-bundle 3.1 和 vich/uploader-bundle 1.15。在我的本地主机上效果很好,但是当我转向生产时,在我的仪表板上,不能在任何内部有图像的实体中 'Create New' 或 'Edit' ,它给我这个错误。
解析“Vich\UploaderBundle\Form\Type\VichImageType”表单选项时出错:选项“upload_dir”、“upload_filename”不存在。
我发现 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_dir和upload_filename选项。
标签: php symfony vichuploaderbundle