Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

migas's avatar
Level 8

if session is expired redirect user to login in laravel 5.4

When a session expires, I get an error and the user is not redirected to the login page.

I Already use laravel-caffeine. This happens in normal pages, I mean, without forms. " (1/1) ErrorException Trying to get property of non-object"

Any suggestions in how to fix this in a simple way without just changing the 'lifetime' => 120,?

Thanks

0 likes
3 replies
Cronix's avatar
Cronix
Best Answer
Level 67

caffeine also looks for a meta tag in addition to forms with a _token field. You could add

<meta name="csrf_token" content="{{ csrf_token() }}">

to your master view and caffeine will work on all pages.

migas's avatar
Level 8

Thanks. I think that was happening because I forgot to put the middleware constructor in the controllers... But your solution is also good. Thanks @Cronix

Cronix's avatar

You shouldn't need to add the middleware to controllers, just register the middleware. Basically the middleware looks at the HTML before it gets sent to the browser and checks for the _token or meta tag, and inserts the little ajax javascript snippet for the drip if it finds either of those 2 things on the page. So if the HTML doesn't contain either of those 2 things, then it won't perform the ajax drip request. So really, there isn't a need to add it manually to controllers.

Please or to participate in this conversation.