Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating and combining controllers in an MVC PHP web application
    text
    copied!<p>Over the last few weeks I have been studying the MVC design pattern for web applications using PHP. Taking a broad view I understand how the pattern works and why it is a very good way of implementing any sort of web application from small or large.</p> <p>As I understand it we have 3 distinct layers that talk to each other via the Controller like so:</p> <p>User Input ---> View ---> Controller ---> Model</p> <p>Site Output &lt;--- View &lt;--- Controller &lt;--- Model</p> <p>With my planned implementation I hope to have a model for each table in my database, and each model will have all the functions/logic necessary to manage this table. In turn each of these models will have an associated view. The Model and View will of course have a Controller which allows them to be used.</p> <p>Now this is easy to get my head around as each single, logical action requiring the database has been covered. However, what happens when a particular action requires the use of more than one table/model?</p> <p>The Admin side of the application isn’t likely to need more than one model at a time to maintain the database. The front-end or user side of the application is a different matter! Say I have a webpage that shows a list of articles for a particular section, a list of currently logged-in users and – borrowing an example from SO – site statistics such as a tag cloud.</p> <p>This one page would require at least 3 models in my planned design – Article, Users and Tags. </p> <p>Obviously my single controllers aren’t going to cut it. So what do I do?</p> <ol> <li><p>Create new monolithic controllers for my web pages?</p> <ul> <li>allow me to get the results I want</li> <li>would require a lot of duplicate coding</li> <li>really hard to maintain if changes required</li> </ul></li> <li><p>Create a “super” controller that manipulates smaller specific controllers</p> <ul> <li>allows me to get the results I want</li> <li>would be modular so changes to one script <em>shouldn’t</em> affect the others</li> <li>minimal code duplication</li> </ul></li> <li><p>Create [insert brilliant solution here]</p></li> </ol> <p>I am currently erring towards Option 2. Simply because it should in theory cut down on coding as all the necessary behaviour will exist in the smaller controllers - and everything will be easy to maintain. </p> <p>Perhaps it could look like this:</p> <p><strong>articlecontroller.php</strong></p> <pre><code>&lt;?php //Article Controller Script if($_GET['article'] = 'foo') { //magic necessary for displaying article "foo". } ?&gt; </code></pre> <p><strong>usercontroller.php</strong></p> <pre><code>&lt;?php //User Controller Script if($_GET['user'] = 'display') { //magic necessary for displaying users } ?&gt; </code></pre> <p><strong>supercontroller.php</strong></p> <pre><code>&lt;?php //"Super" Controller //magic for setting up page if(isset($_GET['article'])) { include('articlecontroller.php'); } if(isset($_GET['user'])) { include('usercontroller.php'); } ?&gt; </code></pre> <p>As you can see, my super controller looks at what that particular page requires and includes the necessary controllers which do the grunt work. <strong>NB:</strong> The code is just a quick and dirty example :)</p> <p>However, I am by no means a pro so that is why I am asking you, SO. Which option is preferred? If neither what Option 3 would you suggest? Any code snippets/examples would be lovely but not required.</p> <p>If you made it this far, thank you for your time.</p> <hr> <p>I thought I would update this question [and accept an answer - forgot to do that :P]. I took a look at the concept of Fat Models/Skinny Controllers and I like how it is done, so I will definitely be taking that directions.</p> <p>I have also taken the advice to look at some of the other frameworks and how they achieve their MVC patterns. There is rather a lot to take in! Still, I have learned quite a few new things along the way, so it was of great benefit. I may have more questions in the future, but for now I have more than enough to make a start.</p> <p>Thank you all for your input.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload