where is the best place to add code that runs on every page load
4 years ago
4 years ago
#1
I set up an app that allows for wildcard subdomains and want to load different content from database after checking the sub-domain. For example, test1.example.com and test2.example.com would show different content in the Welcome module. It is currently extracting the subdomain correctly in config.php and setting a variable. I need to use that variable to search the database and create an object from the data (like a member object) on every page load. Perhaps I can load the object in the session to avoid database reads and if the subdomain changes I can fetch again. I was considering adding this at the end of bootstrap.php. Is this the best place to add that type of code? If yes, then how would I query the database from bootstrap.php?
4 years ago
#2
My thoughts are it is not the best to query the database on bootstrap.php. There would be no security check. I think you can do it from the Welcome.php controller file. Using Segment.
4 years ago
#3
Yes, that's definitely something to consider since the subdomain can easily be changed by a user. I'm trying to avoid setting up a constructor at the top of every module controller to run this but that may be the only option. I'll wait a day or so to mark this resolved in case anyone has a better suggestion. Thanks for your input Dan.
4 years ago
#4
One thing you can do is create a module called _my_functions. In this put your functions and call them at the start of each page load. Similar to calling
4 years ago
#5
Unfortunately I'd have to run that within every method, which is what I want to avoid since it increases chance of an error if I forget. I don't want to touch the core but I think the best place is within Trongate.php __construct, right after: Then I can query every time to get the object. For security I'll make sure I clean SUBDOMAIN before I define it and allow only alphanumeric.
4 years ago
#6
If it makes sense to what you're doing then put the code in the Templates controller. Here's an example of what I'm doing to make sure users are logged in before rendering a view with the template. If it user isn't logged in then the _make_sure_allowed function sends the user to the login page.
4 years ago
#7
I put such code inside the constructor of the controller class. For every request the class is new create and the constructor is called. Example:
4 years ago
#8
Thanks everyone for your valuable input! I was considering this solution by djnordeen but it's too specific for my purpose and involves too much repetition with the very real possibility of me or a future developer forgetting to add it to every method. The solution proposed by horseman will load before anything else as well and it won't be overwritten with future updates. However, it still requires adding to every module, something I'm trying to avoid. I didn't realize cisnez had a similar issue (https://trongate.io/help_bar/thread/y2kkjgxmygdK). Querying for a record based on the subdomain in my template load function works perfectly and I only need to add it once. I can use array_merge to add the results to the $data variable before I load the template. Thanks cisnez!
4 years ago
#9
My solution