php - Laravel 基于角色的身份验证和视图。路由中的中间件不起作用
问题描述
时间 laravel 用户。我按照此步骤创建基于角色的登录和视图。
我相信一旦我们登录,它应该根据用户角色将我们重定向到各自的视图。但是当我登录时,什么也没有发生。它只是带我到默认主页。我的路线文件有什么问题吗?
路由文件
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers;
use App\Http\Middleware;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Route::group(['as'=>'admin.','prefix' => 'admin','namespace'=>'Admin','middleware'=>['auth','admin']], function () {
Route::get('dashboard', [App\Http\Controllers\Admin\DashboardController::class, 'index'])->name('dashboard');
});
Route::group(['as'=>'user.','prefix' => 'user','namespace'=>'User','middleware'=>['auth','user']], function () {
Route::get('dashboard', [App\Http\Controllers\User\DashboardController::class, 'index'])->name('dashboard');
});
Route::group(['as'=>'employer.','prefix' => 'employer','namespace'=>'Employer','middleware'=>['auth','employer']], function () {
Route::get('dashboard', [App\Http\Controllers\Employer\DashboardController::class, 'index'])->name('dashboard');
});
我的登录控制器
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Auth;
class LoginController extends Controller
{
/*
|--------------------------------------------------------------------------
| Login Controller
|--------------------------------------------------------------------------
|
| This controller handles authenticating users for the application and
| redirecting them to your home screen. The controller uses a trait
| to conveniently provide its functionality to your applications.
|
*/
use AuthenticatesUsers;
/**
* Where to redirect users after login.
*
* @var string
*/
protected $redirectTo;
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
if(Auth::check() && Auth::user()->role_id == 1){
$this->redirectTo = route('admin.dashboard');
} elseif(Auth::check() && Auth::user()->role_id == 2){
$this->redirectTo = route('user.dashboard');
} elseif(Auth::check() && Auth::user()->role_id == 3){
$this->redirectTo = route('employer.dashboard');
}
$this->middleware('guest')->except('logout');
}
}
解决方案
推荐阅读
- unity3d - 发布游戏后 Google Play 游戏服务无法登录
- angular - Angular TypeScript Protractror end-2-end 测试 - 无需在代码中调用 browser.sleep()
- java - 为什么 main 方法调用 compare() 方法两次?
- amazon-web-services - 使用 Terraform 创建 AWS 资源组
- amazon-web-services - 有没有其他方法可以知道 DynamoDb 表何时减少?
- laravel - Laravel 在 php 块中使用刀片指令
- docker - 使用intellij在kubernetes中远程调试容器
- typescript - TypeScript 类型中的静态类
- vim - Vim8 不能删除字母
- python - 为什么我的循环生成的 Keras 顺序模型都完全相同?