Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Call to a member function setCookie() on null - when returning controller action from middleware

Hello Good Developers,

This is Not a Direct Duplicate of: Call to a member function setCookie() on null As I am calling a Controller action from Middleware and then in the controller action I am returning View,

Here's My Route from web.php

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

Middleware LegacyEndCheck.php handle function

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

So Far I've no Idea how to fix this issue as cannot return the view directly in middleware, It has to be returned through my controller because of more business logic.

Please help me to understand and resolve this issue.


Update - Stacktrace in Logs

[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}
"} 
like image 472
Pankaj Jha Avatar asked Feb 15 '19 08:02

Pankaj Jha


1 Answers

The Laravel lifecycle sends your request through all middleware, what you are doing is actual replacing the current response with a new view. Only responses or request should be in this flow. This is an anti pattern and it is best to do a redirect. This will make laravels lifecycle try to run its middleware on a view and trying to set a cookie on it.

If you return a view wrapped with a response, it can now add a cookie to it. But you intercept the request response lifecycle in a hacky manner.

return Response::view('survey.legacy.end', [ 'status' => $status, 'respstatus' =>  $respstatus, 'rowSurvey' => $rowSurvey ]);
like image 83
mrhn Avatar answered Nov 10 '22 11:11

mrhn