Home > Software engineering >  Lavavel Middleware does not exist
Lavavel Middleware does not exist

Time:08-20

I have recently moved to Laravel from the normal php and I have managed to finish a project in my localhost, everything works fine at least to my expectation but when I moved my files to the server, I am experincing a middleware issue.

Now I have a middleware called isAdmin which was created using the artisan command. I have also registerd the class in the kernel as:

'isAdmin' => \App\Http\Middleware\IsAdmin::class, 

My isAdmin middleware cass is configued as:

<?php

namespace App\Http\Middleware;

use App\Models\User;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class isAdmin
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse)  $next
     * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
     */
    public function handle(Request $request, Closure $next)
    {

        if (Auth::user() &&  Auth::user()->isAdmin == 1) {
            return $next($request);
        }

        return redirect('/');
    }
}

this middleware works fine on my localhost, I already grouped all routes using the middleware like so:

Route::group(['middleware' => 'isAdmin'], function () {
        //routes that uses this middlewares here
});

When I uploaded my code to a shared hosting server, I get error:

Target class [App\Http\Middleware\IsAdmin] does not exist.

Can anyone suggest how I can fix this?? I also realize that I can not run artisan command because the current php version on the server is less that 8.0.2

CodePudding user response:

Solution

use App\Http\Middleware\IsAdmin;

Route::group(['middleware' => IsAdmin::class], function () {
    //routes that uses this middlewares here
});

CodePudding user response:

Some more detail would be required.

  1. What server are u using? Shared host or a private VPS?
  2. How are u moving the file to the server? Are u deploying using a pipeline or just FTP the file?
  • Related