laravel 在 null 上调用成员函数 setCookie()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48970172/
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
Call to a member function setCookie() on null
提问by John Freedom
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.
我正在尝试在 larval 中完成这个中间件,它会检查以确保订阅了订阅计划。如果用户不是它重定向到支付页面。
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);
}
I am getting this error with not much luck finding a solution Call to a member function setCookie() on null.
我遇到了这个错误,但没有多少运气找到解决方案 Call to a member function setCookie() on null。
回答by Aken Roberts
The problem is where you are returning true
. A middleware should return a response-style object, not a boolean.
问题是你返回的地方true
。中间件应该返回一个响应样式的对象,而不是一个布尔值。
Since that is your "good" path and you want to proceed with your application logic, you should replace return true;
with return $next($request);
由于那是您的“好”路径并且您想继续处理您的应用程序逻辑,因此您应该将其替换return true;
为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);
}
回答by asghar
i solve by redirect to other routereturn redirect()->to('route');
我通过重定向到其他路线解决return redirect()->to('route');
回答by Nayan Zala
return response(view('payments.payment')->with('user',Auth::user()));
return response(view('payments.payment')->with('user',Auth::user()));