Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

This question already has an answer here:

I decided to get my hands wet and got the YII framework for PHP. I created my first application, then created new controller, model and view. Connected to database, got my record passed from controller to the view and printed the hello world.

I am confused now. If I have to do the same thing for each page, this seems like a nightmare to me. In each controller I have to do a lot of same operations - declare variables, and pass them to views. I also need to create models for each page and this is all confusing to me. In my idea the main goal of development is to avoid duplication, but what I see here is lots and lots of duplicated code.

Please advise and clarify. Maybe you could suggest a good reading about MVC and coding patterns and best practices in MVC. Because so far, it takes much more time to create a small site using MVC than using my own programming schema.

share|improve this question
4  
It might well take more time, in which case you might be better sticking to your own schema. MVC isn't the law or anything - use the right tool for the right job. –  Alan B Jun 26 '13 at 8:05
1  
related stackoverflow.com/questions/26685/… –  Simon Jun 26 '13 at 8:18
add comment

marked as duplicate by gnat, Dynamic, GlenH7, Kilian Foth, MichaelT Jun 26 '13 at 17:47

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

3 Answers

First thing to consider is the development method, read on test driven, behaviour driven etc. That makes a big different.

Second consideration is the choice of the development system. MVC you started with works for a lot of systems but not for all. If you need to make a chatbox for example there are much better technologies available (think NodeJS etc.) which make life more easy.

MVC in general fits CRUD work and can be extended in many ways but that is the basic approach. Within MVC there are lots of choices more to make. For example: Consider a page with lots of widgets. How would you load every widget. Is that a separate call to the server, it is an internal request, it is a database record etc. Think before starting a project.

MVC: Your thought is wrong:

I also need to create models for each page and this is all confusing to me.

Read a bit more about MVC and about some basic development approaches. For example the feeling that you have to do a lot of duplicate work is not true. In can give you an example of how we quickly setup a concept application. This does not work for all applications but for basic CRUD it works well.

Database

First we build the database tables, so we structure the data in a correct way with the required relations.

Run scaffold

We use CakePHP but I expect it is also possible with YII: We set a variable which gives us a basic interface so we can test out and enter some data:

http://book.cakephp.org/2.0/en/controllers/scaffolding.html

So you get a full (standard) interface where you can work with. It is not customisable but gives a good feeling for the project data.

Generate the models, controllers, views

When satisfied we generate the code for the models, controllers, views (yes: we don't write that out all by hand constantly).

http://book.cakephp.org/2.0/en/console-and-shells/code-generation-with-bake.html

That will give the same look of the application as the scaffold but it generated all needed files.

Start development

From here on you start development. Based on your wishes you can start with some layout implementation but it is also possible to first start with some other parts. This is really the step where you start development.

Example tasks:

  • Remove or add controllers (controllers are not 1-on-1 linked to models!)
  • Implement some CSS or stylesheet to get the feeling of the application
  • Bake a Plugin to structure code separately (take out the most complex parts)
  • Implement model methods to get the right data and call them from the views
  • Add custom relations between models
  • Implement authentication (don't start with this most of the time).
  • etc.

So, the first steps are thinking about structure (don't try to get the whole application at once) but only the core piece like users, login and the activity (tasks for example).

Then make them good working and good looking. Restructure the standard code you got.

In general it works best to have separate controllers, models, views etc. You are right there is some duplication in it but it is not really duplication. The view action might look the same but is not. It sends other data, requests a specific additional model method etc.

share|improve this answer
add comment

Because so far, it takes much more time to create a small site using MVC than using my own programming schema.

This is the crucial point in your post and about patterns in general. Most of the people out there have the impression that every problem is best solved by a pattern or in your case by an arrangement of several patterns (MVC). Unfortunately this is not the truth since one can imagine instances of the same problem in an infinite number of contexts, which partially differ heavily in the sense of complexity.

Patterns are proven to solve specific problems in specific contexts, so as you noticed already, MVC is not a pattern for creating small sites/development projects, but to achieve lose coupling between your model, view and controller that they are relatively easy exchangeable. If you do not plan to change any of these in the future, MVC is probably not the best choice.

So as a conclusion you see that applying patterns is dependent from:

  • the problem
  • the context
  • quality requirements
  • Do the advantages predominate the disadvantages of it

In the case of MVC you have the following advantages:

  • View can easily be exchanged
  • effort estimations for adjustments are easier, because you can locate the change impact better
  • Your model can in most of the cases be tested independently from controller and view

and disadvantages:

  • effort for implementation increases
  • with model changes all views on it have to be kept consistent
  • big effort upfront to set up the basic framework (interfaces, events) in order to satisfy the specific requirements
share|improve this answer
add comment

Why you won't use a simple framework like Laravel it's amazing trust me And it's a modern framework and not complicated like Yii

share|improve this answer
    
how does this answer the question asked? –  gnat Jun 26 '13 at 10:02
1  
@gnat - the OP doesn't like MVC because it is too complicated for a small site. This answer suggests that MVC isn't the problem, but maybe the particular framework. –  JeffO Jun 26 '13 at 12:33
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.