Laravel 5.3 Middleware class is not exist

Yuko Pangestu picture Yuko Pangestu · Sep 3, 2016 · Viewed 20.1k times · Source

I would like to make some authentication based on middleware.. But unfortunately it returns as the class is not exist

Here is my middleware

Staff.php

<?php

namespace App\Http\Middleware;

use Closure;
use Auth;

class Staff
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $user = Auth::user()->type;
        if ($user == 'S'){
            return $next($request);
        }

        return "no";
    }
}

Here is the kernel.php

<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        //
    ];

    protected $middleware = [
        \App\http\Middleware\Staff::class,
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        // $schedule->command('inspire')
        //          ->hourly();
    }

    /**
     * Register the Closure based commands for the application.
     *
     * @return void
     */
    protected function commands()
    {
        require base_path('routes/console.php');
    }
}

I have tried composer dump-autoload but it doesn't effective.

Here is my route:

Route::get('/staff', 'StaffController@index')->middleware('Staff');

Answer

Andrej Ludinovskov picture Andrej Ludinovskov · Sep 3, 2016

If you want to apply middlewares to Http calls you should register them in app/Http/Kernel.php. However, your middleware is registered for console commands in App\Console\Kernel. See more on Laravel documentation