【问题标题】:Laravel: Redirect different user roles differentlyLaravel:以不同方式重定向不同的用户角色
【发布时间】:2016-12-31 23:16:19
【问题描述】:

是否可以将具有不同用户角色的用户重定向到 laravel 5.1 中的不同页面?

我查看了 Auth 中间件和 Auth 控制器,但没有发现任何处理登录请求本身的内容。

我在这里Laravel redirect back to original destination after login 找到了一些关于登录重定向的信息,但我不确定在哪里放置建议的代码 sn-ps。

谁能帮帮我

【问题讨论】:

  • 将是重新创建/覆盖AuthController 以不同方式处理登录的任务,或者编写新的中间件以根据其角色将新登录的用户重定向到适当的页面。不过,对于 SO 而言,问题有点过于宽泛。

标签: php laravel redirect laravel-5


【解决方案1】:

您可以在 RedirectIfAuthenticated 中间件中更新

namespace App\Http\Middleware;

use Closure;
use Illuminate\Contracts\Auth\Guard;

class RedirectIfAuthenticated
{
    /**
     * The Guard implementation.
     *
     * @var Guard
     */
    protected $auth;

    /**
     * Create a new filter instance.
     *
     * @param  Guard  $auth
     * @return void
     */
    public function __construct(Guard $auth)
    {
        $this->auth = $auth;
    }

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if ($this->auth->check()) {

            $auth = Auth::user()->roles()->first();

            switch ($auth->role) {
                case 'admin':
                        return  redirect()->route('admin');    
                    break;
                case 'superadmin':
                        return  redirect()->route('superadmin'); 
                    break;
                case 'user':
                        return  redirect()->route('user');  
                    break;

                default:
                    # code...
                    return  redirect()->route('user');  
                    break;
            }   

         }

        return $next($request);
    }
}

【讨论】:

  • 不敢相信我错过了中间件中的那个文件
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-13
  • 2020-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多