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

dhcmega's avatar

Set session value before calling controller

Hi, this is my first post, so please let me know if I am not doing it properly. I have been trying to figure out what are the correct words to google, or event ask here, of what I need to do, but it seems that I am not succeding.

I am creating a multisite app, so when the user reaches the server, I need to check on the url the user typed, and go look on the database for all the info related to site, like title, layout, images, name, etc, etc

So, I think I need to query the database, and set a Session value before actually reaching the controller point. Should I create a Service Provider? Add it to the Kernel? Is a Session the right way to do it? I will need to add "site_id" to almost all my database queries.

Thanks!

0 likes
5 replies
willvincent's avatar

Are you sharing any content between sites, or everything separate? If the DB structure is the same for every site, you could probably get away with changing the DB connection in some middleware based on the requested hostname, or always use the same connection but change the table prefix for that connection, etc.

If it's just a matter of adding a where('site_id', '=', _something_) to every query, it'd be cool if you could just alter the query with an event listener, unfortunately I don't think there is an event that fires as a query is being prepared, there's one the fires immediately after a query has been run, but not before. :\

dhcmega's avatar

Hi, thanks for answering. I am not sharing content between sites. I am still trying to decide how to do it, with independent DBs or single DB. I think that independent DBs is an easier way of doing it, but it can became a nightmare to maintain and to track all the transactions. While sharing a DB will require to check for site_id for all DB transactions, user registration, productos, etc.

If I do it with single DB, as soon as a petition arrives, I have to get the domain and use it to fetch the site_id. Where should I put this code? so when the execution reaches the controller, the site id is ready to be used.

willvincent's avatar

You could do the single db, but prefixed tables, then it would behave as separate DBs, but only would need a single set of DB credentials and each site would have it's own tables within that DB..

In that case, you'd want to dynamically set the table prefix for the DB connection on the fly -- middleware ought to be sufficient. But be mindful that you'd have to run migrations for each new site. (Of course you'd have to do that with completely separate DBs too)

dhcmega's avatar

Yes, I understand. I am sticking to one DB and an id_site field at the moment. Prefixing tables is something in between both, but I still will have to do complex queries for getting all the transactions for example. Or create a cron process that export all the transaction to a common table. Thanks!

dhcmega's avatar

I added it to AppServiceProvider inside the boot() method.

Please or to participate in this conversation.