如何从 URL 或 $request 对象获知路由?
阴谋:
在我的网站上实施 ACL 后,如果用户尝试访问未经授权的页面,他将被拒绝并显示一个登录页面。他登录后,我想将用户重定向到他之前被拒绝访问的上一个页面。
为此,我使用 $request ->; 存储请求参数。 getParams()
到会话变量上,该变量将用于再次生成 url。 这就是问题发生的地方,为了生成返回的网址,我需要路线的名称,但我不知道如何阅读。
我需要知道路由名称,以便我能够从会话中存储的数组重新生成 url,或者如果有更好的方法来解决这个问题,请提出建议。
PLOT:
After implementing ACL on my website, if a user tries to access unauthorised page he will be denied and shown a page to login. After he loggs in, I wanted to redirect the user to the previous page to which he was denied to earlier.
To do this, I store the request parameters using $request -> getParams()
, onto a session variable, which will be used to generate the url again. This is where the problem occurs, to generate the url back, I need the name of the route and that i dont know how to read.
I need to know the route name, so that I will be able to regenerate the url, from the array stored in session, or if there is a better way to solve this, please suggest.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么不只存储
$request->getRequestUri()
?这将为您提供浏览器中显示的 URL。Why not just store
$request->getRequestUri()
? This will give you the URL as it appears in the browser.不要试图为简单的问题想出复杂的解决方案。
您可以做到这一点,只需使用
$_SERVER['REQUEST_URI']
,这会得到与 @Phil 的答案相同的结果(纠正我,如果我遗漏了一些东西)。并且足以做你想做的事。Dont try to think of complex solutions for simple problem.
You can do this, with just using
$_SERVER['REQUEST_URI']
, this gives the same result as @Phil's answer (Correct me, If i am missing something). and is more than enough to do what you want.