gpt4 book ai didi

php - Laravel 在 null 上调用成员函数 setCookie() - 当从中间件返回 Controller 操作时

转载 作者:可可西里 更新时间:2023-11-01 12:58:19 25 4
gpt4 key购买 nike

你好优秀的开发者,

这不是以下的直接副本: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);
}

Controller EndLegacyController index Action

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

到目前为止,我不知道如何解决这个问题,因为不能直接在中间件中返回 View ,由于更多的业务逻辑,它必须通过我的 Controller 返回。

请帮助我理解并解决这个问题。


更新 - 日志中的 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}
"}

最佳答案

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

如果您返回一个包含响应的 View ,它现在可以向其中添加一个 cookie。但是你以一种 hacky 的方式拦截了请求响应生命周期。

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

关于php - Laravel 在 null 上调用成员函数 setCookie() - 当从中间件返回 Controller 操作时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54705098/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com