可能这是一个简单的问题,但我仍然是一个业余爱好者。
如何创建一个不带pattern属性的路由?
我的意思是与不可访问的操作相对应的路由?
在yaml中,如果我尝试这样做:
security_loginfailure:
requirements:
_controller: SecurityBundle:Security:loginFailure
Symfony告诉我我的路由必须有一个模式。所以我要加上
pattern: /loginfailure
但是很明显浏览器可以请求/logfailure
是否需要将控制器配置为服务?还是别的什么?
你不能没有模式的路由。如果我理解正确的话,您可能希望使用转发:
http://symfony.com/doc/current/book/controller.html转发
好吧,这是真的,它没有任何意义。我只是想以我的方式处理登录失败,我知道的唯一选项是securityyml中的failure_path选项,当然,它只接受路径(或路由)。
我要做的是覆盖默认的symfony failure_handler在防火墙配置
如果有人感兴趣,我把它放在那里供将来参考。
代替
failure_path: #something
使用failure_handler: failure_service
然后定义你的服务
failure_service:
class: failure_class
argumets:
- @router
- login_route #or another route
#- other arguments like options
故障类
use Symfony'Component'Routing'RouterInterface;
use Symfony'Component'HttpFoundation'Request;
use Symfony'Component'Security'Http'Authentication'DefaultAuthenticationFailureHandler;
use Symfony'Component'Security'Core'Exception'AuthenticationException;
use Symfony'Component'HttpFoundation'RedirectResponse;
class AuthenticationFailure extends DefaultAuthenticationFailureHandler
{
protected $router;
protected $route;
protected $options = array();
public function __construct(RouterInterface $router, $route, array $options)
{
$this->router = $router;
$this->route = $route;
$this->options = $options;
}
public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
{
//finally we can do something
return new RedirectResponse($this->router->generate($this->route));
}
}
这看起来是一样的,但实际上它允许您深入定制验证失败过程。