Call to a member function setCookie() on null

前端 未结 4 1006
不知归路
不知归路 2020-12-11 04:00

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.



        
相关标签:
4条回答
  • 2020-12-11 04:22

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

    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);
    }
    
    0 讨论(0)
  • 2020-12-11 04:26

    i solve by redirect to other routereturn redirect()->to('route');

    0 讨论(0)
  • 2020-12-11 04:36

    change

    return view('payments.payment')
    

    to

    return response()->view('payments.payment')
    
    0 讨论(0)
  • 2020-12-11 04:37

    return response(view('payments.payment')->with('user',Auth::user()));

    0 讨论(0)
提交回复
热议问题