Laravel 5.3中间件类不存在

时间:2016-09-03 08:30:09

标签: php laravel-middleware laravel-5.3

我想基于中间件进行一些身份验证..但不幸的是,它因类不存在而返回

这是我的中间件

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";
    }
}

这是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');
    }
}

我尝试过composer dump-autoload,但它并没有效果。

这是我的路线:

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

1 个答案:

答案 0 :(得分:14)

如果要将中间件应用于Http调用,则应在proxy_pass中注册它们。但是,您的中间件已在app/Http/Kernel.php中注册为控制台命令。 见more on Laravel documentation