Symfony 3 FOSUserBundle 在提交编辑配置文件后重定向到登录
Symfony 3 FOSUserBundle redirect to login after submiting edit profile
这是我的问题,我正在使用 FOSUserBundle 和 Symfony 3,我创建了一个继承 FOSUserBundle 的 UserBundle,因此我可以在需要时覆盖它的某些部分,为了项目的需要,我创建了两个防火墙,一个用于后端和前端部分的第二个,一切正常,直到我尝试编辑当前登录用户的个人资料(用户名和电子邮件),当我在加载编辑时使用 var_dump($this->getUser())表单我获取了当前用户的所有数据,但在提交后我得到 var_dump($this->getUser()) = null 并被重定向到登录表单。
这是security.yml
security:
encoders:
FOS\UserBundle\Model\UserInterface: bcrypt
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: ROLE_ADMIN
providers:
fos_userbundle:
id: fos_user.user_provider.username
firewalls:
admin:
pattern: /admin(.*)
form_login:
provider: fos_userbundle
login_path: /admin/login
check_path: /admin/login_check
csrf_token_generator: security.csrf.token_manager
default_target_path: /admin/
always_use_default_target_path: true
logout:
path: /admin/logout
target: /admin/login
anonymous: true
main:
pattern: ^/
form_login:
provider: fos_userbundle
login_path: /login
check_path: /login_check
csrf_token_generator: security.csrf.token_manager
default_target_path: /
always_use_default_target_path: true
# if you are using Symfony < 2.8, use the following config instead:
# csrf_provider: form.csrf_provider
logout:
path: /logout
target: /login
anonymous: true
access_control:
- { path: ^/admin/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/admin/logout$, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/admin/login_check$, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/admin/, role: ROLE_ADMIN }
routing.yml 中管理(后端)部分的路由:
admin_login:
path: /admin/login
defaults: { _controller: FOSUserBundle:Security:login }
admin_check:
path: /admin/login_check
defaults: { _controller: FOSUserBundle:Security:check }
admin_logout:
path: /admin/logout
defaults: { _controller: FOSUserBundle:Security:logout }
admin_profile_show:
path: /admin/profile/show
defaults: { _controller: FOSUserBundle:Profile:show }
admin_profile_edit:
path: /admin/profile/edit
defaults: { _controller: FOSUserBundle:Profile:edit }
admin_profile_change_password:
path: /admin/profile/changePassword
defaults: { _controller: FOSUserBundle:ChangePassword:changePassword }
这是我的 ProfileController.php 中的 editAction,可以通过上面路由文件中的路由 admin_profile_edit
访问,该操作覆盖 FOSUserBundle
中的
// UserBundle/Controller/ProfileController.php
public function editAction(Request $request)
{
$user = $this->getUser();
if (!is_object($user) || !$user instanceof UserInterface) {
throw new AccessDeniedException('This user does not have access to this section.');
}
/** @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */
$dispatcher = $this->get('event_dispatcher');
$event = new GetResponseUserEvent($user, $request);
$dispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_INITIALIZE, $event);
if (null !== $event->getResponse()) {
return $event->getResponse();
}
/** @var $formFactory \FOS\UserBundle\Form\Factory\FactoryInterface */
$formFactory = $this->get('fos_user.profile.form.factory');
$form = $formFactory->createForm();
$form->setData($user);
$form->handleRequest($request);
if ($form->isValid()) {
/** @var $userManager \FOS\UserBundle\Model\UserManagerInterface */
$userManager = $this->get('fos_user.user_manager');
$event = new FormEvent($form, $request);
$dispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_SUCCESS, $event);
$userManager->updateUser($user);
if (null === $response = $event->getResponse()) {
$url = $this->generateUrl('fos_user_profile_show');
$response = new RedirectResponse($url);
}
$dispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_COMPLETED, new FilterUserResponseEvent($user, $request, $response));
return $response;
}
$requestAttributes = $this->container->get('request_stack')->getCurrentrequest()->attributes;
if ($requestAttributes->get('_route') == 'admin_profile_edit') {
$template = sprintf('UserBundle:Profile:user_admin_edit.html.twig');
} else {
$template = sprintf('FOSUserBundle:Profile:edit.html.twig');
}
return $this->render($template, array(
'form' => $form->createView()
));
}
现在加载编辑表单时一切正常,如果我使用 var_dump($this->getUser()) 当前用户数据显示完美,但提交表单后相同 var_dump 为空,我被重定向到登录表单。
我不知道我是否遗漏了某些东西或做错了,因为我已经在 Symfony 2.8 中对另一个项目做了同样的事情并且它运行完美没有任何问题。
我希望我已经为我的问题提供了最多的信息。
感谢您的帮助。
扩展基本操作(登录),并使用redirect
添加自定义逻辑;
见 Link:
这使用 301 return 代码重定向到客户端:
$this->redirect(
$this->generateUrl(
'person_in_need_view',
array('id' => $id)
)
);
这在内部重定向:
$this->forward(
'person_in_need_view',
array('id' => $id)
)
解决方案是将表单操作从 fos_user_profile_edit
更改为 admin_profile_edit
,为 后端用户加载编辑表单的路径是 admin_profile_edit
,但我忘记在我的 表单操作 中更改它,这就是为什么在提交后用户不再重新定位,因为它被重定向到 fos_user_profile_edit
editAction应该在前端工作。
这是我的问题,我正在使用 FOSUserBundle 和 Symfony 3,我创建了一个继承 FOSUserBundle 的 UserBundle,因此我可以在需要时覆盖它的某些部分,为了项目的需要,我创建了两个防火墙,一个用于后端和前端部分的第二个,一切正常,直到我尝试编辑当前登录用户的个人资料(用户名和电子邮件),当我在加载编辑时使用 var_dump($this->getUser())表单我获取了当前用户的所有数据,但在提交后我得到 var_dump($this->getUser()) = null 并被重定向到登录表单。
这是security.yml
security:
encoders:
FOS\UserBundle\Model\UserInterface: bcrypt
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: ROLE_ADMIN
providers:
fos_userbundle:
id: fos_user.user_provider.username
firewalls:
admin:
pattern: /admin(.*)
form_login:
provider: fos_userbundle
login_path: /admin/login
check_path: /admin/login_check
csrf_token_generator: security.csrf.token_manager
default_target_path: /admin/
always_use_default_target_path: true
logout:
path: /admin/logout
target: /admin/login
anonymous: true
main:
pattern: ^/
form_login:
provider: fos_userbundle
login_path: /login
check_path: /login_check
csrf_token_generator: security.csrf.token_manager
default_target_path: /
always_use_default_target_path: true
# if you are using Symfony < 2.8, use the following config instead:
# csrf_provider: form.csrf_provider
logout:
path: /logout
target: /login
anonymous: true
access_control:
- { path: ^/admin/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/admin/logout$, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/admin/login_check$, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/admin/, role: ROLE_ADMIN }
routing.yml 中管理(后端)部分的路由:
admin_login:
path: /admin/login
defaults: { _controller: FOSUserBundle:Security:login }
admin_check:
path: /admin/login_check
defaults: { _controller: FOSUserBundle:Security:check }
admin_logout:
path: /admin/logout
defaults: { _controller: FOSUserBundle:Security:logout }
admin_profile_show:
path: /admin/profile/show
defaults: { _controller: FOSUserBundle:Profile:show }
admin_profile_edit:
path: /admin/profile/edit
defaults: { _controller: FOSUserBundle:Profile:edit }
admin_profile_change_password:
path: /admin/profile/changePassword
defaults: { _controller: FOSUserBundle:ChangePassword:changePassword }
这是我的 ProfileController.php 中的 editAction,可以通过上面路由文件中的路由 admin_profile_edit
访问,该操作覆盖 FOSUserBundle
// UserBundle/Controller/ProfileController.php
public function editAction(Request $request)
{
$user = $this->getUser();
if (!is_object($user) || !$user instanceof UserInterface) {
throw new AccessDeniedException('This user does not have access to this section.');
}
/** @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */
$dispatcher = $this->get('event_dispatcher');
$event = new GetResponseUserEvent($user, $request);
$dispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_INITIALIZE, $event);
if (null !== $event->getResponse()) {
return $event->getResponse();
}
/** @var $formFactory \FOS\UserBundle\Form\Factory\FactoryInterface */
$formFactory = $this->get('fos_user.profile.form.factory');
$form = $formFactory->createForm();
$form->setData($user);
$form->handleRequest($request);
if ($form->isValid()) {
/** @var $userManager \FOS\UserBundle\Model\UserManagerInterface */
$userManager = $this->get('fos_user.user_manager');
$event = new FormEvent($form, $request);
$dispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_SUCCESS, $event);
$userManager->updateUser($user);
if (null === $response = $event->getResponse()) {
$url = $this->generateUrl('fos_user_profile_show');
$response = new RedirectResponse($url);
}
$dispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_COMPLETED, new FilterUserResponseEvent($user, $request, $response));
return $response;
}
$requestAttributes = $this->container->get('request_stack')->getCurrentrequest()->attributes;
if ($requestAttributes->get('_route') == 'admin_profile_edit') {
$template = sprintf('UserBundle:Profile:user_admin_edit.html.twig');
} else {
$template = sprintf('FOSUserBundle:Profile:edit.html.twig');
}
return $this->render($template, array(
'form' => $form->createView()
));
}
现在加载编辑表单时一切正常,如果我使用 var_dump($this->getUser()) 当前用户数据显示完美,但提交表单后相同 var_dump 为空,我被重定向到登录表单。
我不知道我是否遗漏了某些东西或做错了,因为我已经在 Symfony 2.8 中对另一个项目做了同样的事情并且它运行完美没有任何问题。
我希望我已经为我的问题提供了最多的信息。
感谢您的帮助。
扩展基本操作(登录),并使用redirect
添加自定义逻辑;
见 Link:
这使用 301 return 代码重定向到客户端:
$this->redirect(
$this->generateUrl(
'person_in_need_view',
array('id' => $id)
)
);
这在内部重定向:
$this->forward(
'person_in_need_view',
array('id' => $id)
)
解决方案是将表单操作从 fos_user_profile_edit
更改为 admin_profile_edit
,为 后端用户加载编辑表单的路径是 admin_profile_edit
,但我忘记在我的 表单操作 中更改它,这就是为什么在提交后用户不再重新定位,因为它被重定向到 fos_user_profile_edit
editAction应该在前端工作。