[英]Symfony2 $this->getUser() is empty on other controllers
SigninController 登录控制器
if ($request->getMethod() == 'POST') {
if ($form->isValid()) {
/*
* TO DO: should trigger the api endpoint. Redirection should be handled base
* on the api response.
*/
$request = $this->get("request");
if ($this->isAuthUser($request)) {
$token = new UsernamePasswordToken($signIn, null, 'secured_area', $signIn->getRoles());
$this->get("security.context")->setToken($token);
$request = $this->get("request");
$event = new InteractiveLoginEvent($request, $token);
$this->get("event_dispatcher")->dispatch("security.interactive_login", $event);
//$user = $this->get('security.context')->getToken();
return $this->redirect($this->generateUrl('main_accounts_myaccount'));
} else {
$error = new FormError(ErrorMessages::USER_NOT_AUTHENTICATED);
$form->addError($error);
}
}
}
return $this->render('MainAccountsBundle:Signin:index.html.twig',
array('form'=>$form->createView()));
}
MyAccountController MyAccountController
public function indexAction() {
$user = $this->get('security.context')->getToken();
return $this->render('MainAccountsBundle:MyAccount:index.html.twig');
}
When I use my sign in form it works fine and redirected me to MyAccounts page. 当我使用登录表单时,它可以正常工作,并将我重定向到MyAccounts页面。 When I check,
我检查的时候
{% if is_granted('IS_AUTHENTICATED_REMEMBERED') %}
{{ app.user.username }}
<a href="{{ path('main_accounts_signout') }}">Signout</a>
{% endif %}
on twig for MyAccount that also works. 在MyAccount的树枝上也可以使用。
Issue: But it doesn't print username. 问题:但是它不打印用户名。 When I use print_r($user) from MyAccountController that is empty.
当我从MyAccountController使用print_r($ user)时,它是空的。 When I print_r($token) that prints that shows user is empty.
当我打印print_r($ token)时,显示用户为空。
$user from SignInController SignInController中的$ user
Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken Object
(
[credentials:Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken:private] =>
[providerKey:Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken:private] => secured_area
[user:Symfony\Component\Security\Core\Authentication\Token\AbstractToken:private] => Main\Bundle\AccountsBundle\Entity\User Object
(
[email:Main\Bundle\AccountsBundle\Entity\User:private] => abc@xyz.com
[password:Main\Bundle\AccountsBundle\Entity\User:private] => operator123#46
[id:Main\Bundle\AccountsBundle\Entity\User:private] => 1212121212
[userId:Main\Bundle\AccountsBundle\Entity\User:private] =>
[name:Main\Bundle\AccountsBundle\Entity\User:private] =>
[username:Main\Bundle\AccountsBundle\Entity\User:private] => abc@xyz.com
)
[roles:Symfony\Component\Security\Core\Authentication\Token\AbstractToken:private] => Array
(
[0] => Symfony\Component\Security\Core\Role\Role Object
(
[role:Symfony\Component\Security\Core\Role\Role:private] => ROLE_USER
)
)
[authenticated:Symfony\Component\Security\Core\Authentication\Token\AbstractToken:private] => 1
[attributes:Symfony\Component\Security\Core\Authentication\Token\AbstractToken:private] => Array
(
)
)
$user from MyAccountController 来自MyAccountController的$ user
Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken Object
(
[credentials:Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken:private] =>
[providerKey:Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken:private] => secured_area
[user:Symfony\Component\Security\Core\Authentication\Token\AbstractToken:private] =>
[roles:Symfony\Component\Security\Core\Authentication\Token\AbstractToken:private] => Array
(
[0] => Symfony\Component\Security\Core\Role\Role Object
(
[role:Symfony\Component\Security\Core\Role\Role:private] => ROLE_USER
)
)
[authenticated:Symfony\Component\Security\Core\Authentication\Token\AbstractToken:private] => 1
[attributes:Symfony\Component\Security\Core\Authentication\Token\AbstractToken:private] => Array
(
)
)
tried a lot to figure out. 尝试了很多以找出答案。 was not able to find out.
无法找到。 Please help me on this.
请帮我。
security.yml 安全性
security:
providers:
in_memory:
memory: ~
access_control:
- { path: ^/(signin|signup)?$, roles: IS_AUTHENTICATED_ANONYMOUSLY }
#- { path: ^/, roles: IS_AUTHENTICATED_FULLY }
firewalls:
secured_area:
pattern: ^/
anonymous: ~
form_login:
login_path: /signin
check_path: _security_check
access_denied_url: signin
# logout:
# path: /signout
# #target: /
# invalidate_session: false
# delete_cookies:
# a: { path: null, domain: null }
# b: { path: null, domain: null }
# #handlers: [some.service.id, another.service.id]
# #success_handler: some.service.id
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
default:
anonymous: true
UsernamePasswordToken对象的第一个和第二个参数需要包含用户对象和用户密码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.