【问题标题】:Laravel get redirect URLLaravel 获取重定向地址
【发布时间】:2018-10-29 15:48:26
【问题描述】:

我在获取 Laravel 网址时遇到问题。

我有一个页面,如果用户没有登录,它会将用户重定向到错误的 url,它会给出一个错误。

我正在使用redirect()->intended($this->redirectPath()); 找回用户登录后他在页面上的位置,所以我必须以某种方式编写代码来检查什么是重定向 URL,如果它是会给出错误的 URL,

我知道 URL 是什么,它必须将用户重定向到我需要设置的另一个 URL。

我尝试了不同的方法 $this->redirectPath() == 'here goes the url'

喜欢 redirect()->intended($this->redirectPath()) == 'again url here'

但没有任何效果。

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    试试这个:

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

    或者

    return Redirect::intended();
    

    intended() 检查会话索引 url.intended 是否存在并默认重定向到它,否则重定向到可以覆盖的 $default='/'。

    【讨论】:

    • 对不起,我认为这不是我的问题的解决方案。如果我提供的 url 是实际的重定向 URL,我必须写它必须返回我设置的内容,但在 else 方法中,我必须默认设置 (redirect()->intended($this->redirectPath())
    • 我试过 redirect()->intended()->getTargetURL() ,它只给出 URL,如何获取路径?
    【解决方案2】:

    您必须了解redirect()->intended() 的实际作用。

    它会查找名为url.intended 的会话变量,例如session()->pull('url.intended')(注意pull() 将在此调用后删除该变量,这很方便,因为您通常只需要此变量一次)。

    此变量目前仅在您调用 redirect()->guest('/yourpath') 时设置(它将您当前的 URL 设置为 url.intended),除非您使用 session()->put('url.intended') 在您自己的逻辑中手动设置它。

    所以最明显的使用方法是使用redirect()->guest('/login'),然后在用户成功登录后,您可以使用redirect()->intended()(不带参数)将用户重定向回最初将他发送到的页面登录页面。

    【讨论】:

      猜你喜欢
      • 2013-03-19
      • 2012-03-31
      • 2017-12-12
      • 2014-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-09
      • 1970-01-01
      相关资源
      最近更新 更多