我有一个简单的中间件,用于检查用户会话中是否有密钥。
<?php
namespace App\Http\Middleware;
use Closure;
class CustomAuth
{
public function handle($request, Closure $next)
{
if($request->session()->has('uid')){
return $next($request);
}
else{
return view('unauth');
}
}
}
Run Code Online (Sandbox Code Playgroud)
问题是我总是收到“未按请求设置会话存储”。错误。这是我的路线:
Route::get('home', function () {
return view('home');
})->middleware('web', 'CustomAuth');
Run Code Online (Sandbox Code Playgroud)
我在变量 $middleware 中添加了 app\Http\Kernel.php 中的中间件
protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
\App\Http\Middleware\CustomAuth::class
];
Run Code Online (Sandbox Code Playgroud)
我也尝试改变我的路线:
Route::group(['middleware' => ['web']], function () {
Route::get('home', function () {
return view('home');
})->middleware('CustomAuth');
});
Run Code Online (Sandbox Code Playgroud)
但这没有用。知道如何确保会话已启动,或在调用中间件之前启动它吗?我正在使用 Laravel 5.3