Skip to main content

Call to a member function middleware() on null

 In web.php I the following routes:

web.php

Route::prefix('app')->group(function(){
    Route::post('/create_tag','AdminController@addTag');
})->middleware(AdminCheck::class);

Here, middleware is AdminCheck.

and in my AdminCheck middleware I wrote the following to test the middleware:

AdminCheck.php 

public function handle($requestClosure $next)
    {

        if(!Auth::check())
        {
            return 'not loggedIn';
        }

        return $next($request);
    }

Now when I reload the '/' url of the website it shows 'Call to a member function middleware() on null' 

error.


To solve this: 

In web.php change the previous middleware position like the following:

Route::prefix('app')->middleware([AdminCheck::class])->group(function(){
    Route::post('/create_tag','AdminController@addTag');
});

and inside AdminCheck.php middleware do the following: 


public function handle($requestClosure $next)
    {
        
        if(!Auth::check())
        {
            return response()->json([
                'msg' => 'You are not allowed to access this route'
            ],402);
        }

        return $next($request);
    }





Comments

Popular posts from this blog