这是我的问题,我正在使用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(backend)部分的路由:
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-&gt; getUser()),当前用户数据显示完好,但在提交表单后,相同的var_dump为null并重定向到登录形式。
我不知道我是否遗漏了某些内容或做错了,因为我已经对Symfony 2.8中的另一个项目做了同样的事情,它完美无缺地工作。
我希望我已经为我的问题提供了最大的信息。
感谢您的帮助。
答案 0 :(得分:0)
扩展基本操作(登录),并使用redirect
添加自定义逻辑;
见Link:
使用301返回代码重定向到客户端:
$this->redirect(
$this->generateUrl(
'person_in_need_view',
array('id' => $id)
)
);
这在内部重定向:
$this->forward(
'person_in_need_view',
array('id' => $id)
)
答案 1 :(得分:0)
解决方案是将表单操作从fos_user_profile_edit
更改为admin_profile_edit
,加载编辑表单的路由为后端用户 admin_profile_edit
,但是我忘了在我的表单操作中更改它,这就是为什么在提交用户后不再重新调整用户的原因,因为它被重定向到应该在前端部分工作的fos_user_profile_edit
editAction