Auth::user() 返回 null
2022-08-30 11:06:55
我使用Laravel 5.2,中间件有问题。路由中有代码.php
use Illuminate\Contracts\Auth\Access\Gate; Route::group(['middleware' => 'web'], function () { Route::auth(); Route::get('/', 'HomeController@index'); }); Route::group(['prefix'=>'admin', 'middleware' => 'admin'], function(){ Route::get('/', function(){ return view('admin.index'); }); Route::get('/user', function(){ return view('admin.user'); }); });
内核.php:
protected $routeMiddleware = [ ... 'admin' => \App\Http\Middleware\AdminPanel::class, ];
管理员面板.php
namespace App\Http\Middleware; use Closure; use Illuminate\Support\Facades\Auth; use App\Role; class AdminPanel { public function handle($request, Closure $next) { $user = Auth::user(); dd($user); if($user){ $role = Role::whereName('admin')->first(); if($user->hasRole($role)){ return $next($request); } } return redirect('/'); }
所以
$user = Auth::user
()
始终返回空值。感谢您的建议!