[英]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(后端)部分的路由:
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
訪問,該操作會覆蓋admin_profile_edit
中的路徑
// 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為null並重定向到登錄表單。
我不知道我是否遺漏了某些東西或做錯了,因為我已經在Symfony 2.8中的另一個項目做了同樣的事情,它完美無缺地工作。
我希望我已經為我的問題提供了最大的信息。
謝謝您的幫助。
擴展基本操作(登錄),並使用redirect
添加自定義邏輯;
見鏈接 :
這使用301返回代碼重定向到客戶端:
$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。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.