2

I was a flat php programmer for past 2 years. Now I want to move to MVC architecture and so I am using codeigniter which looks very simple to start with. I want to know some of the best practices out there as I go developing in codeigniter.

I have a controller called building, a model called building_data and a view called building_view. Now i want to display list of building by checking lot of conditions. I am doing the following in flat PHP

  1. Get the list of buildings from database
  2. Split the result based on certain criteria A, B, C
  3. Display the result in section - A, section -B, and section-c as the HTML output.

Now in MVC I am doing the following

  1. Get the list of building in the database on building_data (model)
  2. Store the result from building_data in a $data array of the building controller
  3. Split the results based on criteria A,B,C in building_view and output the HTML (can i do the condition based classification of data (without using mysql queries) in view ? !My actual question)

Am I doing the right thing here without violating MVC architecture rules ?

Deepak
  • 6,684
  • 18
  • 69
  • 121
  • You're doing it right! Avoid having much of php codes in view, avoid having much database queries in controller. – WatsMyName Aug 09 '12 at 04:23
  • What do you mean by _condition based classification_? And what are those criteria? – Bhuvan Rikka Aug 09 '12 at 04:34
  • @BhuvanRikka Criteria like is the building blacklisted, is the building assigned to anyone, when is the building added, is the building listed in another table etc.. actually i cant get all these information in one query. Probably there are 5 or 6 queries running to generate this page. and i have to classify the data based on those 5 query results. – Deepak Aug 09 '12 at 04:53
  • 1
    @Sabin view is not a template – Dejan Marjanović Aug 09 '12 at 05:03
  • @Deepak , what is "flat PHP" ? – tereško Aug 09 '12 at 05:11
  • @tereško all in one page.. database query, application logic and html tags.. – Deepak Aug 09 '12 at 05:13
  • @Deepak , then before you even look at php frameworks, you should invest some time in learning OOP (and no, you cannot learn good OOP from php frameworks). Start by reading through [official documentation](http://uk.php.net/manual/en/language.oop5.php). It will give you some basic understanding of syntax. Then try to go through materials listed at the bottom of [this answer](http://stackoverflow.com/a/9855170/727208). You won't understand all of it, but few do on the first try. – tereško Aug 09 '12 at 05:18
  • @tereško Actually i have experience with Java development so i know OOP concepts. I have tried symfony which needs steep learning curve but when i looked in at codeigniter it got lot of flexibility. But i am afraid if i made a wrong choice.. – Deepak Aug 09 '12 at 05:35
  • Yeah, Symfony2 is one of best option when it comes to php frameworks (it's like being smartest kid in remedial class). But still, even if you have experience with OOP, i would recommend you to watch the lectures from the second link. – tereško Aug 09 '12 at 06:56
  • @tereško Thanks for that. I will go through those links... – Deepak Aug 09 '12 at 08:08

1 Answers1

7

MVC is a design pattern. Not an architecture.

But, if you are looking to learn best practices or MVC, then CodeIgniter is the wrong choice. It is filled with bad and outdated practices (PHP4 code fragments, global state and many other issues), and is not implementing anything even close to MVC pattern. It is more like a bad Rails clone for PHP.

Views are supposed to be objects, and not dumb templates. Your controller should tell model layer which building the user has selected, and then view acquires the details about current building and decides how to represent it all.

In proper MVC implementation, views are instances which contain presentation logic. They acquire information from model layer and then choose which templates to use for rendering the response or even if a HTML response is necessary. Maybe user actually requested data in JSON or XML format. Or maybe the only response, that view needs to send, is a HTTP header.

Also you should be aware that you cannot implement classical MVC pattern in web application (or at least it is extremely difficult and entails use of sockets and persistent model). Instead we use MVC-inspired patterns. The main difference between them is way how View gets to the information from Model layer.

  • in MVP and MVVM patterns the view is passive and receives data through controller-like structure (presenters or viewmodel, perspectively).
  • in Model2 MVC and HMVC patterns the view is active, it requests information directly from model layer.

Also there is third group: Rails-like implementation. It replaces model layer with collection of ActiveRecord based ORMs, pretend that view is a template, and "controller" is combination of presenter and view responsibilities.

tereško
  • 58,060
  • 25
  • 98
  • 150
  • After reading the title I came in here expecting the worst kind of answer; I was pleasantly surprised :} – orourkek Aug 10 '12 at 16:19