Welcome Guest, Not a member yet? Register   Sign In
MVC Best Practices
#3

[eluser]tonanbarbarian[/eluser]
i can answer some of your questions from a slightly different angle.

All of my answers are geared towards getting the best performance from your site.

1. technically you can put your entire site into a single controller. However you would not want to do that if you want your site to perform well because each page request will usually only need to use 1 method from the controller to display the page. If your controller has dozens of methods in it you could end up with a situation whereby PHP is having to parse and process a large code file but only needs to run 10% or less of the code to display that page.

So if you split your site into multiple controllers PHP has less code to process for any given page request and will therefore run faster.

Also from the point of view of code management having multiple controllers make it easier to find and work with code when you have to make changes or otherwise maintain the code.

most people split controller into logical groupings based on the actions on the site, or the tables in the database.
so for example you might have a users controller which handles all interactions with the user account, add, edit, delete, login, logout etc, and you might have a posts controller which handles all blog posts, view, comment, insert, edit, delete etc

2. the main advantage to having controllers in sub folders is that you can then reuse controller names if you need to.
this does not happen often.

3. there is no proper way. i will let others suggest about this because i am talking from the point of view of performance

4. as with the controller example above it is possible to have one model that does everything, but it is extra code that does not need to be processed on each page. As suggested have 1 model per table and that is a good start.


Overall my notes on performance might seem a bit over the top, but if you want to produce a site that performs at its optimum then the less code that has to be parsed for each page means less memory used and faster page processing. For a low use site it does not matter at all, until your hosting provider oversells their server and the load gets high, or suddenly your site becomes the next slashdot and you are getting a million hits per day.

You could take the idea to the extreme and have a separate controller for each page with its own unique model. However I do not recommend this, particularly for the model, as the idea behind using a model in the first place is to reuse common code.

However if you find your controller or model growing a bit large, you might want to considering moving some of the large code blocks from the controller or model into a library. Having a controller or model that has large sections of its code in libraries is another way to help reduce unnecessary code for page requests.


Messages In This Thread
MVC Best Practices - by El Forum - 04-11-2011, 05:33 PM
MVC Best Practices - by El Forum - 04-12-2011, 02:47 AM
MVC Best Practices - by El Forum - 04-13-2011, 04:07 PM



Theme © iAndrew 2016 - Forum software by © MyBB