在中间件中请求输入 returns null

Request input returns null in Middleware

我正在通过中间件检查有效令牌来保护路由,就像这样;-

http://localhost:8097/init/my-secret-token


Route::get('/init/{token}', [MyController::class, 'init'])->middleware('validToken');


// Kernel.php
protected $routeMiddleware = [
    'validToken' => \App\Http\Middleware\EnsureTokenIsValid::class,
 ];


// App/Http/Middleware/EnsureTokenIsValid.php
class EnsureTokenIsValid
{

 public function handle(Request $request, Closure $next)
 {
    dd($request->input('token')); // returns null

    if($request->input('token') !== 'my-secret-token'){
        return redirect('home');
    }
    return $next($request);
 }
}

$request->input('token') returns 出于某种原因为空?我试过以下方法:-

http://localhost:8097/init/token/my-secret-token& http://localhost:8097/init?token=my-secret-token - 只收到一个 400

你试过了吗:

$request->route('token')

这应该直接从您的路线中获取参数。