I’m writing a PHP application which makes use of a modular structure. To modules are basically folder names, and those folders contain PHP files.
It looks something like this:
/modules
/module1
/module2
/module3
My PHP application reads the content of the modules folder and saves the folder names to a table in my database. My modules table looks like this:
id | module | active | order
1 | module1 | 1 | 2
2 | module2 | 0 | 1
3 | module3 | 1 | 3
After the modules folder have been read and the folder names have been saved to the database, i then fetch the rows from the table again. That way i can easily see which modules are active, and in which order i have to show them.
The problem with this method is that i have to do all of the above everytime a page is visited. Because the system doesn’t know when a module have been added or removed from the module folder. So by “reading the folder / saving it to the DB / and reading the DB again” seems to give to most up to date version. But i can imagine that this method is inefficient. I think this can be done better.
There’s one solution i’ve came up with. But i still don’t think that this is the best solution.
Solution 1
When a user visits the site, then do all of the above. Read the modules folder, save the folder names to the table (after clearing the table first) and then get the rows from the table and store that in a session. When the user navigates through the site i can simply iterate over the session.
Cons: Removed/Added modules will only be visible when the browser is closed and re-opened.
Improvements?
I’m sure there’s a more elegant way of doing this. Anyone any ideas?
The flow should be the following:
When a normal user requests a page
I think this is the most efficient way.
Disclaimer: IMHO this is a waste of time. You’d better learn a real framework, maybe an MVC one like CakePHP, or one of those widely used CMS like Drupal, Joomla or WordPress. This way you’ll have 1. A working system 2. A secure system 3. Reusable knowledge. There’s no need to reinvent the wheel, even for learning purposes. You’ll eventually speed up the process by learning from other’s successes (and failures).