【问题标题】:Laravel 5 middleware Auth check not workingLaravel 5 中间件身份验证检查不起作用
【发布时间】:2015-07-19 17:56:31
【问题描述】:

如果用户在未登录或没有“管理员”类型的情况下尝试访问管理页面,我想显示 404 错误页面。如果用户登录,则此工作正常,如果不是用户访问管理面板。如果我删除if(Auth::check()),脚本会显示Trying to get property of non-object 错误。

class AdminMiddleware {
    public function handle($request, Closure $next)
    {
        if(Auth::check()){
            if ($request->user()->type != 'Admin'){
                return abort(404);
            }
        }

        return $next($request);
    }

}

【问题讨论】:

    标签: php redirect laravel-5 infinite-loop middleware


    【解决方案1】:

    试试这个

    class AdminMiddleware {
        public function handle($request, Closure $next)
        {
            if(Auth::check()){
                if ($request->user()->type != 'Admin'){
                    return abort(404);
                }
            }else{
                return abort(404);
            }
            return $next($request);
        }
    }
    

    它应该检查用户是否登录,如果是,检查他和管理员,如果他没有登录,则显示 404

    或更短的版本

    class AdminMiddleware {
        public function handle($request, Closure $next)
        {
            if(Auth::check() && $request->user()->type == 'Admin'){
                return $next($request);
            }
            return abort(404);
        }
    }
    

    【讨论】:

    • 它有效,非常感谢!我忘了把 else 放在 Auth::check() 中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-04
    • 2015-07-28
    • 1970-01-01
    • 1970-01-01
    • 2016-10-21
    • 1970-01-01
    相关资源
    最近更新 更多