在Symfony2中获取登录信息


Get Login Information in Symfony2

如何在Symfony2中获取登录信息?在普通PHP中,我在会话中存储了一些变量,如ID和Username,用于查询等。

如果我有这样的实体

<?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,
        ));
    }
}

我如何以适当的方式在$entity = em->getRepository('SifoAdminBundle:MstPelajar')->find($id);变量中获得$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(),        ));
    }

您可以使用

从控制器访问用户信息
$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在你的代码中没有初始化。