Search code examples
laravellaravel-routinglaravel-middlewarelaravel-request

Laravel 8: GET params can't be accessed in Middleware's Request's inputs


I have defined this route in the web.php route file:

Route::get('/middleware_test_user_project_change/{pro_id}/{projet_id}', function ($pro_id, $projet_id) {
    return 'test';
})->middleware('user.project.change');

I have defined this handle function in my middleware (which I've added into the kernel with the following entry: 'user.project.change' => \App\Http\Middleware\CheckUserProposition::class):

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use App\Models\User;

class CheckUserProposition
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle(Request $request, Closure $next)
    {
        $projet_id = $request->input('projet_id');
        $pro_id = $request->input('pro_id');
        return $next($request);
    }

}

However, both $projet_id and $pro_id return NULL when I access the following URL: https://XYZ/middleware_test_user_project_change/1/1

As I've correctly set up the middleware and the routes parameters (which are, finally, GET variables), why can't I use them in my middleware as request inputs?


Solution

  • Route parameters are not part of the 'inputs'. They are a separate thing; this is why you don't see them when you get all the inputs with $request->all().

    If you want a route parameter you should probably explicitly ask for it:

    $request->route('projet_id');
    $request->route()->parameter('projet_id');