VerifyCsrfToken.php 第 156 行中的 ErrorException:试图获取非对象 Laravel 中间件的属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42230601/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:19:35  来源:igfitidea点击:

ErrorException in VerifyCsrfToken.php line 156: Trying to get property of non-object laravel middleware

phplaravellaravel-5laravel-5.3laravel-middleware

提问by Alen

I am getting this error when I created my own middleware and used it

当我创建自己的中间件并使用它时出现此错误

public function handle($request, Closure $next)
{
    $privilege = $request->session()->has('privilege');

    if($request->session()->has('privilege'))
    {
        if($privilege == "Owner" || $privilege == "owner")
        {
            return $next($request);
        }
        else
        {
            return redirect()->back()->withErrors(['privilege_check' => "You are not privileged to go there!."]);
        }
    }
    return '/home';
}

回答by Ali Rasheed

Most probably you haven't set the session('privilege'). If you have already set it up then close down the browser and run the application again from start. It maybe did not set up during the development.

很可能你还没有设置session('privilege'). 如果您已经设置了它,请关闭浏览器并从头开始再次运行该应用程序。它可能没有在开发过程中设置。

回答by ImBhavin95

Check this

检查这个

public function __construct()
{
    $this->middleware(function ($request, $next) {
        if(Session::get('user_id') == NULL)
        {
            return Redirect::to('login');
        }else{
            return $next($request);
        }
    });
}