代碼如下。我有一個loginAction方法,我想通過我的twig文件中的會話訪問用戶名。如何在symfony2中的應用程序中存儲會話並訪問它?
public function loginAction(Request $request)
{
$session= $this->getRequest()->getSession();
if($request->getMethod()=='POST')
{
$username=$request->get('username');
$password=$request->get('password');
$em= $this->getDoctrine()->getEntityManager();
$repository = $em->getRepository('MWANRegisterBundle:User');
$user= $repository->findOneBy(array('userName'=>$username, 'userPassword'=> $password));
if($user)
{
$login= new Session();
// Session is in the same class as getter setter functions
// for username and password
$login->setUsername($username);
$session->set('login', $login);
if($session->has('login'))
{
$login = $session->get('login');
$username = $login->getUsername();
// var_dump($username); exit();
// in show_home I redirect to twig file list.html.twig
return $this->redirect($this->generateUrl('show_home'));
}
}
else
{
echo("wrong username OR password");
exit();
}
}
return $this->render('MWANRegisterBundle:Default:login.html.twig');
}
---------------------這裏是註銷方法----------------- ---------
public function logoutAction(Request $request)
{
$session= $this->getRequest()->getSession();
$session->clear();
return $this->render('MWANRegisterBundle:Default:login.html.twig');
}
// listAction is action to render list.html.twig i want username of session here
public function listAction (Request $request)
{
$session= $this->getRequest()->getSession();
return $this->render('MWANRegisterBundle:Default:list.html.twig');
}
請指引我一步一步......
===============一個問題=== ================ $ plainPassword = $ this-> get('request') - > request-> get('password'); $ encoder = $ this-> container-> get('security.password_encoder'); $ encoded = $ encoder-> encodePassword($ entity,$ plainPassword); $ entity-> setPassword($ encoded); –
是什麼問題? – mgh
Gholami 當我在Security.yml中使用bcrypt。它在動態編碼後不會與存儲在數據庫中的密碼相匹配。 –