如何更改默认目标路径的行为

编辑该页面

警告:你浏览的文档欧宝官网下载appob娱乐下载Symfony 2.7,不再维护。

这个页面的更新版本Symfob娱乐下载ony 6.2(当前的稳定版本)。

如何更改默认目标路径的行为

默认情况下,安全组件的信息保留在一个会话变量命名的最后请求URI_security.main.target_path(与主要防火墙的名称、定义security.yml)。成功登录后,用户被重定向到这个路径,来帮助他们继续从最后一个已知的页面访问。

在某些情况下,这是不理想。例如,当最后一个请求URI是一个XMLHttpRequest返回HTML或部分HTML响应,用户被重定向回浏览器不能呈现一个页面。

为了解决这种行为,你就只需要扩展ExceptionListener类并覆盖默认的方法命名setTargetPath ():

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/ / src / AppBundle /安全/防火墙/ ExceptionListener.php名称空间AppBundle\安全\防火墙;使用ob娱乐下载\组件\HttpFoundation\请求;使用ob娱乐下载\组件\安全\Http\防火墙\ExceptionListener作为BaseExceptionListener;ExceptionListener扩展BaseExceptionListener{受保护的函数setTargetPath(请求美元请求){/ /不保存XHR请求的目标路径/ /您可以添加任何你想要的更多的逻辑/ /注意,non-GET请求已经被忽略如果(美元请求- >isXmlHttpRequest ()) {返回;}::setTargetPath (美元请求);}}

通过阻止setTargetPath ()从被称为父,安全组件不会保留请求URI。添加尽可能多或尽可能少的逻辑要求你的场景!

接下来,创建ExceptionListenerPass替换默认的定义ExceptionListener您刚才创建的一个。确保在安全配置中使用防火墙的名称:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/ / src / AppBundle DependencyInjection /编译器/ ExceptionListenerPass.php名称空间AppBundle\DependencyInjection\编译器;使用ob娱乐下载\组件\DependencyInjection\编译器\CompilerPassInterface;使用ob娱乐下载\组件\DependencyInjection\ContainerBuilder;使用AppBundle\安全\防火墙\ExceptionListener;ExceptionListenerPass实现了CompilerPassInterface{公共函数过程(ContainerBuilder美元容器){/ /使用的名称后缀如防火墙。“secured_area”美元定义=美元容器- >getDefinition (“security.exception_listener.secured_area”);美元定义- >setClass (ExceptionListener::类);}}

最后,添加一个编译器传递给绑在一起:

1 2 3 4 5 6 7 8 9 10 11 12
/ / src / AppBundle / AppBundle.php名称空间AppBundle;使用AppBundle\DependencyInjection\编译器\ExceptionListenerPass;AppBundle扩展{公共函数构建(ContainerBuilder美元容器){美元容器- >addCompilerPass (ExceptionListenerPass ());}}
这项工作,包括代码示例,许可下Creative Commons冲锋队3.0许可证。