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

nathanrobjohn's avatar

Where to place Session logic

Hi Guys,

I am just wondering where you put all session logic? an app i have taken over its currently all in controllers so your constantly repeating yourself and i am trying to stick to DRY theory where would you suggest putting the logic?

0 likes
3 replies
Kryptonit3's avatar

Do you have a general example of what you mean by session logic? Storing data upon login, logic specific to certain actions on your site? Flashing messages to the session? This is a very vague question.

nathanrobjohn's avatar

So the site basically its a travel tour website so user goes to site searches a location and picks a bus time and its then carried through sessions until they checkout so every page has


                Session::put('PersonName', $user->FirstName . ' ' . $user->MiddleName . ' ' . $user->LastName);
                Session::put('PersonID', $user->PersonID);
                Session::put('Email', $user->Email);
                Session::put('MobilePhone', $user->MobilePhone);
Kryptonit3's avatar

@nathanrobjohn

by default, any data you add to the session stays the entire length of the session, you only have to add it once. If the session ever dies, so does the data. ( I think by default laravel sessions are 2 hours )

I am assuming you have a form on your site somewhere asking for this data and you post it to a controller method to persist it to the session.

What is the concern? Are you somehow manually carrying this data from each request?

If the concern is "Should i set these session values in my controllers?" then I would say it all depends. You are already handling the incoming request in your controller method, it shouldn't be an issue setting session data in the same request as processing the form.

Please or to participate in this conversation.