【问题标题】:Laravel: Can I use Redirect::intended in filters other than authentication filter?Laravel:我可以在身份验证过滤器以外的过滤器中使用 Redirect::intended 吗?
【发布时间】:2014-06-28 14:00:04
【问题描述】:

Laravel Docs 上关于 Authenticating Users 的部分说:

Redirect::intended 函数会将用户重定向到他们在被身份验证过滤器捕获之前尝试访问的 URL。

我可以将它与其他过滤器一起使用吗?

Route::filter('auth_role_set', function()
{
    if (! Session::has('auth_role')) return Redirect::to('roles');
});

在控制器中:

public function roles()
{
    $user = Auth::user();
    $roles = explode(',', $user->roles);

    if (count($roles) == 1)
    {
        Session::put('auth_role', $user->roles);
        return Redirect::intended('/');
    }
}

【问题讨论】:

  • 试试看会发生什么!最坏的情况是它没有做你想做的事。不过,我不确定你到底需要它做什么?
  • 它对我不起作用。所以,我想知道是否有人知道它是否有效或者我犯了任何错误。
  • 它不起作用,因为您要添加额外的重定向。第一次重定向发生后,会话被清除,第二个请求在url.intended 上没有任何内容。您可以在第一次重定向之前将数据保留在会话调用Session::keep('url.intended') 中。
  • @Raphael_ Laravel 什么时候在会话中设置url.intended?是否仅在被身份验证过滤器捕获之前发生。
  • 哦,我的错。我的第一条评论有点错误。当你调用 Redirect::guest() 时,Laravel 在会话中设置 url.intended。尝试在过滤器上使用它,而不是 Redirect::to()

标签: php laravel


【解决方案1】:

正如 Raphael_ 已经提到的,您需要这些步骤来使用Redirect::intended()

  • 在您的 filters.php 中使用 Redirect::guest('/role') 将用户重定向到您的角色设置页面
  • 在您的控制器中使用Redirect::intended('/fallback-url')

最终用户将到达第一页,/role 重定向开始的地方。

【讨论】:

    最近更新 更多