Laravel 5.1创建SuperAdmin中间件报错:Class master不存在
Hey there, let's break down why you're hitting this ReflectionException and fix it step by step. The error tells us Laravel can't find a class named master—which makes sense because your middleware is called IsMaster. This almost always boils down to a mismatch in how you registered or referenced the middleware.
Step 1: Verify Your Middleware File & Namespace
First, double-check your IsMaster.php file in app/Http/Middleware/:
- Ensure the filename is exactly
IsMaster.php(case-sensitive—Linux servers care about this!) - Confirm the namespace and class name match perfectly:
<?php namespace App\Http\Middleware; use Closure; use Illuminate\Http\Request; class IsMaster { public function handle(Request $request, Closure $next) { // Your logic here: check if auth user's admin field is 101 if (auth()->check() && auth()->user()->admin === 101) { return $next($request); } abort(403, 'You are not authorized to access this area.'); } }
Step 2: Fix Middleware Registration in Kernel.php
Head to app/Http/Kernel.php and locate the $routeMiddleware array (this is where route-specific middleware lives). You need to map the alias master to your full middleware class path—make sure there are no typos here:
protected $routeMiddleware = [ // ... other existing middleware like 'auth', 'guest' 'master' => \App\Http\Middleware\IsMaster::class, ];
Common mistakes here: misspelling IsMaster, forgetting the namespace, or using the wrong case.
Step 3: Confirm Middleware Usage in AdminController
In your AdminController.php, make sure you're using the correct alias (master) in the constructor:
public function __construct() { $this->middleware('master'); }
If you're using the middleware directly in routes, double-check that too:
Route::prefix('admin')->middleware('master')->group(function () { // Admin routes here });
Don't accidentally use IsMaster (the class name) instead of the alias master here—that's a common slip-up.
Step 4: Clear Laravel Caches
Old cached config/routes can cause Laravel to look for outdated class references. Run these commands in your terminal:
php artisan cache:clear php artisan route:clear php artisan config:clear php artisan optimize:clear
This will wipe all cached data and force Laravel to reload your latest middleware setup.
Bonus: Guard Against Null User Errors
While not related to the current Class master error, adding a check for auth()->check() before accessing auth()->user()->admin will prevent a separate error if a guest tries to access the admin route. You already have this in your logic, but it's worth emphasizing!
内容的提问来源于stack exchange,提问作者Aleks Per

