由网友(冷月星空)分享简介:我有这样的配置,它允许我在CRUD中创建一个pdf文档,有没有办法将此代码添加到CRUD easyAdmin中,或者将我的EasyAdmin文档的CRUD链接到symfony的CRUD。我在EasyAdmin表中创建文档时遇到问题DocumentController.php<?phpnamespace AppCo...我有这样的配置,它允许我在CRUD中创建一个pdf文档,有没有办法将此代码添加到CRUD easyAdmin中,或者将我的EasyAdmin文档的CRUD链接到symfony的CRUD。
我在EasyAdmin表中创建文档时遇到问题
DocumentController.php
<?php
namespace AppController;
use AppEntityDocument;
use AppFormDocumentType;
use AppRepositoryDocumentRepository;
use SymfonyBundleFrameworkBundleControllerAbstractController;
use AppServiceFileUploader;
use SymfonyComponentHttpFoundationRequest;
use SymfonyComponentHttpFoundationResponse;
use SymfonyComponentRoutingAnnotationRoute;
use SensioBundleFrameworkExtraBundleConfigurationIsGranted;
/**
* @IsGranted("ROLE_USER")
* @Route("/documents")
*/
class DocumentController extends AbstractController
{
/**
* @Route("/", name="document_index", methods={"GET"})
*/
public function index(DocumentRepository $documentRepository): Response
{
return $this->render('document/index.html.twig', [
'documents' => $documentRepository->findAll([], ['created_at' => 'desc']),
]);
}
/**
* @Route("/new", name="document_new", methods={"GET","POST"})
*/
public function new(Request $request, FileUploader $fileUploader): Response
{
$document = new Document();
$document->setCreatedAt(new DateTime('now'));
$form = $this->createForm(DocumentType::class, $document);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager = $this->getDoctrine()->getManager();
$file = $form['fileDocument']->getData();
$originalFilename = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME);
// this is needed to safely include the file name as part of the URL
$fileName = transliterator_transliterate('Any-Latin; Latin-ASCII; [^A-Za-z0-9_] remove; Lower()', $originalFilename);
$fileName = md5(uniqid()) . '.' . $file->guessExtension();
$file->move(
$this->getParameter('brochures_directory'),
$fileName
);
$document->setFileDocument($fileName);
$entityManager->persist($document);
$entityManager->flush();
return $this->redirectToRoute('document_index', array('id' => $document->getId()));
}
return $this->render('document/new.html.twig', [
// 'document' => $document,
'form' => $form->createView(),
]);
}
/**
* @Route("/{id}", name="document_show", methods={"GET"})
*/
public function show(Document $document): Response
{
return $this->render('document/show.html.twig', [
'document' => $document,
]);
}
/**
* @Route("/{id}/edit", name="document_edit", methods={"GET","POST"})
*/
public function edit(Request $request, Document $document): Response
{
$form = $this->createForm(DocumentType::class, $document);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$file = $form['fileDocument']->getData();
$originalFilename = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME);
// this is needed to safely include the file name as part of the URL
$fileName = transliterator_transliterate('Any-Latin; Latin-ASCII; [^A-Za-z0-9_] remove; Lower()', $originalFilename);
$fileName = md5(uniqid()) . '.' . $file->guessExtension();
$file->move(
$this->getParameter('brochures_directory'),
$fileName
);
$document->setFileDocument($fileName);
$this->getDoctrine()->getManager()->flush();
return $this->redirectToRoute('document_index');
}
return $this->render('document/edit.html.twig', [
'document' => $document,
'form' => $form->createView(),
]);
}
/**
* @Route("/{id}", name="document_delete", methods={"DELETE"})
*/
public function delete(Request $request, Document $document): Response
{
if ($this->isCsrfTokenValid('delete' . $document->getId(), $request->request->get('_token'))) {
$entityManager = $this->getDoctrine()->getManager();
$entityManager->remove($document);
$entityManager->flush();
}
return $this->redirectToRoute('document_index');
}
}
DocumentCrudController轻松管理
<?php
namespace AppControllerAdmin;
use AppEntityDocument;
use AppEntityPublication;
use EasyCorpBundleEasyAdminBundleConfigAction;
use EasyCorpBundleEasyAdminBundleConfigActions;
use EasyCorpBundleEasyAdminBundleConfigCrud;
use EasyCorpBundleEasyAdminBundleControllerAbstractCrudController;
use EasyCorpBundleEasyAdminBundleFieldDateTimeField;
use EasyCorpBundleEasyAdminBundleFieldIdField;
use EasyCorpBundleEasyAdminBundleFieldImageField;
use EasyCorpBundleEasyAdminBundleFieldTextareaField;
use EasyCorpBundleEasyAdminBundleFieldTextField;
use SymfonyComponentFormExtensionCoreTypeFileType;
use SymfonyComponentValidatorConstraintsFile;
class DocumentCrudController extends AbstractCrudController
{
public static function getEntityFqcn(): string
{
return Document::class;
}
public function configureCrud(Crud $crud): Crud
{
return $crud
->setPageTitle(Crud::PAGE_INDEX, 'Liste de Documents')
;
}
public function configureFields(string $pageName): iterable
{
ImageField::new('fileDocument', 'Document PDF')->setFormType(FileType::class)
->setBasePath('docs');
return [
IdField::new('id')->onlyOnIndex(),
TextField::new('nomDocument', 'Titre'),
DateTimeField::new('created_at', 'Date de création'),
TextField::new('fileDocument', 'Document PDF')
->hideOnIndex()
->setFormType(FileType::class, [
'constraints' => [
new File([
'maxSize' => '1024k',
'mimeTypes' => [
'application/pdf',
'application/x-pdf',
],
'mimeTypesMessage' => 'Veuillez télécharger un document PDF valide',
])
],
]),
];
}
public function configureActions(Actions $actions): Actions
{
return $actions
->add(Crud::PAGE_INDEX, Action::DETAIL);
}
}
我不知道如何在Easy Admin中实现相同的配置。 Look Here这是我从表EasyAdmin创建文档时发生的情况。 谢谢。
推荐答案
这有点奇怪,但您需要使用Imagefield(https://symfony.com/bundles/EasyAdminBundle/current/fields/ImageField.html)
和In You CrudController:
public function configureFields(string $pageName): iterable{
return [
ImageField::new('pdf', 'Your PDF')
->setFormType(FileUploadType::class)
->setBasePath('documents/') //see documentation about ImageField to understand the difference beetwen setBasePath and setUploadDir
->setUploadDir('public/documents/')
->setColumns(6)
->hideOnIndex()
->setFormTypeOptions(['attr' => [
'accept' => 'application/pdf'
]
]),
];
}
请参阅有关Imagefield的文档,了解setBasePath和setUploadDir的区别
-编辑-
在CRUD的索引页中,您可以为您的文件创建如下链接:
public function configureFields(string $pageName): iterable{
return [
ImageField::new('pdf', 'Your PDF')
->setFormType(FileUploadType::class)
->setBasePath('documents/') //see documentation about ImageField to understand the difference beetwen setBasePath and setUploadDir
->setUploadDir('public/documents/')
->setColumns(6)
->hideOnIndex()
->setFormTypeOptions(['attr' => [
'accept' => 'application/pdf'
]
]),
TextField::new('pdf')->setTemplatePath('admin/fields/document_link.html.twig')->onlyOnIndex(),
];
}
您的templates/admin/fields/document_link.html.twig:
{% if field.value %}
<a href="{{ field.value }}" target="_blank">Download file</a>
{% else %}
--
{% endif %}
相关推荐
最新文章