Master Controller with Dynamic Actions
Hi
I don’t know if this is relevant to your question, but all my models use a single master controller containing dynamic actions. With dynamic actions, I mean this:
All our normal CRUD actions do more-or-less the same thing and they don’t use much of the model’s info. For example: for a single table, the Create action would probably need the model-name and maybe the PK-name. But that is about all. The rest is done by the model, not the action.
So if you store the model-names and PK-names of all your tables in a separate meta-data-table, then you can re-use a single dynamic action to create all your models for all your tables.
The actions don’t use your modle’s field names directly, but retrieve them from the meta-data-table:
$idName = $meta_data_model->name_of_table_id;
$model->$idName = 5;
All my controllers are almost empty. They just have enough data to know what meta-data-table records to read. Then, all my controllers extend from the master controller - which contains all the dynamic actions.
All my actions in this master controller can handle ‘single table’, ‘one-to-many’, ‘many-to-many’, and ‘tree’ db structures.
Okay, my master controller has 2000 lines, but if you change one thing, then it works for your entire application. You don’t have to make the changes in all your other crud controllers as well. And you have zero duplicate code in your controllers.
But such a huge controller could load unused actions/functions as well. So an even better idea might be to store these dynamic actions in separate files/widgets/helpers. Then in your controllers, you only import/include the actions you need. But still, if any of these dynamic actions are updated, then they will be updated immediately in all controllers.
Controllers can obviously have their own unique actions as well. But such actions should be very few, because they will quickly become dynamic actions when you need to re-use them in other controllers as well.