在null上调用成员函数setCookie() [英] Call to a member function setCookie() on null

查看:92
本文介绍了在null上调用成员函数setCookie()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在幼虫中完成此中间件,以检查确保已订阅了订阅计划.如果不是该用户,它将重定向到付款页面.

I am trying to finish this middleware in larval that checks to make sure is subscribed to a subscription plan. If the user is not it redirects to the payment page.

public function handle($request, Closure $next)
{
    if(Auth::check()){
        if (Auth::user()->subscribed('main')) {
            return true;
        }else{
            return view('payments.payment')->with('user',Auth::user());
        }
    }else{
        abort(403, 'Unauthorized action.');
    }
    return $next($request);
}

我遇到这个错误的原因是运气不佳,无法找到解决方案.对null的成员函数setCookie()的调用.

I am getting this error with not much luck finding a solution Call to a member function setCookie() on null.

推荐答案

问题在于您要返回true.中间件应返回一个响应样式的对象,而不是布尔值.

The problem is where you are returning true. A middleware should return a response-style object, not a boolean.

由于这是您的好"路径,并且您希望继续进行应用程序逻辑,因此应将return true;替换为return $next($request);

Since that is your "good" path and you want to proceed with your application logic, you should replace return true; with return $next($request);

public function handle($request, Closure $next)
{
    if(Auth::check()){
        if (Auth::user()->subscribed('main')) {
            return $next($request);
        }else{
            return view('payments.payment')->with('user',Auth::user());
        }
    }else{
        abort(403, 'Unauthorized action.');
    }
}

在不相关的建议下,您可以稍微整理一下条件逻辑,以使代码更易于阅读/关注:

On an unrelated recommendation, you can clean up your conditional logic a bit to make your code easier to read/follow:

public function handle($request, Closure $next)
{
    // If the user is not logged in, respond with a 403 error.
    if ( ! Auth::check()) {
        abort(403, 'Unauthorized action.');
    }

    // If the user is not subscribed, show a different payments page.
    if ( ! Auth::user()->subscribed('main')) {
        return view('payments.payment')->with('user',Auth::user());
    }

    // The user is subscribed; continue with the request.
    return $next($request);
}

这篇关于在null上调用成员函数setCookie()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆