01 YII2 Intro
01 YII2 Intro
TOC 2
• Yii is a high performance, component-based PHP
framework for rapidly developing modern Web
applications.
YII2 intro 3
• How does Yii Compare with Other Frameworks?
• Like most PHP frameworks, Yii implements the MVC
(Model-View-Controller) architectural pattern and
promotes code organization based on that pattern.
• Yii is a full-stack framework providing many proven and
ready-to-use features: query builders and ActiveRecord
for both relational and NoSQL databases; RESTful API
development support; multi-tier caching support;
• Yii is extremely extensible. You can customize or replace
nearly every piece of the core's code.
YII2 intro 4
• Yii applications are organized according to the model-
view-controller (MVC) architectural pattern.
• Models represent data, business logic and rules;
• views are output representation of models; and
• controllers take input and convert it to commands for
models and views.
• entry scripts: they are PHP scripts that are directly accessible by end users.
They are responsible for starting a request handling cycle.
• applications: they are globally accessible objects that manage application
components and coordinate them to fulfill requests.
• application components: they are objects registered with applications and
provide various services for fulfilling requests.
• modules: they are self-contained packages that contain complete MVC by
themselves. An application can be organized in terms of multiple modules.
• filters: they represent code that need to be invoked before and after the
actual handling of each request by controllers.
• widgets: they are objects that can be embedded in views. They may contain
controller logic and can be reused in different views.
THE MODEL 8
THE CONTROLLER 9
• The action first creates an EntryForm object.
• It then tries to populate the model with the data from
$_POST, provided in Yii by yii\web\Request::post().
• If the model is successfully populated (i.e., if the user has
submitted the HTML form), the action will call validate()
to make sure the values entered are valid.
THE CONTROLLER 10
THE VIEW (FORM) 11
• The view uses a powerful widget called ActiveForm to build
the HTML form.
• The begin() and end() methods of the widget render the
opening and closing form tags, respectively.
• Between the two method calls, input fields are created by the
field() method.
• The first input field is for the "name" data, and the second for
the "email" data.
• After the input fields, the yii\helpers\Html::submitButton()
method is called to generate a submit button.
APPLICATION STRUCTURE 14
Static structure of an
application. 15
• Each application has an entry script web/index.php which
is the only Web accessible PHP script in the application.
• The entry script takes an incoming request and creates an
application instance to handle it.
• The application resolves the request with the help of its
components, and dispatches the request to the MVC
elements.
• Widgets are used in the views to help build complex and
dynamic user interface elements.
Static structure of an
application. 16
REQUEST LIFECYCLE 17
1. A user makes a request to the entry script web/index.php.
2. The entry script loads the application configuration and creates an
application instance to handle the request.
3. The application resolves the requested route with the help of the request
application component.
4. The application creates a controller instance to handle the request.
5. The controller creates an action instance and performs the filters for the
action.
6. If any filter fails, the action is cancelled.
7. If all filters pass, the action is executed.
8. The action loads a data model, possibly from a database.
9. The action renders a view, providing it with the data model.
10. The rendered result is returned to the response application component.
11. The response component sends the rendered result to the user's browser.
REQUEST LIFECYCLE 18
Bootstrapping refers to the process of preparing the environment
before an application starts to resolve and process an incoming
request.
Bootstrapping is done in two places: the entry script and the
application.
BOOTSTRAPPING 19
In the constructor of the application, the following bootstrapping work is
done:
• preInit() is called, which configures some high priority application properties, such as
basePath.
• Register the error handler.
• Initialize application properties using the given application configuration.
• init() is called which in turn calls bootstrap() to run bootstrapping components.
• Include the extension manifest file vendor/yiisoft/extensions.php.
• Create and run bootstrap components declared by extensions.
• Create and run application components and/or modules that are declared in the application's
bootstrap property.
Because the bootstrapping work has to be done before handling every request, it is very
important to keep this process light and optimize it as much as possible.
BOOTSTRAPPING 20
• Try not to register too many bootstrapping components.
• A bootstrapping component is needed only if it wants to participate
the whole life cycle of requesting handling.
BOOTSTRAPPING 21
• Requests made to an application are represented in terms
of yii\web\Request objects which provide information
such as request parameters, HTTP headers, cookies, etc.
HTTP REQUESTS 22
• To get request parameters, you can call get() and post()
methods of the request component.
HTTP REQUESTS 23
HTTP REQUESTS 24
• You can get the HTTP method used by the current request
via the expression Yii::$app->request->method.
• A whole set of boolean properties is also provided for you
to check if the current method is of certain type.
HTTP HEADERS 28
• When an application finishes handling a request, it
generates a response object and sends it to the end user.
• The response object contains information such as the
HTTP status code, HTTP headers and body.
HTTP RESPONSES 29
• Example, to indicate the request is successfully handled, you
may set the status code to be 200, like the following:
• Yii::$app->response->statusCode = 200;
• If the exception that you want to throw is not among the above list,
you may create one by extending from yii\web\HttpException, or
directly throw it with a status code, for example,
HTTP HEADERS 33
• Most responses should have a body which gives the content that you
want to show to end users (usually with valid HTML).
• If you already have a formatted body string, you may assign it to the
yii\web\Response::$content property of the response. For example,
• $response = Yii::$app->response;
• $response->format = \yii\web\Response::FORMAT_JSON;
• $response->data = ['message' => 'hello world'];
HTTP REDIRECT 38
• Like browser redirection, file sending is another feature that relies on
specific HTTP headers. Yii provides a set of methods to support
various file sending needs. They all have built-in support for the
HTTP range header.
FLUSHING A RESPONSE 41