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

alonegamer's avatar

laravel 11 middleware not working for some strange reasons

i developed a full laravel project. and now i am about to host it. but just before i could host it, i found out a problem, that every route that i am using for admin only is not even being protected by middleware. since laravel 11 has no kernel. what the hell am i supposed to do ? my one sesitive page is /bhashyas/view which should only be accessible to admin role. but it is accessible to any user (doesn't even have to be registered.) and i have several of this sensitive pages. the problem i am having is.

this is my route.

Route::middleware(['auth', 'role:author,admin'])->group(function () {
    Route::get('/bhashyas/view', [BhashyasController::class, 'view'])->name('bhashyas.view');
});

but when i try to access /bhashyas/view (even when i am admin) it throws

Illuminate  \  Contracts  \  Container  \  BindingResolutionException
Target class [role] does not exist.

PHP 8.2.13 Laravel 11.0.8 please please please i really need help. it's kinda urgent.

0 likes
9 replies
jlrdw's avatar

In bootstrap app note the items I added:

<?php

use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;
use Illuminate\Support\Facades\Route;

return Application::configure(basePath: dirname(__DIR__))
                ->withRouting(
                        web: __DIR__ . '/../routes/web.php',
                        commands: __DIR__ . '/../routes/console.php',
                        using: function () {
                            Route::middleware('web')
                            ->namespace('App\Http\Controllers')
                            ->group(base_path('routes/web.php'));
                        },
                        health: '/up',
                )
                ->withMiddleware(function (Middleware $middleware) {
                    $middleware->group('web', [
                        \Illuminate\Cookie\Middleware\EncryptCookies::class,
                        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
                        \Illuminate\Session\Middleware\StartSession::class,
                        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
                        \Illuminate\Foundation\Http\Middleware\ValidateCsrfToken::class,
                        \Illuminate\Routing\Middleware\SubstituteBindings::class,
                        \Illuminate\Session\Middleware\AuthenticateSession::class,
                    ]);
                })
                ->withExceptions(function (Exceptions $exceptions) {
                    //
                })->create();

The withMiddleware part, and this worked for me.

alonegamer's avatar

mine is like this <?php

use Illuminate\Foundation\Application; use Illuminate\Foundation\Configuration\Exceptions; use Illuminate\Foundation\Configuration\Middleware;

return Application::configure(basePath: dirname(DIR)) ->withRouting( web: DIR.'/../routes/web.php', api: DIR.'/../routes/api.php', commands: DIR.'/../routes/console.php', health: '/up', ) ->withMiddleware(function (Middleware $middleware) {

})
->withExceptions(function (Exceptions $exceptions) {
    //
})->create();

should i copy paste yours ?

alonegamer's avatar

update 1:- after copy pasting from your bootstrap app, i just simply can not access dashboard and login. (in a normal test user account) (which also means i can't access it as admin) so i have commented it out for now.

Snapey's avatar

please format your code in your questions

How is this different to your dev environment?

gych's avatar

@alonegamer To format your code start with 3 backticks add the code close with 3 backticks

```

YOUR CODE

```

Will result in

YOUR CODE
1 like
puklipo's avatar

Only you know what a "role" is.

// bootstrap/app.php

use App\Http\Middleware\Role;
 
->withMiddleware(function (Middleware $middleware) {
    $middleware->alias([
        'role' => Role::class,
    ]);
})

Please or to participate in this conversation.