【问题标题】:Create superAdmin middleware in Laravel 5.1在 Laravel 5.1 中创建 superAdmin 中间件
【发布时间】:2018-03-11 22:01:12
【问题描述】:

我需要在名为“master”的应用中创建 SuperAdmin 中间件,因此我制作了中间件“IsMaster”:

<?php

namespace App\Http\Middleware;

use Closure;

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

        return redirect('/auth/logout');
    }
}

从上面的代码中可以看出,如果我的“用户”表中的“管理员”字段的值为 101,即 MASTER 用户。

现在我将它添加到 Kernel.php:

protected $routeMiddleware = [
        'auth' => \App\Http\Middleware\Authenticate::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
        'admin' => \App\Http\Middleware\IsAdmin::class,
        'master' => \App\Http\Middleware\IsMaster::class,

    ];

现在在 AdminController.php 我像这样使用它:

class AdminController extends Controller
{

    public function __construct() {
        $this->middleware('master', ['except' => ['getMail']]);
    }

...

当我尝试使用主用户帐户登录时出现错误:

Container.php 第 741 行中的ReflectionException:Class master 没有 存在

这里有什么问题?我真的不明白是什么造成了问题......

我也尝试:php artisan config:clear, php artisan cache:clear ...

【问题讨论】:

  • 我的 IsAdmin 课程完美运行...
  • composer dumpautoload 有用吗?
  • 是的,这行得通!非常感谢!

标签: php laravel laravel-5.1 middleware


【解决方案1】:

只是为了将其标记为答案。

作曲家转储自动加载

【讨论】:

    猜你喜欢
    • 2016-03-19
    • 2015-12-02
    • 2015-12-24
    • 1970-01-01
    • 2016-03-18
    • 2015-10-14
    • 1970-01-01
    • 1970-01-01
    • 2016-04-13
    相关资源
    最近更新 更多