If you not planning to use stateless api's move the contents of web middleware group to global middleware stack. Session will always be created then.
Session variables inside ExceptionHandler are not working
I'm porting an existing app into Laravel 5.2 and I'm struggling with the session management and the ExceptionHandler.
The application checks in Blade for some session variables and acting accordingly. The session variables are managed in a Middleware and the routes are inside the 'web' group. Everything works like a charm!
I'm also using a custom ExceptionHandler which checks for the error, creates custom error messages and passes them into a "generic error" view.
return response()->view('errors.generic', [
'action' => "error",
'errorCode' => $e->getStatusCode(),
'headTitle' => "Error {$e->getStatusCode()}",
'pageTitle' => $tmpErrorMessage,
'pageTitleDescription' => "Error {$e->getStatusCode()}"
]);
The problem is that, when an error is triggered (e.x. 404) the session has not been initialized by Laravel and the view returns a "Session store not set on request".
I manage to "solve" the problem by creating an "error" route:
Route::get('/error/', 'IndexController@error')->name('error');
and I've changed the ExceptionHandler to:
return redirect()->route('error', [
'action' => "error",
'errorCode' => $e->getStatusCode(),
'headTitle' => "Error {$e->getStatusCode()}",
'pageTitle' => $tmpErrorMessage,
'pageTitleDescription' => "Error {$e->getStatusCode()}"
]);
and finally inside the IndexController@error :
return view('errors.generic', [
'action' => "error",
'errorCode' => $request->get('errorCode'),
'headTitle' => $request->get('headTitle'),
'pageTitle' => $request->get('pageTitle'),
'pageTitleDescription' => $request->get('pageTitleDescription')
]);
This solution works fine, the session is initialized by the Laravel, and the session variables are accessible from the template. But.... this solution creates an ugly URI.
What am I doing wrong with the exception handling? What can I do to have access to the session variables inside an exception handling?
T.I.A
What is this? CheckSessionMiddleware(), if at all using this move it below StartSession middleware.
Please or to participate in this conversation.