【问题标题】:Retrieve the user during creation Document PDF Symfony在创建文档 PDF Symfony 期间检索用户
【发布时间】:2021-05-11 04:09:50
【问题描述】:

创建pdf文档时如何找回登录用户(管理员)?

用户已连接。用户已连接,并从仪表板创建文档

DocumentController.php

/**
     * @Route("/new", name="document_new", methods={"GET","POST"})
     */
    public function new(Request $request): Response
    {
        
        $document = new Document();
        $document->setCreatedAt(new \DateTime('now'));
        $form = $this->createForm(DocumentType::class, $document);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $document->setUsers($this->getUser());
            $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', array('id' => $document->getId()));
        }

        return $this->render('document/new.html.twig', [
          
            'form' => $form->createView(),
        ]);
    }

谢谢。

【问题讨论】:

    标签: symfony symfony4


    【解决方案1】:

    希望对您有所帮助

    /**
     * @Route("/new", name="document_new", methods={"GET","POST"})
     * @IsGranted("ROLE_ADMINISTRATOR") // if you want allowed this action only for ROLE_ADMINISTRATOR => replace by role that you want,
     */
    public function new(Request $request): Response
    {
    
        // you need juste call helper function $this->getUser() 
        $currentUser =  $this->getUser();
    
        $document = new Document();
        $document->setCreatedAt(new \DateTime('now'));
        $form = $this->createForm(DocumentType::class, $document);
        $form->handleRequest($request);
    
        if ($form->isSubmitted() && $form->isValid()) {
            $document->setUsers($this->getUser());
            $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', array('id' => $document->getId()));
        }
    
        return $this->render('document/new.html.twig', [
          
            'form' => $form->createView(),
        ]);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多