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

[eluser]eldrinofsoalim[/eluser]
Hi!

Can anyone refer me to guides, screencasts, articles, threads about Model-View-Controller Best Practices?

Basically I'm starting out in PHP and I've chosen Code Igniter as a terrific start especially for a beginner like me. I've understood that models handle the database CRUD, views handle the UI and controller serves as an intermediary between them.

But I'm having confusion on how to ORGANIZE all three.

Here are a few of my concerns:
1) What's the general rule on when you should create new controllers?
2) How should you organize controllers? Should you put controllers in a folder under the controller folder?
3) What's the proper way of creating an admin-member-public website? How do you organize the views and the controllers?
4) Do you really need only one model for all the website needs? I've read that only one model is really needed?
5) Tips for naming conventions?

As you can see, my questions are very very very amateur/noob/novice. But I hope you can see my willingness to learn.

Please refer to me articles or guides or screencasts or threads that have talked about these extensively, especially for beginners. I want to learn the FUNDAMENTALS.

Maybe from my post, you can infer what kind articles I also need to learn?

Looking for more mentors through this Forum,
eldrinofsoalim
#2

[eluser]umefarooq[/eluser]
Hi, here is answer to your questions

1) When you need different functionality from you existing controller create new controller, you have one controller name page which show only content, second controller name gallery for showing picture gallerias.

2) if you want to organize your controller use modular separation every module will have its own controller,view and models folder

3) for admin panel check this link http://philsturgeon.co.uk/blog/2009/07/C...odeIgniter use third method to create admin panel you will fine HMVC modular separation link also there

4) when you are dealing with different tables then you have to create different models, but you can optimize one model to get common records from tables, like get_by_id,get_many here is link for my_model you can extend your model with this and share the common task of model http://ellislab.com/forums/viewthread/143817/P10/

and here is session of screen cast
http://net.tutsplus.com/sessions/codeign...m-scratch/
#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.




Theme © iAndrew 2016 - Forum software by © MyBB