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

AbdulBazith's avatar

ReflectionException (-1) Class clearance does not exist after login

Guys iam working with a project School Management System

i processed my login authentication work with spatie-larevel-permission package.

everything is fine but iam getting an error when moving to add a student after login

ReflectionException (-1)
Class clearance does not exist

i have added this line in my StudentController

public function __construct() {
        $this->middleware(['auth', 'clearance']); //isAdmin middleware lets only users with a //specific permission permission to access these resources
    }

even i changed the line like below also


 public function __construct() {
        $this->middleware(['auth', 'isAdmin']); //isAdmin middleware lets only users with a //specific permission permission to access these resources
    }

whats the problem, after login it moves to home page, but when i click the nav bar add student it shows such an error. i dont know which line to add, when i referred in net they used with except('index','show'). but i no need that, everthing should be authenticated, as this was an application

i have users table, role table, role->permission table, user->role table.

and in my user model i added this line use HasRoles;

i used php php artisan make:auth

0 likes
6 replies
jove's avatar

Try running composer dump-autoload

Also, the package spatie/laravel-permission comes with RoleMiddleware, PermissionMiddleware and RoleOrPermissionMiddleware. Why can't you use that? etc

Route::group(['middleware' => ['role:super-admin']], function () {
    //
});

Route::group(['middleware' => ['permission:publish articles']], function () {
    //
});
AbdulBazith's avatar

@jove thank you for your reply.

at my blade level, i started restricting by adding these lines like

@role('writer')
    I am a writer!
@else
    I am not a writer...
@endrole

but at my controller level i didnt do that?? so can you suggest you ideas?

whats the problem in that error ReflectionException (-1) Class clearance does not exist

Nakov's avatar

@abdulbazith in order to use a custom middleware you will need to register it in your App\Http\Kernel.php class, in the $routeMiddleware array.

For example:

protected $routeMiddleware = [
        'auth' => \App\Http\Middleware\Authenticate::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
        'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
        'can' => \Illuminate\Auth\Middleware\Authorize::class,
        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
        'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
        'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
        'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,

        // add your own here
        'isAdmin' => \App\Http\Middleware\IsAdmin::class
    ];
1 like
jove's avatar

@abdulbazith If you want to use the ones offered by the package (See documentation: https://docs.spatie.be/laravel-permission/v3/basic-usage/middleware/)

You will also have to do what @nakov said for these, like this (mentioned in the documentation):

protected $routeMiddleware = [
    // ...
    'role' => \Spatie\Permission\Middlewares\RoleMiddleware::class,
    'permission' => \Spatie\Permission\Middlewares\PermissionMiddleware::class,
    'role_or_permission' => \Spatie\Permission\Middlewares\RoleOrPermissionMiddleware::class,
];

Note, the // ... does not mean you should remove the existing ones. You need them too.

Then just use them in the controller like this

public function __construct()
{
    $this->middleware(['role:super-admin','permission:publish articles|edit articles']);
}

See the documentation for more examples.

AbdulBazith's avatar

@jove @nakov

i tried like this adding this line in my App\Http\Kernel.php class, in the $routeMiddleware array.

protected $routeMiddleware = [
        'auth' => \App\Http\Middleware\Authenticate::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
        'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
        'can' => \Illuminate\Auth\Middleware\Authorize::class,
        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
        'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
        'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
        'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,

        // add your own here
        'isAdmin' => \App\Http\Middleware\IsAdmin::class     // This line i added
    ];


and when i used like below

public function __construct() {
        $this->middleware(['auth', 'isAdmin']); //isAdmin middleware lets only users with a //specific permission permission to access these resources
    }

it shows error Class App\Http\Middleware\IsAdmin does not exist

i googled it found a line when i added that it works. but i dont know wether that line is correct or not.

  public function __construct() {
        $this->middleware('auth', ['except' => ['getActivate', 'anotherMethod']]);
    }

The above line works fine. but i dont know those methods why present??

Nakov's avatar

@abdulbazith

it shows error Class App\Http\Middleware\IsAdmin does not exist

The error says it all. You don't have a class IsAdmin in your middleware directory, or the namespace on top is wrong.. Make sure that your class starts like this:

<?php 

namespace App\Http\Middleware;

...

You googled and found a line that works, but that line would work for ANY authenticated user, not just the Admin.

Please or to participate in this conversation.