Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Ilovekrupuk's avatar

Laravel 11 Middleware parameter doesnt work

This is my middleware

public function handle(Request $request, Closure $next, ...$roles): Response
    {
        if (Auth::check()) {
            \Log::info('User Role: ' . Auth::user()->role);
        \Log::info('Allowed Roles: ' . implode(', ', $roles));
            // Check if the user's role is in the array of allowed roles
            if (in_array(Auth::user()->role, $roles)) {
                return $next($request); // Allow the request to proceed
            }
        }

        // If the user is not authorized, redirect or return a response
        return redirect('/login')->with('error', 'You do not have access to this resource.');
    }

And this is my routing

Route::get('/', [AdminController::class, 'main'])->middleware(CheckUser::class,':admin');

And this is a snippet of the log

[2024-11-05 07:34:01] local.INFO: User Role: admin  
[2024-11-05 07:34:01] local.INFO: Allowed Roles:   
[2024-11-05 07:34:01] local.ERROR: Target class [] does not exist. {"userId":1,"exception":"[object] (Illuminate\\Contracts\\Container\\BindingResolutionException(code: 0): Target class [] does not exist. at /var/www/ezzraledb/vendor/laravel/framework/src/Illuminate/Container/Container.php:940)
[stacktrace]

Why is there nothing in the Allowed Roles: ??? I tried accessing the main page while being logged in as admin, it doesnt work and it redirected me to the login page

0 likes
2 replies
cgsmith105's avatar
Level 1

middleware method call should be a period not a comma.

also is good code practice to make the parameter required or throw an exception if something is expected but not passed.

1 like

Please or to participate in this conversation.