2014-05-07 50 views
1

Hollo there,如何在Symfony2中實際登錄信息?在正常的PHP,我存儲在會話像ID和用戶名一些變量和用於查詢等在Symfony2中獲取登錄信息

如果我有實體這樣

<?php 

namespace Sifo\AdminBundle\Entity; 
use Doctrine\ORM\Mapping as ORM; 
use Symfony\Component\Security\Core\User\UserInterface; 

/** 
* MstPelajar 
*/ 
class MstPelajar implements UserInterface, \Serializable 
{ 
    /** 
    * @var integer 
    */ 
    private $id; 

    * @var string 
    */ 
    private $nis; 


    /** 
    * @var string 
    */ 
    private $password; 

    /** 
    * @var string 
    */ 
    private $salt; 

    /** 
    * @var boolean 
    */ 
    private $aktif; 

    /** 
    * @var \DateTime 
    */ 
    private $timestamp; 

    /** 
    * @var string 
    */ 
    private $operator; 

    private $username; 

    /** 
    * Set id 
    * 
    * @param integer $id 
    * @return MstPelajar 
    */ 
    public function setId($id) 
    { 
     $this->id = $id; 

     return $this; 
    } 

    /** 
    * Get id 
    * 
    * @return integer 
    */ 
    public function getId() 
    { 
     return $this->id; 
    } 


    /** 
    * Set nis 
    * 
    * @param string $nis 
    * @return MstPelajar 
    */ 
    public function setNis($nis) 
    { 
     $this->nis = $nis; 

     return $this; 
    } 

    /** 
    * Get nis 
    * 
    * @return string 
    */ 
    public function getNis() 
    { 
     return $this->nis; 
    } 

    /** 
    * Set password 
    * 
    * @param string $password 
    * @return MstPelajar 
    */ 
    public function setPassword($password) 
    { 
     $this->password = $password; 

     return $this; 
    } 

    /** 
    * Get password 
    * 
    * @return string 
    */ 
    public function getPassword() 
    { 
     return $this->password; 
    } 

    /** 
    * Set salt 
    * 
    * @param string $salt 
    * @return MstPelajar 
    */ 
    public function setSalt($salt) 
    { 
     $this->salt = $salt; 

     return $this; 
    } 

    /** 
    * Get salt 
    * 
    * @return string 
    */ 
    public function getSalt() 
    { 
     return $this->salt; 
    } 

    public function __construct() 
    { 
     $this->aktif = true; 
     // may not be needed, see section on salt below 
     // $this->salt = md5(uniqid(null, true)); 
    } 

    public function getUsername() 
    { 
     return $this->nis; 
    } 

    public function getRoles() 
    { 
     return array('ROLE_USER'); 
    } 

    public function eraseCredentials() 
    { 
    } 

    public function serialize() 
    { 
     return serialize(array(
      $this->id, 
      $this->nis, 
      $this->password, 
      // see section on salt below 
      // $this->salt, 
     )); 
    } 

    public function unserialize($serialized) 
    { 
     list (
      $this->id, 
      $this->nis, 
      $this->password, 
      // see section on salt below 
      // $this->salt 
     ) = unserialize($serialized); 
    } 
} 

和DefaultController這樣的:

<?php 

namespace Sifo\AdminBundle\Controller; 

use Symfony\Component\HttpFoundation\Request; 
use Symfony\Bundle\FrameworkBundle\Controller\Controller; 
use Symfony\Component\Security\Core\SecurityContext; 
use Symfony\Component\Security\Core\Exception\AccessDeniedException; 

use Sifo\AdminBundle\Form\DefaultType; 

class DefaultController extends Controller 
{ 

public function indexAction() 
    { 
     return $this->render('SifoAdminBundle:Default:index.html.twig'); 
    } 

public function loginAction() 
    { 
     $request = $this->getRequest(); 
     $session = $request->getSession(); 

     // get the login error if there is one 
     if ($request->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) { 
      $error = $request->attributes->get(SecurityContext::AUTHENTICATION_ERROR); 
     } else { 
      $error = $session->get(SecurityContext::AUTHENTICATION_ERROR); 
      $session->remove(SecurityContext::AUTHENTICATION_ERROR); 
     } 

     return $this->render('SifoAdminBundle:Default:login.html.twig', array(
      // last username entered by the user 
      'last_username' => $session->get(SecurityContext::LAST_USERNAME), 
      'error'   => $error, 
     )); 
    } 
} 

如何我得到$id$entity = em->getRepository('SifoAdminBundle:MstPelajar')->find($id);變量適當的方式顯示在另一個頁面(已驗證已經),如下例:

public function indexAction(){ 
    $em = $this->getDoctrine()->getManager(); 
    $entity = $em->getRepository('SifoAdminBundle:MstPelajar')->find($id); 
    if (!$entity) { 
     throw $this->createNotFoundException('Unable to find MstPelajar entity.'); 
    } 

    return $this->render('SifoUserBundle:Profil:index.html.twig', array(
     'entity'  => $entity, 
     'delete_form' => $deleteForm->createView(),  )); 
    } 

謝謝。

回答

1

您可以使用

$user = $this->getUser(); // or $user = $this->get('security.context')->getToken()->getUser(); 

控制器訪問用戶信息,並通過這種方式,可以避免讓存儲庫,並再次找到用戶,因爲這些信息已經在會話。

您的代碼將是這樣的:

public function indexAction(){ 
    $user = $this->getUser(); 

    return $this->render('SifoUserBundle:Profil:index.html.twig', array(
     'entity'  => $user, 
     'delete_form' => $deleteForm->createView(),  )); 
} 

注意$ deleteForm是不是在你的代碼初始化。

+0

非常感謝,它的工作。但我好奇哪種方式是最好的?或者兩者都是相同的功能? –

+1

兩者都是一樣的。調用getUser()只是在答案中寫入較長版本的代理方法。 – tomazahlin

相關問題