Vous êtes sur la page 1sur 4

YII Framework

1. 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 implementing MVC, Yii also introduces a front-controller, called Application, which
encapsulates the execution context for the processing of a request. Application collects some information
about a user request and then 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


1. A Typical Workflow
The following diagram shows a typical workflow of a Yii application when it is handling a user request:
Typical workflow of a 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 detailed user request information from an application 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.
2. Application
The application object encapsulates the execution context within which a request is processed. Its main
task is to collect some basic information about the request, and dispatch it to an appropriate controller for
further processing. It also serves as the central place for keeping application-level configuration settings.
For this reason, the application object is also called the front-controller.
The application object is instantiated as a singleton by the entry script. The application singleton can be
accessed at any place via Yii::app().
3. Controller
A controller is an instance of CController or of a class that extends CController. It is created by the
application object when the user requests it. When a controller runs, it performs the requested action,
which usually brings in the needed models and renders an appropriate view. An action, in its simplest
form, is just a controller class method whose name starts with action.
4. Model
A model is an instance of CModel or a class that extends CModel. Models are used to keep data and their
relevant business rules.
A model represents a single data object. It could be a row in a database table or an html form with user
input fields. Each field of the data object is represented by an attribute of the model. The attribute has a
label and can be validated against a set of rules.
Yii implements two kinds of models: Form models and active records. They both extend from the same
base class, CModel.
A form model is an instance of CFormModel. Form models are used to store data collected from user
input. Such data is often collected, used and then discarded. For example, on a login page, we can use a
form model to represent the username and password information that is provided by an end user. For
more details, please refer to Working with Forms
Active Record (AR) is a design pattern used to abstract database access in an object-oriented fashion.
Each AR object is an instance of CActiveRecord or of a subclass of that class, representing a single row
in a database table. The fields in the row are represented as properties of the AR object. Details about AR
can be found in Active Record.
5.View
A view is a PHP script consisting mainly of user interface elements. It can contain PHP statements, but it
is recommended that these statements should not alter data models and should remain relatively simple. In
the spirit of separating of logic and presentation, large chunks of logic should be placed in controllers or
models rather than in views.
5.1. Layout
Layout is a special view that is used to decorate views. It usually contains parts of a user interface that are
common among several views. For example, a layout may contain a header and a footer, and embed the
view in between, like this:
......header here......
<?php echo $content; ?>
......footer here......
5.2.Widget
A widget is an instance of CWidget or a child class of CWidget. It is a component that is mainly for
presentational purposes. A widget is usually embedded in a view script to generate a complex, yet self-
contained user interface. For example, a calendar widget can be used to render a complex calendar user
interface. Widgets facilitate better reusability in user interface code.
6. Extending Yii
Extending Yii is a common activity during development. For example, when you write a new controller,
you extend Yii by inheriting its CController class; when you write a new widget, you are extending
CWidget or an existing widget class. If the extended code is designed to be reused by third-party
developers, we call it an extension.
An extension usually serves for a single purpose. In Yii's terms, it can be classified as follows,
application component
behavior
widget
controller
action
filter
console command
validator: a validator is a component class extending CValidator.
helper: a helper is a class with only static methods. It is like global functions using the class name
as their namespace.
module: a module is a self-contained software unit that consists of models, views, controllers and
other supporting components. In many aspects, a module resembles to an application. The main
difference is that a module is inside an application. For example, we could have a module that
provides user management functionalities.
An extension can also be a component that does not fall into any of the above categories. As a matter of
fact, Yii is carefully designed such that nearly every piece of its code can be extended and customized to
fit for individual needs.

Vous aimerez peut-être aussi