Laravel在null上调用成员函数setCookie()-从中间件返回控制器操作时

时间:2019-02-15 08:18:37

标签: php laravel laravel-middleware

你好,开发人员,

这不是以下各项的直接副本:Call to a member function setCookie() on null 当我从中间件中调用Controller动作,然后在controller动作中,我返回View,

这是我从web.php出发的路线

Route::get('/end', [EndPageController::class, 'index'])
->name('survey.end')
->middleware('App\Http\Middleware\LegacyEndCheck');

中间件LegacyEndCheck.php句柄功能

public function handle($request, Closure $next)
{
    $sjid = $request->input('sjid', false);

    if( empty($sjid) || strlen($sjid) !== 36 ){
        return app()->make(EndLegacyController::class)->index($request); //I have to call EndLegacyController@index here as I can't change URL using redirect route
    }

    return $next($request);
}

控制器EndLegacyController index操作

public function index(Request $request)
{
    $sjid = $request->input('sjid',false);
    $status = $request->input('status',false);
    if( empty($sjid) || empty($status) ){
        $this->status = 5;
        return view('survey.legacy.end')
            ->with('status', $this->status);
    }
    dd('allright');
}

Error Screenshot

到目前为止,我还不知道如何解决此问题,因为无法直接在中间件中返回视图,由于更多的业务逻辑,必须通过我的控制器将其返回。

请帮助我了解并解决此问题。


更新-日志中的Stacktrace

[2019-02-15 07:59:42] local.ERROR: Call to a member function setCookie() on null {"exception":"[object] (Symfony\\Component\\Debug\\Exception\\FatalThrowableError(code: 0): Call to a member function setCookie() on null at D:\\xampp-new-installations\\xampp-7-2-4\\htdocs\\samppoint-new-laravel\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\VerifyCsrfToken.php:180)
[stacktrace]
#0 D:\\xampp-new-installations\\xampp-7-2-4\\htdocs\\samppoint-new-laravel\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\VerifyCsrfToken.php(77): Illuminate\\Foundation\\Http\\Middleware\\VerifyCsrfToken->addCookieToResponse(Object(Illuminate\\Http\\Request), Object(Illuminate\\View\\View))
#1 D:\\xampp-new-installations\\xampp-7-2-4\\htdocs\\samppoint-new-laravel\\vendor\\laravel\\framework\\src\\Illuminate\\Support\\helpers.php(1027): Illuminate\\Foundation\\Http\\Middleware\\VerifyCsrfToken->Illuminate\\Foundation\\Http\\Middleware\\{closure}(Object(Illuminate\\View\\View))
#2 D:\\xampp-new-installations\\xampp-7-2-4\\htdocs\\samppoint-new-laravel\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\VerifyCsrfToken.php(79): tap(Object(Illuminate\\View\\View), Object(Closure))
#3 D:\\xampp-new-installations\\xampp-7-2-4\\htdocs\\samppoint-new-laravel\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(163): Illuminate\\Foundation\\Http\\Middleware\\VerifyCsrfToken->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#4 D:\\xampp-new-installations\\xampp-7-2-4\\htdocs\\samppoint-new-laravel\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#5 D:\\xampp-new-installations\\xampp-7-2-4\\htdocs\\samppoint-new-laravel\\vendor\\laravel\\framework\\src\\Illuminate\\View\\Middleware\\ShareErrorsFromSession.php(49): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#6 D:\\xampp-new-installations\\xampp-7-2-4\\htdocs\\samppoint-new-laravel\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(163): Illuminate\\View\\Middleware\\ShareErrorsFromSession->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#7 D:\\xampp-new-installations\\xampp-7-2-4\\htdocs\\samppoint-new-laravel\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#8 D:\\xampp-new-installations\\xampp-7-2-4\\htdocs\\samppoint-new-laravel\\vendor\\laravel\\framework\\src\\Illuminate\\Session\\Middleware\\StartSession.php(63): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#9 D:\\xampp-new-installations\\xampp-7-2-4\\htdocs\\samppoint-new-laravel\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(163): Illuminate\\Session\\Middleware\\StartSession->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#10 D:\\xampp-new-installations\\xampp-7-2-4\\htdocs\\samppoint-new-laravel\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#11 D:\\xampp-new-installations\\xampp-7-2-4\\htdocs\\samppoint-new-laravel\\vendor\\laravel\\framework\\src\\Illuminate\\Cookie\\Middleware\\AddQueuedCookiesToResponse.php(37): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#12 D:\\xampp-new-installations\\xampp-7-2-4\\htdocs\\samppoint-new-laravel\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(163): Illuminate\\Cookie\\Middleware\\AddQueuedCookiesToResponse->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#13 D:\\xampp-new-installations\\xampp-7-2-4\\htdocs\\samppoint-new-laravel\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#14 D:\\xampp-new-installations\\xampp-7-2-4\\htdocs\\samppoint-new-laravel\\vendor\\laravel\\framework\\src\\Illuminate\\Cookie\\Middleware\\EncryptCookies.php(66): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#15 D:\\xampp-new-installations\\xampp-7-2-4\\htdocs\\samppoint-new-laravel\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(163): Illuminate\\Cookie\\Middleware\\EncryptCookies->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#16 D:\\xampp-new-installations\\xampp-7-2-4\\htdocs\\samppoint-new-laravel\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#17 D:\\xampp-new-installations\\xampp-7-2-4\\htdocs\\samppoint-new-laravel\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(104): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#18 D:\\xampp-new-installations\\xampp-7-2-4\\htdocs\\samppoint-new-laravel\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php(684): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))
#19 D:\\xampp-new-installations\\xampp-7-2-4\\htdocs\\samppoint-new-laravel\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php(659): Illuminate\\Routing\\Router->runRouteWithinStack(Object(Illuminate\\Routing\\Route), Object(Illuminate\\Http\\Request))
#20 D:\\xampp-new-installations\\xampp-7-2-4\\htdocs\\samppoint-new-laravel\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php(625): Illuminate\\Routing\\Router->runRoute(Object(Illuminate\\Http\\Request), Object(Illuminate\\Routing\\Route))
#21 D:\\xampp-new-installations\\xampp-7-2-4\\htdocs\\samppoint-new-laravel\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php(614): Illuminate\\Routing\\Router->dispatchToRoute(Object(Illuminate\\Http\\Request))
#22 D:\\xampp-new-installations\\xampp-7-2-4\\htdocs\\samppoint-new-laravel\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Kernel.php(176): Illuminate\\Routing\\Router->dispatch(Object(Illuminate\\Http\\Request))
#23 D:\\xampp-new-installations\\xampp-7-2-4\\htdocs\\samppoint-new-laravel\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php(30): Illuminate\\Foundation\\Http\\Kernel->Illuminate\\Foundation\\Http\\{closure}(Object(Illuminate\\Http\\Request))
#24 D:\\xampp-new-installations\\xampp-7-2-4\\htdocs\\samppoint-new-laravel\\vendor\\fideloper\\proxy\\src\\TrustProxies.php(57): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#25 D:\\xampp-new-installations\\xampp-7-2-4\\htdocs\\samppoint-new-laravel\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(163): Fideloper\\Proxy\\TrustProxies->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#26 D:\\xampp-new-installations\\xampp-7-2-4\\htdocs\\samppoint-new-laravel\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#27 D:\\xampp-new-installations\\xampp-7-2-4\\htdocs\\samppoint-new-laravel\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest.php(31): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#28 D:\\xampp-new-installations\\xampp-7-2-4\\htdocs\\samppoint-new-laravel\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(163): Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#29 D:\\xampp-new-installations\\xampp-7-2-4\\htdocs\\samppoint-new-laravel\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#30 D:\\xampp-new-installations\\xampp-7-2-4\\htdocs\\samppoint-new-laravel\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest.php(31): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#31 D:\\xampp-new-installations\\xampp-7-2-4\\htdocs\\samppoint-new-laravel\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(163): Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#32 D:\\xampp-new-installations\\xampp-7-2-4\\htdocs\\samppoint-new-laravel\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#33 D:\\xampp-new-installations\\xampp-7-2-4\\htdocs\\samppoint-new-laravel\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize.php(27): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#34 D:\\xampp-new-installations\\xampp-7-2-4\\htdocs\\samppoint-new-laravel\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(163): Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#35 D:\\xampp-new-installations\\xampp-7-2-4\\htdocs\\samppoint-new-laravel\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#36 D:\\xampp-new-installations\\xampp-7-2-4\\htdocs\\samppoint-new-laravel\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\CheckForMaintenanceMode.php(62): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#37 D:\\xampp-new-installations\\xampp-7-2-4\\htdocs\\samppoint-new-laravel\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(163): Illuminate\\Foundation\\Http\\Middleware\\CheckForMaintenanceMode->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#38 D:\\xampp-new-installations\\xampp-7-2-4\\htdocs\\samppoint-new-laravel\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#39 D:\\xampp-new-installations\\xampp-7-2-4\\htdocs\\samppoint-new-laravel\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(104): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#40 D:\\xampp-new-installations\\xampp-7-2-4\\htdocs\\samppoint-new-laravel\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Kernel.php(151): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))
#41 D:\\xampp-new-installations\\xampp-7-2-4\\htdocs\\samppoint-new-laravel\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Kernel.php(116): Illuminate\\Foundation\\Http\\Kernel->sendRequestThroughRouter(Object(Illuminate\\Http\\Request))
#42 D:\\xampp-new-installations\\xampp-7-2-4\\htdocs\\samppoint-new-laravel\\public\\index.php(55): Illuminate\\Foundation\\Http\\Kernel->handle(Object(Illuminate\\Http\\Request))
#43 {main}
"} 

1 个答案:

答案 0 :(得分:2)

Laravel生命周期通过所有中间件发送您的请求,实际上您正在用新视图替换当前响应。仅响应或请求应在此流程中。这是一种反模式,最好进行重定向。这将使laravels生命周期尝试在视图上运行其中间件并尝试在其上设置cookie。

如果返回包含响应的视图,则该视图现在可以向其中添加cookie。但是您会以一种卑鄙的方式拦截请求响应生命周期。

return Response::view('survey.legacy.end', [ 'status' => $status, 'respstatus' =>  $respstatus, 'rowSurvey' => $rowSurvey ]);