简体   繁体   English

Laravel获取重定向URL

[英]Laravel get redirect URL

I have problems with getting Laravel URL. 我在获取Laravel URL时遇到问题。

I have one page that if user is not logged in, it redirect user to wrong url and it's giving an error. 我有一个页面,如果用户未登录,则会将用户重定向到错误的url,并且显示错误。

I'm using redirect()->intended($this->redirectPath()); 我正在使用redirect()->intended($this->redirectPath()); to get back user where he was on page after login, so somehow I have to write code that will check what is the redirect URL, and if it's the URL that will give an error, 为了让用户在登录后回到页面上的位置,所以我不得不以某种方式编写代码来检查什么是重定向URL,以及是否是会产生错误的URL,

I know what is the URL, it must redirect user to another URL that I need to set. 我知道什么是URL,它必须将用户重定向到我需要设置的另一个URL。

I tried different methods taking $this->redirectPath() == 'here goes the url' 我尝试了采用$this->redirectPath() == 'here goes the url'

and like redirect()->intended($this->redirectPath()) == 'again url here' 就像redirect()->intended($this->redirectPath()) == 'again url here'

But nothing works. 但是没有任何效果。

Try this: 尝试这个:

return Redirect::intended('here goes the url');

Or 要么

return Redirect::intended();

intended() checks if the session index url.intended exists and redirects to it by default or else redirect to $default='/' which can be overwritten. purposes intended()检查会话索引url.intended是否存在并默认重定向到它,否则重定向到$ default ='/',它可以被覆盖。

You have to understand what redirect()->intended() actually does. 您必须了解redirect()->intended()实际作用。

It looks for a session variable named url.intended like session()->pull('url.intended') (note that pull() will remove the variable after this call, which is handy because you usually only need this variable once). 它会寻找一个名为url.intended的会话变量,就像session()->pull('url.intended') (请注意, pull()将在此调用后删除该变量,这很方便,因为您通常只需要一次使用此变量) 。

This variable is currently only set when you call redirect()->guest('/yourpath') (it will set your current URL as the url.intended ), unless you manually set it in your own logic using session()->put('url.intended') . 当前仅在调用redirect()->guest('/yourpath')时设置此变量(它将您的当前URL设置为url.intended ),除非您使用session()->put('url.intended')以自己的逻辑手动设置它session()->put('url.intended')

So the most obvious way to use this is to use redirect()->guest('/login') , and then after the user has successfully logged in you may use redirect()->intended() (without parameter) to redirect the user back to the page that originally sent him to the login page. 因此,最明显的使用方法是使用redirect()->guest('/login') ,然后在用户成功登录后,您可以使用redirect()->intended() (无参数)进行重定向用户返回到最初将他发送到登录页面的页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM