2016-08-24 55 views
-5

我正在用symfony3做一個web應用程序,我需要一種方法讓用戶編輯和更新他們的配置文件。我使用FosUserBundle來管理用戶訪問,但我不知道要採取什麼措施來解決我的問題。 有人有一些想法或有用的鏈接分享? 謝謝使用fosuserbundle更新用戶配置文件symfony3

+1

看看他們的[GitHub Page](https://github.com/FriendsOfSymfony/FOSUserBundle),你會發現你需要的一切。提示:查看簡介頁面。一般提示:你是StackOverflow的新手,所以請查看[How-to-ask](http://stackoverflow.com/help/how-to-ask)頁面。你的問題是無關緊要的,因爲它是關於尋找工具或第三方頁面的,所以它很可能會被關閉。 – KhorneHoly

+0

問一個好問題,得到一個很好的迴應。 –

+0

如果你買洗衣機,你問如何在互聯網上使用它?只需閱讀產品隨附的文檔。 – malcolm

回答

-1

我解決了這個問題。是的,這是我在stackoverflow上的第一個問題,我正在學習。
這裏是我的代碼:

dogController.php

 /** 
    * @Route("/profile/edit", name= "edit_profile" ) 
    */ 

    public function edit_profileAction(Request $request) 
    { 

     $user = $this->getUser(); 

     $form = $this->createForm(UserType::class, $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 = $this->get('event_dispatcher'); 

      $dispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_SUCCESS, $event); 

      $userManager->updateUser($user); 

      if (null === $response = $event->getResponse()) { 

       //$url = $this->generateUrl('fos_user_profile_show'); 

       $url = $this->generateUrl('edit_profile'); 
       $response = new RedirectResponse($url); 

      } 
      $dispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_COMPLETED, new FilterUserResponseEvent($user, $request, $response)); 

      $this->get('session')->getFlashBag()->add(
              'notice', 
              'Profile Updated!' 
       ); 

      return $response; 
     } 


     return $this->render('dog/edit_profile.html.twig',array('form'=>$form->createView())); 
    } 

UserType.php

<?php 

    namespace AppBundle\Form; 

    use Symfony\Component\Form\AbstractType; 
    use Symfony\Component\Form\FormBuilderInterface; 
    use Symfony\Component\OptionsResolver\OptionsResolverInterface; 
    use Vich\UploaderBundle\Form\Type\VichImageType; 
    use Symfony\Component\HttpFoundation\File\File; 
    use Symfony\Component\Form\Extension\Core\Type\FileType; 


    class UserType extends AbstractType{ 

     public function buildForm(FormBuilderInterface $builder, array $options){ 

      $builder->add('username') 
        ->add('name') 
        ->add('surname') 
        ->add('email') 
        ->add('telephone') 
        ->add('imageFile', FileType::Class); 


     } 

     public function setDefaultOptions(OptionsResolverInterface $resolver){ 

      $resolver->setDefaults(array('data_class' => 'AppBundle\Entity\User')); 
     } 

     public function getName() { 

      return 'appBundle_user'; 
     }`enter code here` 
    } 

edit_profile.html.twig

<header> 
    <div class="container"> 
    <div class="row"> 
     <div class="col-lg-12"> 
      <h2 class="name">Edit Profile</h2> 
      <div class="row" > 
      <div class="col-lg-12"> 

       <div class="input_width" > 

        {% for flashMessage in app.session.flashbag.get('notice') %} 

        <div class="alert alert-success alert"> 
         {{ flashMessage }} 
        </div> 

        {% endfor %} 

        {{form_start(form, {'attr': {'class': 'form-horizontal'}})}} 
        {{ form_errors(form) }} 

         <div class="form-group"> 
        {{ form_label(form.username) }} 
        {{ form_errors(form.username) }} 
        {{ form_widget(form.username , {'attr': {'class': 'form-control'}}) }} 
        </div> 

         <div class="form-group"> 
        {{ form_label(form.email) }} 
        {{ form_errors(form.email) }} 
        {{ form_widget(form.email , {'attr': {'class': 'form-control'}}) }} 
        </div> 



        <div class="form-group"> 
        {{ form_label(form.name) }} 
        {{ form_errors(form.name) }} 
        {{ form_widget(form.name , {'attr': {'class': 'form-control'}}) }} 
        </div> 

        <div class="form-group"> 

        {{ form_label(form.surname) }} 
        {{ form_errors(form.surname) }} 
        {{ form_widget(form.surname, {'attr': {'class': 'form-control'}}) }} 
        </div> 

        <div class="form-group"> 

        {{ form_label(form.telephone) }} 
        {{ form_errors(form.telephone) }} 
        {{ form_widget(form.telephone, {'attr': {'class': 'form-control'}}) }} 
        </div> 

        <div class="form-group"> 

        {{ form_label(form.imageFile) }} 
        {{ form_errors(form.imageFile) }} 
        {{ form_widget(form.imageFile) }} 
        </div> 

        <input type="submit" value="Submit" class="btn btn-default bluInput " /> 
        {{form_end(form)}} 

       </div> 
      </div> 
      </div> 
     </div> 
    </div> 
    </div> 
</header> 

基本上我不得不創建一個表格類型鏈接到我的進口然後,只要Fos用戶捆綁用戶管理器,就可以在我的控制器中使用它。