10

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');
Ahmad Baktash Hayeri
  • 5,802
  • 4
  • 30
  • 43
Yuko Pangestu
  • 193
  • 1
  • 2
  • 19

1 Answers1

22

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

Stacked
  • 6,892
  • 7
  • 57
  • 73
Andrej
  • 7,474
  • 1
  • 19
  • 21
  • Argh, thank you for your idea, Yes that's true I put it on console.. That's why I seen strange code there, thank you for reminding me... Case Closed – Yuko Pangestu Sep 03 '16 at 09:20