没有路径的Symfony 2路由,有可能吗?

时间:2014-05-30 13:49:36

标签: php security symfony routes

可能这是一个简单的问题,但我仍然是业余爱好者。

如何创建没有"模式的路线"属性?

我是指与无法访问的操作相对应的路线?

如果我尝试类似的话,那就是yaml:

security_loginfailure:
  requirements:
      _controller: SecurityBundle:Security:loginFailure

Symfony告诉我,我的路线必须有一个模式。所以我必须添加类似

的内容
pattern: /loginfailure

但显然浏览器可以请求/ loginfailure。

我应该将控制器配置为服务吗?或者是什么?

2 个答案:

答案 0 :(得分:1)

你不能没有模式的路线。如果我理解正确,您可能想要使用转发:

http://symfony.com/doc/current/book/controller.html#forwarding

答案 1 :(得分:0)

好的,确实没有任何意义。我只想以我的方式处理登录失败,我唯一知道的选项是security yml中的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));
    }
}

这可能看起来完全相同,但实际上它允许您自定义auth故障过程。