在Symfony2中获取登录信息

时间:2014-05-07 16:42:39

标签: php symfony

如何在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,
        ));
    }
}

如何在$id变量$entity = em->getRepository('SifoAdminBundle:MstPelajar')->find($id);以正确的方式获取另一个页面中的show(已经过身份验证),如下例所示:

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 个答案:

答案 0 :(得分: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未在您的代码中初始化。