簡體   English   中英

如何使用FosUserBundle在UserEntity中添加個人資料圖片

[英]how to add profile picture in UserEntity with FosUserBundle

我嘗試在用戶實體中添加個人資料圖片,但失敗了,我使用symfony 3和fosuserbundle,為此,我使用列表器,這里是我的整個代碼: UserEntity的代碼:

namespace Forum\ForumBundle\Entity;

use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Security\Core\Util\SecureRandom;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\HttpFoundation\File\File;

/**
 * @ORM\Entity
 * @ORM\Table(name="fos_user")
 * @ORM\HasLifecycleCallbacks()
 */
class User extends BaseUser

{

    /**
     * @ORM\Column(type="string")
     *
     * @Assert\NotBlank(message="Please, upload the product brochure as a PDF file.")
     * @Assert\Image(
     *     
     * )
     */
    private $brochure;

    public function getBrochure()
    {
        return $this->brochure;
    }

    public function setBrochure(File $file = null)
    {
       $this->brochure = $file;

        return $this;
    }

public function __construct()
    {
        parent::__construct();
         $this->test = false; 
         //  $this->uploadProfilePicture();
        // your own logic
    }

}

我的聽眾:

namespace Forum\ForumBundle\EventListener;

use Symfony\Component\HttpFoundation\File\UploadedFile;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\Event\PreUpdateEventArgs;
use Forum\ForumBundle\Entity\User;
use Forum\ForumBundle\FileUploader;

class BrochureUploadListener
{
    private $uploader;

    public function __construct(FileUploader $uploader)
    { 
        $this->uploader = $uploader;
    }

    public function prePersist(LifecycleEventArgs $args)
    {die("good");
        $entity = $args->getEntity();

        $this->uploadFile($entity);
    }

    public function preUpdate(PreUpdateEventArgs $args)
    {
        $entity = $args->getEntity();

        $this->uploadFile($entity);
    }

    private function uploadFile($entity)
    {
        // upload only works for Product entities
        if (!$entity instanceof Product) {
            return;
        }

        $file = $entity->getBrochure();

        // only upload new files
        if (!$file instanceof UploadedFile) {
            return;
        }

        $fileName = $this->uploader->upload($file);
        $entity->setBrochure($fileName);
    }
}

偵聽器使用Uploader文件:

namespace Forum\ForumBundle;

use Symfony\Component\HttpFoundation\File\UploadedFile;

class FileUploader
{
    private $targetDir;

    public function __construct($targetDir)
    {
        $this->targetDir = $targetDir;
    }

    public function upload(UploadedFile $file)
    {
        $fileName = md5(uniqid()).'.'.$file->guessExtension();

        $file->move($this->targetDir, $fileName);

        return $fileName;
    }
}

我也配置我的服務

 app.brochure_uploader:
        class: Forum\ForumBundle\FileUploader
        arguments: ['%brochures_directory%']
    app.doctrine_brochure_listener:
        class: Forum\ForumBundle\EventListener\BrochureUploadListener
        arguments: ['@app.brochure_uploader']
        tags:
            - { name: doctrine.event_listener, event: prePersist }
            - { name: doctrine.event_listener, event: preUpdate }

我在config.yml中聲明參數:brochure-directory

parameters:
    locale: fr
    brochures_directory: 'web/uploads/brochures'

問題是當我更新實體時,兄弟

 <?php /* * This file is part of the FOSUserBundle package. * * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace FOS\\UserBundle\\Controller; use FOS\\UserBundle\\FOSUserEvents; use FOS\\UserBundle\\Event\\FormEvent; use FOS\\UserBundle\\Event\\FilterUserResponseEvent; use FOS\\UserBundle\\Event\\GetResponseUserEvent; use FOS\\UserBundle\\Model\\UserInterface; use Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller; use Symfony\\Component\\HttpFoundation\\Request; use Symfony\\Component\\HttpFoundation\\RedirectResponse; use Symfony\\Component\\Security\\Core\\Exception\\AccessDeniedException; /** * Controller managing the user profile * * @author Christophe Coevoet <stof@notk.org> */ class ProfileController extends Controller { /** * Show the user */ public function showAction() { $user = $this->getUser(); if (!is_object($user) || !$user instanceof UserInterface) { throw new AccessDeniedException('This user does not have access to this section.'); } return $this->render('FOSUserBundle:Profile:show.html.twig', array( 'user' => $user )); } /** * Edit the user */ public function editAction(Request $request) { $user = $this->getUser(); if (!is_object($user) || !$user instanceof UserInterface) { throw new AccessDeniedException('This user does not have access to this section.'); } /** @var $dispatcher \\Symfony\\Component\\EventDispatcher\\EventDispatcherInterface */ $dispatcher = $this->get('event_dispatcher'); $event = new GetResponseUserEvent($user, $request); $dispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_INITIALIZE, $event); if (null !== $event->getResponse()) { return $event->getResponse(); } /** @var $formFactory \\FOS\\UserBundle\\Form\\Factory\\FactoryInterface */ $formFactory = $this->get('fos_user.profile.form.factory'); $form = $formFactory->createForm(); $form->setData($user); $form->handleRequest($request); if ($form->isValid()) { /** @var $userManager \\FOS\\UserBundle\\Model\\UserManagerInterface */ $userManager = $this->get('fos_user.user_manager'); $event = new FormEvent($form, $request); $dispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_SUCCESS, $event); $userManager->updateUser($user); if (null === $response = $event->getResponse()) { $url = $this->generateUrl('fos_user_profile_show'); $response = new RedirectResponse($url); } $dispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_COMPLETED, new FilterUserResponseEvent($user, $request, $response)); return $response; } return $this->render('FOSUserBundle:Profile:edit.html.twig', array( 'form' => $form->createView() )); } } 

您是否嘗試過將config.yml更改為:

parameters:
    locale: fr
    brochures_directory: '%kernel.root_dir%/../web/uploads/brochures'

也許那是問題所在? 請先嘗試。

編輯#2。 其次,您可以編輯app / config / parameters.yml並添加以下參數:

brochures_directory: uploads/brochures

還要確保每次都清除緩存:

php bin/console cache:clear --env=prod

看看是否可行。

編輯#3。 我注意到您的配置與默認配置完全不同,並且我試圖復制該設置,但是這樣做涉及很多工作。

我發現使用的方法與您使用的方法有所不同,這也可能會影響您所看到的問題:

parameters:
   locale: fr
   brochures_directory: uploads/brochures

因此,換句話說,刪除引號並去除“ web”前綴。 還要確保您已經創建了這些文件夾,並在其下創建了“上載”和“ brochures”文件夾。

看看是否可行。

就像我說過要嘗試復制一樣,然后我得到“請以PDF文件上傳產品手冊”。 在“注冊”頁面上,但我仍然需要大量工作來嘗試復制您的設置。

根據我收到的消息,我認為這可能很簡單。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM