Php Symfony 2条没有路径的路线,有可能吗?
也许这是个简单的问题,但我还是个业余爱好者 如何创建没有“模式”属性的路由 我指的是一条与无法访问的操作相对应的路径 在yaml中,如果我尝试以下操作:Php Symfony 2条没有路径的路线,有可能吗?,php,security,symfony,routes,Php,Security,Symfony,Routes,也许这是个简单的问题,但我还是个业余爱好者 如何创建没有“模式”属性的路由 我指的是一条与无法访问的操作相对应的路径 在yaml中,如果我尝试以下操作: security_loginfailure: requirements: _controller: SecurityBundle:Security:loginFailure Symfony告诉我,我的路线必须有一个模式。因此,我必须添加一些类似的内容 pattern: /loginfailure 但很明显,浏览器可能会请求/
security_loginfailure:
requirements:
_controller: SecurityBundle:Security:loginFailure
Symfony告诉我,我的路线必须有一个模式。因此,我必须添加一些类似的内容
pattern: /loginfailure
但很明显,浏览器可能会请求/登录失败
我应该将控制器配置为服务吗?或者什么?没有图案就不能走路线。如果我理解正确,您可能希望使用转发:
好的,所以它确实没有任何意义。我只是想用我自己的方式处理登录失败,我知道的唯一选项是security yml中的failure_path选项,当然,它只接受路径(或路由) 我要做的是覆盖防火墙配置中的默认symfony failure\u处理程序 如果有人感兴趣,我会把这个放在那里,以备将来参考 而不是
failure_path: #something
使用
然后定义您的服务
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));
}
}
这看起来可能完全一样,但实际上它允许您深入自定义身份验证失败过程。似乎很有趣,我可以尝试一下。为什么您需要这样的路径?路由是指将url与特定控制器的特定操作相匹配,因此没有url的路由毫无意义。在这种情况下,我想使用security.yml中的“failure_path:”选项,该选项需要路径或路由。但我不想让这条路线有一个可从浏览器访问的路径(我知道读这篇文章可能没有意义)。
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));
}
}