[英]Symfony 3 - FosUserBundle - Redirect to the current page after registration
我有一个注册模式窗口,我想在成功注册后将用户重定向到同一页面上。
为此,我使用包含Request
对象的新关联控制器覆盖fos_user_registration_confirmed
路由。
在表单操作字段中,我尝试添加一个包含当前路径的参数current_path
,然后使用$current_path = $request->attributes->get('current_path');
进行检索$current_path = $request->attributes->get('current_path');
,但始终为NULL。
我尝试使用表单隐藏参数尝试相同的操作,然后使用$current_path = $request->request->get('current_path');
检索它$current_path = $request->request->get('current_path');
,但始终为NULL。
表单中的当前路径是正确的,但是似乎无法在控制器中检索到它。
public function registrationConfirmedAction(Request $request)
{
// POST: doesn't work
$current_path = $request->request->get('current_path');
// GET: doesn't work
if($current_path == NULL)
$current_path = $request->attributes->get('current_path');
if($current_path != NULL)
return new RedirectResponse($current_path);
return new RedirectResponse($this->generateUrl('pp_home_homepage'));
}
编辑 :我如何获得当前路径
current_path在我的register_content.html.twig模板中:
{% set current_path = app.request.get('current_path') %}
我在代码中的两个位置添加了它:
{{ form_start(form, {
'method': 'post',
'action': path('fos_user_registration_register') ~ '?current_path=' ~ current_path,
'attr': {
'class': 'fos_user_registration_register',
'novalidate': 'novalidate',
}
}) }}
和这里:
<input type="hidden" name="current_path" value="{{ current_path }}">
该属性在我的基本模板中生成:
{% set current_path = path(app.request.attributes.get('_route'), app.request.attributes.get('_route_params')) %}
然后将其发送到我的控制器:
{{ render(controller(
'PPUserBundle:Registration:register',
{'current_path': current_path}
)) }}
编辑 :使用监听器
我发现最好创建一个实现EventSubscriberInterface
的侦听器:
public static function getSubscribedEvents()
{
return array(
FOSUserEvents::REGISTRATION_SUCCESS => 'onRegistrationSuccess'
);
}
public function onRegistrationSuccess(FormEvent $event)
{
$current_path = $event->getRequest()->request->get('current_path');
$response = new RedirectResponse($current_path . '?registration_confirmed=true');
$event->setResponse($response);
}
然后,不要忘记修改app/config/services.yml
:
pp_user.registration_success:
class: PP\UserBundle\EventListener\RegistrationSuccess
autowire: true
tags:
- { name: kernel.event_subscriber }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.