我已经尝试了很多方法来解决这个问题,但它仍然无法正常工作。我想要的是,在用户成功注册后,他正在重定向到想要的页面。
所以,我有一个UserBundle:
namespace MDB\UserBundle\EventListener;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use FOS\UserBundle\FOSUserEvents;
use Symfony\Component\HttpFoundation\RedirectResponse;
class RegistrationConfirmListener implements EventSubscriberInterface {
private $router;
public function __construct(UrlGeneratorInterface $router) {
$this->router = $router;
}
/**
* {@inheritDoc}
*/
public static function getSubscribedEvents() {
return array(
FOSUserEvents::REGISTRATION_CONFIRM => 'onRegistrationConfirm'
);
}
public function onRegistrationConfirm(GetResponseUserEvent $event) {
$url = $this->router->generate('mdb_platform_homepage');
$event->setResponse(new RedirectResponse($url));
}
}
?>
服务:
mdb_user.registration.form.type:
class: MDB\UserBundle\Form\Type\RegistrationFormType
tags:
- { name: form.type, alias: mdb_user_registration }
mdb.user.validator.contains_user:
class: MDB\UserBundle\Validator\Constraints\ContainsUserValidator
arguments: [ "@doctrine.orm.entity_manager" ]
tags:
- { name: validator.constraint_validator, alias: contains_user }
mdb_user.registration_complet:
class: MDB\UserBundle\EventListener\RegistrationConfirmListener
arguments: ["@router"]
tags:
- { name: kernel.event_subscriber }
我已经尝试了很多其他配置并检查其他论坛,但似乎重定向永远不会起作用。
有人有想法吗? 提前谢谢
答案 0 :(得分:1)
我解决了这个问题:
在app / config / routing中:
添加此
fos_user_registration_confirmed:
pattern: /registration/confirmed
defaults: { _controller: MDBUserBundle:User:confirmed }
requirements:
_method: GET
并在目标控制器中执行您想要的操作:
public function confirmedAction()
{
return $this->render('MDBPlatformBundle:Default:index.html.twig'
);
}
可以为类似情况提供帮助的良好链接:How to customize FOS UserBundle URLs
希望这会有所帮助
答案 1 :(得分:0)
有一些方法可以做到这一点:
更改默认页面
# app/config/security.yml
security:
firewalls:
main:
form_login:
# ...
always_use_default_target_path: true
use_referer: true
default_target_path: default_security_target
从表单
中控制重定向网址{# src/Acme/SecurityBundle/Resources/views/Security/login.html.twig #}
{% if error %}
<div>{{ error.message }}</div>
{% endif %}
<form action="{{ path('login_check') }}" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="_username" value="{{ last_username }}" />
<label for="password">Password:</label>
<input type="password" id="password" name="_password" />
<input type="hidden" name="_target_path" value="account" />
<input type="submit" name="login" />
</form>
并在您的安全中:
# app/config/security.yml
security:
firewalls:
main:
form_login:
target_path_parameter: redirect_url
阅读详情:http://symfony.com/doc/current/cookbook/security/form_login.html
答案 2 :(得分:-1)
如果您只想重定向,可以更简化LeZelkins接受的答案。使用FrameworkBundle RedirectController:
# routing.yml
fos_user_registration_confirmed:
path: /registration/confirmed
defaults:
_controller: FrameworkBundle:Redirect:urlRedirect
path: /admin/dashboard