Also available in these languages:
DeutschEnglishEspañolFrançaisעִבְרִיתBahasa Indonesia日本語polskiPortuguêsRomâniaРусскийsvenska简体中文

Model-View-Controller (MVC)

Yii implements the model-view-controller (MVC) design pattern which is widely adopted in Web programming. MVC aims to separate business logic from user interface considerations so that developers can more easily change each part without affecting the other. In MVC, the model represents the information (the data) and the business rules; the view contains elements of the user interface such as text, form inputs; and the controller manages the communication between the model and the view.

Besides MVC, Yii also introduces a front-controller, called application, which represents the execution context of request processing. Application resolves the user request and dispatches it to an appropriate controller for further handling.

The following diagram shows the static structure of a Yii application:

Static structure of Yii application

Static structure of Yii application

A Typical Workflow

The following diagram shows a typical workflow of a Yii application when it is handling a user request:

A typical workflow of Yii application

A typical workflow of Yii application
  1. A user makes a request with the URL http://www.example.com/index.php?r=post/show&id=1 and the Web server handles the request by executing the bootstrap script index.php.
  2. The bootstrap script creates an application instance and runs it.
  3. The application obtains the detailed user request information from an application component named request.
  4. The application determines the requested controller and action with the help of an application component named urlManager. For this example, the controller is post which refers to the PostController class; and the action is show whose actual meaning is determined by the controller.
  5. The application creates an instance of the requested controller to further handle the user request. The controller determines that the action show refers to a method named actionShow in the controller class. It then creates and executes filters (e.g. access control, benchmarking) associated with this action. The action is executed if it is allowed by the filters.
  6. The action reads a Post model whose ID is 1 from the database.
  7. The action renders a view named show with the Post model.
  8. The view reads and displays the attributes of the Post model.
  9. The view executes some widgets.
  10. The view rendering result is embedded in a layout.
  11. The action completes the view rendering and displays the result to the user.
$Id: basics.mvc.txt 1622 2009-12-26 20:56:05Z qiang.xue $
If you find any typos or errors in the tutorial, please create a Yii ticket to report it. If it is a translation error, please create a Yiidoc ticket, instead. Thank you.

Total 9 comments:

#6
Why MVC?
by sapauljoseph at 12:39am on December 5, 2008.

Is MVC the only model that is great? Isn't there some other better models? Why all Frameworks should use MVC only?

#19
MVC is not a model
by jonah at 12:15pm on December 16, 2008.

@sapauljoseph

I don't think you understand what MVC is. MVC stands for model-view-controller. MVC is not a model. It simply means that your Yii app will mainly be written with models, views and controllers. Of course, it also has other things such a widgets, components and extensions.

#20
..
by jonah at 12:19pm on December 16, 2008.

Unless by "model" you mean the framework development pattern. Most php frameworks use MVC, but I think there is another type that runs on events. Never tried one, but since there are less frameworks of this type I assume it is not as good of a pattern.

#26
MVC as Architectural Pattern
by sapauljoseph at 12:26am on December 19, 2008.

@jonah

Thanks for pointing the typo. Yes, I meant MVC as an Architectural Pattern only. I know MVC is widely accepted but that doesn't mean it's the best out there right? And, I thought Yii runs on events also right? So, I don't think there is a separate architectural pattern that runs on events.

N-Tier is around for as long as I can remember and there are others like SOA and PAC.

Anyways, I still love Yii for it's very much similarity to the RoR and of course the eventual MVC...

Thanks for sharing your thoughts...

#337
MVC
by alexweber at 5:42pm on May 29, 2009.

MVC is used so much because it simplified application development and maintenance by separating the application into 3 logical components; model (business logic), view (look and feel) and controller (the "glue").

This is obviously a very simplified description but basically the MVC pattern allows for organized and maintainable code.

#483
Is Filter part of Controller?
by linzer at 7:30pm on July 20, 2009.

Questions: - Filter is now run in the controller? - can give example of usage in filter?

#803
Iconic
by faceyspacey at 11:38am on November 14, 2009.

It's very iconic how Yii is breaking things down here so perfectly. I've never seen a framework made so easy to understand or the MVC pattern in general. They're really on to something here. You can tell with all their language translations, etc.

#1061
ajsie
by ajsie at 2:08pm on February 2, 2010.

great work guys! this is one of the best if not the best framework in the php framework djungle! i really appreciate your work!

#1507
Lite on CPU
by izap at 5:48pm on May 19, 2010.

I found yii better than other framework. It's easy to understand and on the other hand lite on CPU resources. Great work. keep it up

Your Comment:

You may enter comment using Markdown syntax.

Please login with your forum account.
Note: you must have at least ONE forum post with your account.