在我的应用程序中,我希望在登录后将用户重定向到个人资料,因此我在我的TWIG页面中使用名为_target_path
和值为/profile/{userid}
的隐藏输入(我已经试图制作一个IF语句来检查app.user.username
变量何时存在,但这不是正确的逻辑)
<input type="hidden" name="_target_path" value="/profile/{% if app.user.username is defined %}{{ app.user.username }}{% endif %}" />
当我渲染表格时,我自然没有按预期工作,我还没有在会话中设置用户名。
这是AuthController.php中的loginAction()
/**
* @Route("login/", name="login")
*/
public function loginAction(Request $request)
{
$authenticationUtils = $this->get('security.authentication_utils');
$error = $authenticationUtils->getLastAuthenticationError();
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('auth/login.html.twig', array(
'last_username' => $lastUsername,
'error' => $error,
));
}
我可以使用参数ID
访问个人资料页面,因此网址就像example.com/profile/1
,但没有ID导致404。
因此,我需要在登录后profile/{id}
从autenticationUtils
我确信我的逻辑不正确,但我无法在网络上找到任何可以解决我问题的内容。
这是我的security.yml
security:
providers:
user_db:
entity: { class: AppBundle\Entity\User, property: username }
encoders:
Symfony\Component\Security\Core\User\User:
algorithm: bcrypt
cost: 12
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
anonymous: ~
form_login:
login_path: login
check_path: login
logout: true
logout:
csrf_parameter: _csrf_token
csrf_token_generator: ~
csrf_token_id: logout
path: /logout
target: /
success_handler: ~
invalidate_session: true
delete_cookies:
name:
path: null
domain: null
handlers: []
admin:
pattern: ^/
provider: user_db
http_basic:
realm: 'Admin Area'
provider: in_memory
form_login: ~
access_control:
- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/admin, roles: ROLE_ADMIN }
- { path: ^/profilo, roles: [ROLE_USER, ROLE_ADMIN] }
答案 0 :(得分:1)
您需要更改DefaultAuthenticationSuccessHandler(假设您使用普通的symfony机制,而不使用任何捆绑包,例如FOSUserBundle)。
首先制作你自己的Handler(或部分,尤其是onAuthenticationSuccess):
namespace ...
use ...
class AuthenticationSuccessHandler extends DefaultAuthenticationSuccessHandler
{
/**
* {@inheritdoc}
*/
public function onAuthenticationSuccess(Request $request, TokenInterface $token)
{
**DO your logic here**
//or call parent if you want default behaviour
return parent::onAuthenticationSuccess($request, $token);
}
}
根据需要注入更多服务。
第二次覆盖DI中的默认服务:
security.authentication.success_handler:
class: AppBundle\Handler\AuthenticationSuccessHandler
arguments: ['@security.http_utils', {}]
tags:
- { name: 'monolog.logger', channel: 'security' }
答案 1 :(得分:0)
来自Symfony文档:Changing the default Page
首先,可以设置默认页面(即,如果会话中没有存储先前页面,则用户被重定向到的页面)。要将其设置为default_security_target
路由,请使用以下配置:
# app/config/security.yml
security:
# ...
firewalls:
main:
form_login:
# ...
default_target_path: default_security_target
现在,如果会话中未设置任何网址,则会将用户发送到default_security_target
路由。
您可以通过将always_use_default_target_path
选项设置为true来将用户始终重定向到默认页面,而不管他们先前请求的URL:
# app/config/security.yml
security:
# ...
firewalls:
main:
form_login:
# ...
always_use_default_target_path: true
答案 2 :(得分:-1)
登录后你应该在PHP中做这样的事情:
return $this->redirectToRoute('name_profile_route', { id : $this->getUser()->getId()});