微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Symfony 5 vich_upload 删除文件不起作用

如何解决Symfony 5 vich_upload 删除文件不起作用

我在 symfony 5 上遇到了 Vich_upload 的问题,我无法用普通的 crud 删除文件
我查了很多论坛和git问题,我使用文档中的生命周期事件配置但无法解决问题。
我可以上传和编辑文件,但不能删除它。
文件不会删除数据库,也不会删除到保存文件夹。
实体图片.PHP

        /**
             * @ORM\Column(type="string",length=255)
             * @Assert\Length(min = 3,max = 255,minMessage = "Le nom de l'image doit être minimum égale à {{ limit }} caractères.",maxMessage = "Le nom de l'image doit être inférieur à {{ limit }} caractères.")
             */
            private $image_label;
        **
             * @Vich\UploadableField(mapping="product_image",fileNameProperty="image_label")
             * @var File|null
             */
            private $image_file;
        /**
             * @ORM\Column(type="datetime")
             */
            private $updated_at;
    /**
         * @return File|null
         */
        public function getimageFile(): ?File
        {
            return $this->image_file;
        }
    
        /**
         * @param File $image_file
         * @return Image
         */
        public function setimageFile(File $image_file): Image
        {
            $this->image_file = $image_file;
    
            if ($this->image_file instanceof UploadedFile) {
                // It is required that at least one field changes if you are using doctrine
                // otherwise the event listeners won't be called and the file is lost
                $this->updated_at = new DateTime("Now");
            }
    
            return $this;
        }
 /**
     * @return DateTimeInterface|null
     */
    public function getUpdatedAt(): ?DateTimeInterface
    {
        return $this->updated_at;
    }

    /**
     * @param DateTimeInterface $updated_at
     * @return $this
     */
    public function setUpdatedAt(DateTimeInterface $updated_at): self
    {
        $this->updated_at = $updated_at;

        return $this;
    }

    

ImageType.PHP

 public function buildForm(FormBuilderInterface $builder,array $options)
    {
        $builder
            /*.....*/
            ->add('image_file',VichImageType::class,[
                'required'      => false,'allow_delete'  => true,'download_link' => true,])
           /*......*/
        ;
    }

vich_uploader.yaml

vich_uploader:
    db_driver: orm

    mappings:
        product_image:
            uri_prefix: /images/products
            upload_destination: '%kernel.project_dir%/public/images/products'
            namer: Vich\UploaderBundle\Naming\OrignameNamer

        product_video:
            uri_prefix: /video
            upload_destination: '%kernel.project_dir%/public/video'
            namer: Vich\UploaderBundle\Naming\OrignameNamer

            inject_on_load: false
            delete_on_update: true
            delete_on_remove: true

ImageController.PHP

 /**
     * @Route("/{id}",name="image_delete",methods={"DELETE"})
     * @param Request $request
     * @param Image $image
     * @return Response
     */
    public function delete(Request $request,Image $image): Response
    {
        if ($this->isCsrftokenValid('delete'.$image->getId(),$request->request->get('_token'))) {
            $entityManager = $this->getDoctrine()->getManager();
            $entityManager->remove($image);
            $entityManager->flush();
        }

        return $this->redirectToRoute('image_index');
    }

_delete_form.html.twig

<form method="post" action="{{ path('image_delete',{'id': image.id}) }}" onsubmit="return confirm('Êtes-vous sûr de vouloir supprimer cette image ?');">
    <input type="hidden" name="_method" value="DELETE">
    <input type="hidden" name="_token" value="{{ csrf_token('Supprimer' ~ image.id) }}">
    <button class="btn btn-outline-danger my-1">Supprimer</button>
</form>

先谢谢你。
最好的问候。

解决方法

我犯了一个大错误,我翻译了 csrf 令牌 value="{{ csrf_token('Supprimer' ~ image.id) }}" 最终无法验证令牌,我的错,抱歉打扰。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。