Yii Guide 2.0 en PDF
Yii Guide 2.0 en PDF
to
Yii 2.0
http://www.yiiframework.com/doc/guide
Qiang Xue,
Alexander Makarov,
Carsten Brandt,
Klimov Paul,
and
many contributors from the Yii community
1 Introduction 1
1.1 What is Yii . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Upgrading from Version 1.1 . . . . . . . . . . . . . . . . . . . 2
2 Getting Started 13
2.1 What do you need to know . . . . . . . . . . . . . . . . . . . 13
2.2 Installing Yii . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.3 Running Applications . . . . . . . . . . . . . . . . . . . . . . 21
2.4 Saying Hello . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
2.5 Working with Forms . . . . . . . . . . . . . . . . . . . . . . . 28
2.6 Working with Databases . . . . . . . . . . . . . . . . . . . . . 34
2.7 Generating Code with Gii . . . . . . . . . . . . . . . . . . . . 40
2.8 Looking Ahead . . . . . . . . . . . . . . . . . . . . . . . . . . 46
3 Application Structure 49
3.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
3.2 Entry Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
3.3 Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
3.4 Application Components . . . . . . . . . . . . . . . . . . . . . 64
3.5 Controllers . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
3.6 Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
3.7 Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
3.8 Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
3.9 Filters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
3.10 Widgets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
3.11 Assets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
3.12 Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
iii
iv CONTENTS
9 Security 409
9.1 Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409
9.2 Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . 409
9.3 Authorization . . . . . . . . . . . . . . . . . . . . . . . . . . . 413
9.4 Working with Passwords . . . . . . . . . . . . . . . . . . . . . 431
CONTENTS v
10 Caching 443
10.1 Caching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443
10.2 Data Caching . . . . . . . . . . . . . . . . . . . . . . . . . . . 443
10.3 Fragment Caching . . . . . . . . . . . . . . . . . . . . . . . . 452
10.4 Page Caching . . . . . . . . . . . . . . . . . . . . . . . . . . . 456
10.5 HTTP Caching . . . . . . . . . . . . . . . . . . . . . . . . . . 457
13 Testing 493
13.1 Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 493
13.2 Testing environment setup . . . . . . . . . . . . . . . . . . . . 494
13.3 Unit Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 495
13.4 Functional Tests . . . . . . . . . . . . . . . . . . . . . . . . . 496
13.5 Acceptance Tests . . . . . . . . . . . . . . . . . . . . . . . . . 496
13.6 Fixtures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 497
15 Widgets 569
vi CONTENTS
16 Helpers 573
16.1 Helpers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 573
16.2 ArrayHelper . . . . . . . . . . . . . . . . . . . . . . . . . . . . 574
16.3 Html helper . . . . . . . . . . . . . . . . . . . . . . . . . . . . 583
16.4 Url Helper . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 590
Chapter 1
Introduction
1
2 CHAPTER 1. INTRODUCTION
definitive guide to learn about them all. Chances are that some features you
previously had to develop for yourself are now part of the core code.
1.2.1 Installation
Yii 2.0 fully embraces Composer4 , the de facto PHP package manager. In-
stallation of the core framework, as well as extensions, are handled through
Composer. Please refer to the Installing Yii section to learn how to install
Yii 2.0. If you want to create new extensions, or turn your existing 1.1 exten-
sions into 2.0-compatible extensions, please refer to the Creating Extensions
section of the guide.
1.2.3 Namespace
The most obvious change in Yii 2.0 is the use of namespaces. Almost every
core class is namespaced, e.g., yii\web\Request. The “C” prefix is no longer
used in class names. The naming scheme now follows the directory structure.
For example, yii\web\Request indicates that the corresponding class file is web
/Request.php under the Yii framework folder.
4
https://getcomposer.org/
5
http://php.net/manual/en/language.namespaces.php
6
http://php.net/manual/en/functions.anonymous.php
7
http://php.net/manual/en/book.spl.php
8
http://php.net/manual/en/language.oop5.late-static-bindings.php
9
http://php.net/manual/en/book.datetime.php
10
http://php.net/manual/en/language.oop5.traits.php
11
http://php.net/manual/en/book.intl.php
4 CHAPTER 1. INTRODUCTION
(You can use any core class without explicitly including that class file,
thanks to the Yii class loader.)
parent::__construct($config);
}
In the above, the last parameter of the constructor must take a configuration
array that contains name-value pairs for initializing the properties at the end
of the constructor. You can override the init() method to do initialization
work that should be done after the configuration has been applied.
By following this convention, you will be able to create and configure
new objects using a configuration array:
$object = Yii::createObject([
’class’ => ’MyClass’,
’property1’ => ’abc’,
’property2’ => ’cde’,
], [$param1, $param2]);
1.2.6 Events
In Yii 1, events were created by defining an on-method (e.g., onBeforeSave).
In Yii 2, you can now use any event name. You trigger an event by calling
the trigger() method:
$event = new \yii\base\Event;
$component->trigger($eventName, $event);
There are many enhancements to the event features. For more details, please
refer to the Events section.
1.2.8 Views
The most significant change about views in Yii 2 is that the special variable
$this in a view no longer refers to the current controller or widget. Instead,
$this now refers to a view object, a new concept introduced in 2.0. The view
object is of type yii\web\View, which represents the view part of the MVC
pattern. If you want to access the controller or widget in a view, you can
use $this->context.
To render a partial view within another view, you use $this->render(),
not $this->renderPartial(). The call to render also now has to be explicitly
6 CHAPTER 1. INTRODUCTION
echoed, as the render() method returns the rendering result, rather than
directly displaying it. For example:
echo $this->render(’_item’, [’item’ => $item]);
Besides using PHP as the primary template language, Yii 2.0 is also equipped
with official support for two popular template engines: Smarty and Twig.
The Prado template engine is no longer supported. To use these template
engines, you need to configure the view application component by setting the
View::$renderers property. Please refer to the Template Engines section
for more details.
1.2.9 Models
Yii 2.0 uses yii\base\Model as the base model, similar to CModel in 1.1.
The class CFormModel has been dropped entirely. Instead, in Yii 2 you should
extend yii\base\Model to create a form model class.
Yii 2.0 introduces a new method called scenarios() to declare supported
scenarios, and to indicate under which scenario an attribute needs to be
validated, can be considered as safe or not, etc. For example:
public function scenarios()
{
return [
’backend’ => [’email’, ’role’],
’frontend’ => [’email’, ’!role’],
];
}
In the above, two scenarios are declared: backend and frontend. For the
backend scenario, both the email and role attributes are safe, and can be
massively assigned. For the frontend scenario, email can be massively assigned
while role cannot. Both email and role should be validated using rules.
The rules() method is still used to declare the validation rules. Note
that due to the introduction of scenarios(), there is no longer an unsafe
validator.
In most cases, you do not need to override scenarios() if the rules()
method fully specifies the scenarios that will exist, and if there is no need to
declare unsafe attributes.
To learn more details about models, please refer to the Models section.
1.2.10 Controllers
Yii 2.0 uses yii\web\Controller as the base controller class, which is similar
to CController in Yii 1.1. yii\base\Action is the base class for action classes.
The most obvious impact of these changes on your code is that a con-
troller action should return the content that you want to render instead of
echoing it:
1.2. UPGRADING FROM VERSION 1.1 7
Please refer to the Controllers section for more details about controllers.
1.2.11 Widgets
Yii 2.0 uses yii\base\Widget as the base widget class, similar to CWidget in
Yii 1.1.
To get better support for the framework in IDEs, Yii 2.0 introduces a new
syntax for using widgets. The static methods begin(), end(), and widget()
have been introduced, to be used like so:
use yii\widgets\Menu;
use yii\widgets\ActiveForm;
1.2.12 Themes
Themes work completely differently in 2.0. They are now based on a path
mapping mechanism that maps a source view file path to a themed view
file path. For example, if the path map for a theme is [’/web/views’ => ’/
web/themes/basic’], then the themed version for the view file /web/views/site
/index.php will be /web/themes/basic/site/index.php. For this reason, themes
can now be applied to any view file, even a view rendered outside of the
context of a controller or a widget.
Also, there is no more CThemeManager component. Instead, theme is a con-
figurable property of the view application component.
Please refer to the Theming section for more details.
8 CHAPTER 1. INTRODUCTION
1.2.14 I18N
Yii 2.0 removes the built-in date formatter and number formatter pieces in
favor of the PECL intl PHP module12 .
Message translation is now performed via the i18n application compon-
ent. This component manages a set of message sources, which allows you to
use different message sources based on message categories.
Please refer to the Internationalization section for more details.
12
http://pecl.php.net/package/intl
1.2. UPGRADING FROM VERSION 1.1 9
1.2.16 Assets
Yii 2.0 introduces a new concept called asset bundle that replaces the script
package concept found in Yii 1.1.
An asset bundle is a collection of asset files (e.g. JavaScript files, CSS
files, image files, etc.) within a directory. Each asset bundle is represented
as a class extending yii\web\AssetBundle. By registering an asset bundle
via yii\web\AssetBundle::register(), you make the assets in that bundle
accessible via the Web. Unlike in Yii 1, the page registering the bundle will
automatically contain the references to the JavaScript and CSS files specified
in that bundle.
Please refer to the Managing Assets section for more details.
1.2.17 Helpers
Yii 2.0 introduces many commonly used static helper classes, including.
• yii\helpers\Html
• yii\helpers\ArrayHelper
• yii\helpers\StringHelper
• yii\helpers\FileHelper
• yii\helpers\Json
Please refer to the Helper Overview section for more details.
1.2.18 Forms
Yii 2.0 introduces the field concept for building a form using yii\widgets
\ActiveForm. A field is a container consisting of a label, an input, an error
message, and/or a hint text. A field is represented as an ActiveField object.
Using fields, you can build a form more cleanly than before:
<?php $form = yii\widgets\ActiveForm::begin(); ?>
<?= $form->field($model, ’username’) ?>
<?= $form->field($model, ’password’)->passwordInput() ?>
<div class="form-group">
<?= Html::submitButton(’Login’) ?>
</div>
<?php yii\widgets\ActiveForm::end(); ?>
$command = $query->createCommand();
$sql = $command->sql;
$rows = $command->queryAll();
Best of all, such query building methods can also be used when working with
Active Record.
Please refer to the Query Builder section for more details.
Now you can use $customer->orders to access a customer’s orders from the
related table. You can also use the following code to perform an on-the-fly
relational query with a customized query condition:
$orders = $customer->getOrders()->andWhere(’status=1’)->all();
When eager loading a relation, Yii 2.0 does it differently from 1.1. In partic-
ular, in 1.1 a JOIN query would be created to select both the primary and
the relational records. In Yii 2.0, two SQL statements are executed without
using JOIN: the first statement brings back the primary records and the
second brings back the relational records by filtering with the primary keys
of the primary records.
1.2. UPGRADING FROM VERSION 1.1 11
Another change is that you can’t define attribute default values through
public properties anymore. If you need those, you should set them in the
init method of your record class.
public function init()
{
parent::init();
$this->status = self::STATUS_NEW;
}
use yii\db\ActiveRecord;
use yii\base\Behavior;
Please refer to the Url manager docs section for more details.
An important change in the naming convention for routes is that camel
case names of controllers and actions are now converted to lower case where
each word is separated by a hypen, e.g. the controller id for the CamelCaseController
will be camel-case. See the section about controller IDs and action IDs for
more details.
13
https://github.com/yiisoft/yii2-app-advanced/blob/master/docs/guide/
README.md
Chapter 2
Getting Started
2.1.1 PHP
Yii is a PHP framework so make sure you read and understand language
reference1 .
1
http://php.net/manual/en/langref.php
2
https://code.tutsplus.com/tutorials/object-oriented-php-for-beginners--net-12762
3
https://getcomposer.org/
13
14 CHAPTER 2. GETTING STARTED
composer.phar in the local directory instead, you have to adjust the example
commands accordingly.
If you had Composer already installed before, make sure you use an up
to date version. You can update Composer by running composer self-update.
Installing Yii
With Composer installed, you can install Yii application template by running
the following command under a Web-accessible folder:
composer create-project --prefer-dist yiisoft/yii2-app-basic basic
This will install the latest stable version of Yii application template in a
directory named basic. You can choose a different directory name if you
want.
Note that the development version of Yii should not be used for
production as it may break your running code.
11
https://getcomposer.org/doc/articles/troubleshooting.md#
api-rate-limit-and-oauth-tokens
12
https://getcomposer.org/doc/articles/troubleshooting.md
13
https://getcomposer.org/doc/04-schema.md#minimum-stability
16 CHAPTER 2. GETTING STARTED
3. Modify the config/web.php file by entering a secret key for the cookieValidationKey
configuration item (this is done automatically if you are installing Yii
using Composer):
// !!! insert a secret key in the following (if it is empty) - this is
required by cookie validation
’cookieValidationKey’ => ’enter your secret key here’,
"replace": {
"bower-asset/jquery": ">=1.11.0",
"bower-asset/inputmask": ">=3.2.0",
"bower-asset/punycode": ">=1.3.0",
"bower-asset/yii2-pjax": ">=2.0.0"
},
After installation is done, either configure your web server (see next sec-
tion) or use the built-in PHP web server20 by running the following console
command while in the project web directory:
You can use your browser to access the installed Yii application with the
following URL:
http://localhost:8080/
20
https://secure.php.net/manual/en/features.commandline.webserver.php
18 CHAPTER 2. GETTING STARTED
You should configure your PHP installation so that it meets the minimum
requirements of Yii. Most importantly, you should have PHP 5.4 or above.
Ideally latest PHP 7. You should also install the PDO PHP Extension21 and
a corresponding database driver (such as pdo_mysql for MySQL databases),
if your application needs a database.
The application installed according to the above instructions should work out
of box with either an Apache HTTP server22 or an Nginx HTTP server23 ,
21
http://www.php.net/manual/en/pdo.installation.php
22
http://httpd.apache.org/
23
http://nginx.org/
2.2. INSTALLING YII 19
on Windows, Mac OS X, or Linux running PHP 5.4 or higher. Yii 2.0 is also
compatible with facebook’s HHVM24 . However, there are some edge cases
where HHVM behaves different than native PHP, so you have to take some
extra care when using HHVM.
On a production server, you may want to configure your Web server
so that the application can be accessed via the URL http://www.example.com
/index.php instead of http://www.example.com/basic/web/index.php. Such con-
figuration requires pointing the document root of your Web server to the
basic/web folder. You may also want to hide index.php from the URL, as de-
scribed in the Routing and URL Creation section. In this subsection, you’ll
learn how to configure your Apache or Nginx server to achieve these goals.
<Directory "path/to/basic/web">
# use mod_rewrite for pretty URL support
RewriteEngine on
# If a directory or a file exists, use the request directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Otherwise forward the request to index.php
RewriteRule . index.php
24
http://hhvm.com/
20 CHAPTER 2. GETTING STARTED
# ...other settings...
</Directory>
server_name mysite.test;
root /path/to/basic/web;
index index.php;
access_log /path/to/basic/log/access.log;
error_log /path/to/basic/log/error.log;
location / {
# Redirect everything that isn’t a real file to index.php
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass 127.0.0.1:9000;
#fastcgi_pass unix:/var/run/php5-fpm.sock;
try_files $uri =404;
25
http://wiki.nginx.org/
26
http://php.net/install.fpm
2.3. RUNNING APPLICATIONS 21
location ~* /\. {
deny all;
}
}
When using this configuration, you should also set cgi.fix_pathinfo=0 in the
php.ini file in order to avoid many unnecessary system stat() calls.
Also note that when running an HTTPS server, you need to add fastcgi_param
HTTPS on; so that Yii can properly detect if a connection is secure.
Note that unlike framework itself, after project template is installed it’s all
yours. You’re free to add or delete code and overall modify it as you need.
2.3.1 Functionality
The basic application installed contains four pages:
• the homepage, displayed when you access the URL http://hostname/
index.php,
• the “About” page,
• the “Contact” page, which displays a contact form that allows end users
to contact you via email,
• and the “Login” page, which displays a login form that can be used to
authenticate end users. Try logging in with “admin/admin”, and you
will find the “Login” main menu item will change to “Logout”.
These pages share a common header and footer. The header contains a main
menu bar to allow navigation among different pages.
You should also see a toolbar at the bottom of the browser window.
This is a useful debugger tool27 provided by Yii to record and display a
27
https://github.com/yiisoft/yii2-debug/blob/master/docs/guide/README.md
22 CHAPTER 2. GETTING STARTED
The most important directories and files in your application are (assuming
the application’s root directory is basic):
In general, the files in the application can be divided into two types: those
under basic/web and those under other directories. The former can be directly
accessed via HTTP (i.e., in a browser), while the latter can not and should
not be.
Yii implements the model-view-controller (MVC)28 architectural pattern,
which is reflected in the above directory organization. The models directory
contains all model classes, the views directory contains all view scripts, and
the controllers directory contains all controller classes.
The following diagram shows the static structure of an application.
28
http://wikipedia.org/wiki/Model-view-controller
2.3. RUNNING APPLICATIONS 23
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.
3. The application resolves the requested route with the help of the re-
quest application component.
5. The controller creates an action instance and performs the filters for
the action.
11. The response component sends the rendered result to the user’s browser.
2.4. SAYING HELLO 25
Info: Actions are the objects that end users can directly refer to
for execution. Actions are grouped by controllers. The execution
result of an action is the response that an end user will receive.
Actions must be declared in controllers. For simplicity, you may declare the
say action in the existing SiteController. This controller is defined in the
class file controllers/SiteController.php. Here is the start of the new action:
<?php
namespace app\controllers;
use yii\web\Controller;
In the above code, the say action is defined as a method named actionSay
in the SiteController class. Yii uses the prefix action to differentiate action
methods from non-action methods in a controller class. The name after the
action prefix maps to the action’s ID.
26 CHAPTER 2. GETTING STARTED
When it comes to naming your actions, you should understand how Yii
treats action IDs. Action IDs are always referenced in lower case. If an action
ID requires multiple words, they will be concatenated by dashes (e.g., create
-comment). Action method names are mapped to action IDs by removing any
dashes from the IDs, capitalizing the first letter in each word, and prefixing
the resulting string with action. For example, the action ID create-comment
corresponds to the action method name actionCreateComment.
The action method in our example takes a parameter $message, whose
value defaults to "Hello" (in exactly the same way you set a default value for
any function or method argument in PHP). When the application receives a
request and determines that the say action is responsible for handling said
request, the application will populate this parameter with the same named
parameter found in the request. In other words, if the request includes a
message parameter with a value of "Goodbye", the $message variable within the
action will be assigned that value.
Within the action method, render() is called to render a view file named
say. The message parameter is also passed to the view so that it can be used
there. The rendering result is returned by the action method. That result
will be received by the application and displayed to the end user in the
browser (as part of a complete HTML page).
The say view should be saved in the file views/site/say.php. When the method
render() is called in an action, it will look for a PHP file named as views/
ControllerID/ViewName.php.
Note that in the above code, the message parameter is HTML-encoded
before being printed. This is necessary as the parameter comes from an
end user, making it vulnerable to cross-site scripting (XSS) attacks29 by
embedding malicious JavaScript code in the parameter.
Naturally, you may put more content in the say view. The content can
consist of HTML tags, plain text, and even PHP statements. In fact, the
say view is just a PHP script that is executed by the render() method. The
content printed by the view script will be returned to the application as the
response’s result. The application will in turn output this result to the end
user.
29
http://en.wikipedia.org/wiki/Cross-site_scripting
2.4. SAYING HELLO 27
This URL will result in a page displaying “Hello World”. The page shares
the same header and footer as the other application pages.
If you omit the message parameter in the URL, you would see the page
display just “Hello”. This is because message is passed as a parameter to the
actionSay() method, and when it is omitted, the default value of "Hello" will
be used instead.
Info: The new page shares the same header and footer as other
pages because the render() method will automatically embed
the result of the say view in a so-called layout which in this case
is located at views/layouts/main.php.
The r parameter in the above URL requires more explanation. It stands for
route, an application wide unique ID that refers to an action. The route’s
format is ControllerID/ActionID. When the application receives a request, it
will check this parameter, using the ControllerID part to determine which
controller class should be instantiated to handle the request. Then, the
controller will use the ActionID part to determine which action should be
instantiated to do the real work. In this example case, the route site/say
will be resolved to the SiteController controller class and the say action. As
a result, the SiteController::actionSay() method will be called to handle the
request.
28 CHAPTER 2. GETTING STARTED
Info: Like actions, controllers also have IDs that uniquely identify
them in an application. Controller IDs use the same naming rules
as action IDs. Controller class names are derived from controller
IDs by removing dashes from the IDs, capitalizing the first letter
in each word, and suffixing the resulting string with the word
Controller. For example, the controller ID post-comment corres-
ponds to the controller class name PostCommentController.
2.4.4 Summary
In this section, you have touched the controller and view parts of the MVC
architectural pattern. You created an action as part of a controller to handle
a specific request. And you also created a view to compose the response’s
content. In this simple example, no model was involved as the only data
used was the message parameter.
You have also learned about routes in Yii, which act as the bridge between
user requests and controller actions.
In the next section, you will learn how to create a model, and add a new
page containing an HTML form.
namespace app\models;
use Yii;
use yii\base\Model;
2.5. WORKING WITH FORMS 29
The class extends from yii\base\Model, a base class provided by Yii, com-
monly used to represent form data.
The EntryForm class contains two public members, name and email, which are
used to store the data entered by the user. It also contains a method named
rules(), which returns a set of rules for validating the data. The validation
rules declared above state that
• both the name and email values are required
• the email data must be a syntactically valid email address
If you have an EntryForm object populated with the data entered by a user,
you may call its validate() method to trigger the data validation routines.
A data validation failure will set the hasErrors property to true, and you
may learn what validation errors occurred through errors.
<?php
$model = new EntryForm();
$model->name = ’Qiang’;
$model->email = ’bad’;
if ($model->validate()) {
// Good!
} else {
// Failure!
// Use $model->getErrors()
}
<?php
namespace app\controllers;
use Yii;
use yii\web\Controller;
use app\models\EntryForm;
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.
<ul>
<li><label>Name</label>: <?= Html::encode($model->name) ?></li>
<li><label>Email</label>: <?= Html::encode($model->email) ?></li>
</ul>
The entry view displays an HTML form. It should be stored in the file
views/site/entry.php.
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
?>
<?php $form = ActiveForm::begin(); ?>
<div class="form-group">
<?= Html::submitButton(’Submit’, [’class’ => ’btn btn-primary’]) ?>
</div>
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.
You will see a page displaying a form with two input fields. In front of
each input field, a label indicates what data is to be entered. If you click
the submit button without entering anything, or if you do not provide a
valid email address, you will see an error message displayed next to each
problematic input field.
After entering a valid name and email address and clicking the submit
button, you will see a new page displaying the data that you just entered.
2.5. WORKING WITH FORMS 33
Magic Explained
You may wonder how the HTML form works behind the scene, because it
seems almost magical that it can display a label for each input field and show
error messages if you do not enter the data correctly without reloading the
page.
Yes, the data validation is initially done on the client-side using JavaS-
cript, and secondarily performed on the server-side via PHP. yii\widgets
\ActiveForm is smart enough to extract the validation rules that you have
declared in EntryForm, turn them into executable JavaScript code, and use the
JavaScript to perform data validation. In case you have disabled JavaScript
on your browser, the validation will still be performed on the server-side, as
shown in the actionEntry() method. This ensures data validity in all circum-
stances.
The labels for input fields are generated by the field() method, using the
property names from the model. For example, the label Name will be generated
for the name property.
You may customize a label within a view using the following code:
<?= $form->field($model, ’name’)->label(’Your Name’) ?>
<?= $form->field($model, ’email’)->label(’Your Email’) ?>
Info: Yii provides many such widgets to help you quickly build
complex and dynamic views. As you will learn later, writing
a new widget is also extremely easy. You may want to turn
much of your view code into reusable widgets to simplify view
development in future.
2.5.5 Summary
In this section of the guide, you have touched every part in the MVC archi-
tectural pattern. You have learned how to create a model class to represent
the user data and validate said data.
You have also learned how to get data from users and how to display
data back in the browser. This is a task that could take you a lot of time
when developing an application, but Yii provides powerful widgets to make
this task very easy.
In the next section, you will learn how to work with databases, which
are needed in nearly every application.
34 CHAPTER 2. GETTING STARTED
This section will describe how to create a new page that displays country
data fetched from a database table named country. To achieve this goal, you
will configure a database connection, create an Active Record class, define
an action, and create a view.
Through this tutorial, you will learn how to:
• configure a DB connection,
• define an Active Record class,
• query data using the Active Record class,
• display data in a view in a paginated fashion.
Note that in order to finish this section, you should have basic knowledge and
experience using databases. In particular, you should know how to create a
database, and how to execute SQL statements using a DB client tool.
To begin, create a database named yii2basic, from which you will fetch
data in your application. You may create an SQLite, MySQL, PostgreSQL,
MSSQL or Oracle database, as Yii has built-in support for many database
applications. For simplicity, MySQL will be assumed in the following de-
scription.
Next, create a table named country in the database, and insert some
sample data. You may run the following SQL statements to do so:
At this point, you have a database named yii2basic, and within it a country
table with three columns, containing ten rows of data.
2.6. WORKING WITH DATABASES 35
return [
’class’ => ’yii\db\Connection’,
’dsn’ => ’mysql:host=localhost;dbname=yii2basic’,
’username’ => ’root’,
’password’ => ’’,
’charset’ => ’utf8’,
];
If you need to work with databases support for which isn’t bundled with Yii,
check the following extensions:
• Informix32
• IBM DB233
• Firebird34
31
http://www.php.net/manual/en/book.pdo.php
32
https://github.com/edgardmessias/yii2-informix
33
https://github.com/edgardmessias/yii2-ibm-db2
34
https://github.com/edgardmessias/yii2-firebird
36 CHAPTER 2. GETTING STARTED
namespace app\models;
use yii\db\ActiveRecord;
Using the Country class, you can easily manipulate data in the country table,
as shown in these snippets:
use app\models\Country;
// get all rows from the country table and order them by "name"
$countries = Country::find()->orderBy(’name’)->all();
<?php
namespace app\controllers;
use yii\web\Controller;
use yii\data\Pagination;
use app\models\Country;
$countries = $query->orderBy(’name’)
->offset($pagination->offset)
->limit($pagination->limit)
->all();
return $this->render(’index’, [
’countries’ => $countries,
’pagination’ => $pagination,
]);
}
}
the following:
<?php
use yii\helpers\Html;
use yii\widgets\LinkPager;
?>
<h1>Countries</h1>
<ul>
<?php foreach ($countries as $country): ?>
<li>
<?= Html::encode("{$country->code} ({$country->name})") ?>:
<?= $country->population ?>
</li>
<?php endforeach; ?>
</ul>
The view has two sections relative to displaying the country data. In the
first part, the provided country data is traversed and rendered as an un-
ordered HTML list. In the second part, a yii\widgets\LinkPager widget
is rendered using the pagination information passed from the action. The
LinkPager widget displays a list of page buttons. Clicking on any of them will
refresh the country data in the corresponding page.
To see how all of the above code works, use your browser to access the
following URL:
http://hostname/index.php?r=country%2Findex
2.6. WORKING WITH DATABASES 39
At first, you will see a page showing five countries. Below the countries,
you will see a pager with four buttons. If you click on the button “2”, you
will see the page display another five countries in the database: the second
page of records. Observe more carefully and you will find that the URL in
the browser also changes to
http://hostname/index.php?r=country%2Findex&page=2
2.6.7 Summary
In this section, you learned how to work with a database. You also learned
how to fetch and display data in pages with the help of yii\data\Pagination
40 CHAPTER 2. GETTING STARTED
and yii\widgets\LinkPager.
In the next section, you will learn how to use the powerful code gen-
eration tool, called Gii35 , to help you rapidly implement some commonly
required features, such as the Create-Read-Update-Delete (CRUD) opera-
tions for working with the data in a database table. As a matter of fact, the
code you have just written can all be automatically generated in Yii using
the Gii tool.
if (YII_ENV_DEV) {
$config[’bootstrap’][] = ’gii’;
$config[’modules’][’gii’] = [
’class’ => ’yii\gii\Module’,
];
}
Thanks to that line, your application is in development mode, and will have
already enabled Gii, per the above configuration. You can now access Gii
via the following URL:
http://hostname/index.php?r=gii
Note: If you are accessing Gii from a machine other than loc-
alhost, the access will be denied by default for security purpose.
You can configure Gii to add the allowed IP addresses as follows,
’gii’ => [
’class’ => ’yii\gii\Module’,
’allowedIPs’ => [’127.0.0.1’, ’::1’, ’192.168.0.*’, ’
192.168.178.20’] // adjust this to your needs
],
To use Gii to generate an Active Record class, select the “Model Generator”
(by clicking the link on the Gii index page). Then fill out the form as follows:
• Table Name: country
• Model Class: Country
42 CHAPTER 2. GETTING STARTED
When using Gii, if you have already created the same file and would be
overwriting it, click the diff button next to the file name to see the differences
between the code to be generated and the existing version.
2.7. GENERATING CODE WITH GII 43
When overwriting an existing file, check the box next to “overwrite” and
then click the “Generate” button. If creating a new file, you can just click
“Generate”.
Next, you will see a confirmation page indicating the code has been
successfully generated. If you had an existing file, you’ll also see a message
indicating that it was overwritten with the newly generated code.
Next, click on the “Preview” button. You will see a list of files to be
generated, as shown below.
To see how it works, use your browser to access the following URL:
http://hostname/index.php?r=country%2Findex
You will see a data grid showing the countries from the database table. You
may sort the grid, or filter it by entering filter conditions in the column
headers.
For each country displayed in the grid, you may choose to view its details,
update it, or delete it. You may also click on the “Create Country” button
on top of the grid to be provided with a form for creating a new country.
46 CHAPTER 2. GETTING STARTED
The following is the list of the files generated by Gii, in case you want to
investigate how these features are implemented, or to customize them:
• Controller: controllers/CountryController.php
• Models: models/Country.php and models/CountrySearch.php
• Views: views/country/*.php
2.7.5 Summary
In this section, you have learned how to use Gii to generate the code that
implements complete CRUD functionality for content stored in a database
table.
paginated fashion. You have also learned how to use Gii39 to generate code
automatically. Using Gii for code generation turns the bulk of your Web
development process into a task as simple as just filling out some forms.
This section will summarize the Yii resources available to help you be
more productive when using the framework.
• Documentation
– The Definitive Guide40 : As the name indicates, the guide precisely
defines how Yii should work and provides general guidance about
using Yii. It is the single most important Yii tutorial, and one
that you should read before writing any Yii code.
– The Class Reference41 : This specifies the usage of every class
provided by Yii. It should be mainly used when you are writ-
ing code and want to understand the usage of a particular class,
method, property. Usage of the class reference is best only after
a contextual understanding of the entire framework.
– The Wiki Articles42 : The wiki articles are written by Yii users
based on their own experiences. Most of them are written like
cookbook recipes, and show how to solve particular problems us-
ing Yii. While the quality of these articles may not be as good as
the Definitive Guide, they are useful in that they cover broader
topics and can often provide ready-to-use solutions.
– Books43
• Extensions44 : Yii boasts a library of thousands of user-contributed
extensions that can be easily plugged into your applications, thereby
making your application development even faster and easier.
• Community
– Forum: http://www.yiiframework.com/forum/
– IRC chat: The #yii channel on the freenode network (irc://
irc.freenode.net/yii)
– Slack chanel: https://yii.slack.com
– Gitter chat: https://gitter.im/yiisoft/yii2
– GitHub: https://github.com/yiisoft/yii2
– Facebook: https://www.facebook.com/groups/yiitalk/
– Twitter: https://twitter.com/yiiframework
– LinkedIn: https://www.linkedin.com/groups/yii-framework-1483367
– Stackoverflow: http://stackoverflow.com/questions/tagged/
yii2
39
https://github.com/yiisoft/yii2-gii/blob/master/docs/guide/README.md
40
http://www.yiiframework.com/doc-2.0/guide-README.html
41
http://www.yiiframework.com/doc-2.0/index.html
42
http://www.yiiframework.com/wiki/?tag=yii2
43
http://www.yiiframework.com/doc/
44
http://www.yiiframework.com/extensions/
48 CHAPTER 2. GETTING STARTED
Chapter 3
Application Structure
3.1 Overview
• 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 applic-
ation 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.
1
http://wikipedia.org/wiki/Model-view-controller
49
50 CHAPTER 3. APPLICATION STRUCTURE
exit($exitCode);
3.3 Applications
Applications are objects that govern the overall structure and lifecycle of Yii
application systems. Each Yii application system contains a single applic-
ation object which is created in the entry script and is globally accessible
through the expression \Yii::$app.
There are two types of applications: Web applications and console applications.
As the names indicate, the former mainly handles Web requests, while the
latter handles console command requests.
3.3. APPLICATIONS 53
Required Properties
In any application, you should at least configure two properties: id and
basePath.
The basePath property is often used to derive other important paths (e.g.
the runtime path). For this reason, a path alias named @app is predefined to
represent this path. Derived paths may then be formed using this alias (e.g.
@app/runtime to refer to the runtime directory).
Important Properties
The properties described in this subsection often need to be configured be-
cause they differ across different applications.
This property is provided so that you can define aliases in terms of applica-
tion configurations instead of by calling the Yii::setAlias() method.
// a class name
’app\components\Profiler’,
// a configuration array
[
’class’ => ’app\components\Profiler’,
’level’ => 3,
3.3. APPLICATIONS 55
],
// an anonymous function
function () {
return new app\components\Profiler();
}
],
]
$config[’bootstrap’][] = ’gii’;
$config[’modules’][’gii’] = ’yii\gii\Module’;
}
[
’catchAll’ => [
’offline/notice’,
’param1’ => ’value1’,
’param2’ => ’value2’,
],
]
The array keys of this property represent the controller IDs, while the array
values represent the corresponding controller class names or configurations.
modules This property specifies the modules that the application contains.
The property takes an array of module classes or configurations with the
array keys being the module IDs. For example:
[
’modules’ => [
// a "booking" module specified with the module class
’booking’ => ’app\modules\booking\BookingModule’,
3
http://en.wikipedia.org/wiki/IETF_language_tag
58 CHAPTER 3. APPLICATION STRUCTURE
name This property specifies the application name that may be displayed
to end users. Unlike the id property, which should take a unique value, the
value of this property is mainly for display purposes; it does not need to be
unique.
You do not always need to configure this property if none of your code
is using it.
Then in your code where you need to use the size value, you can simply use
code like the following:
$size = \Yii::$app->params[’thumbnail.size’];
$width = \Yii::$app->params[’thumbnail.size’][0];
Later if you decide to change the thumbnail size, you only need to modify
it in the application configuration; you don’t need to touch any dependent
code.
Useful Properties
charset This property specifies the charset that the application uses. The
default value is ’UTF-8’, which should be kept as-is for most applications
unless you are working with a legacy system that uses a lot of non-Unicode
data.
extensions This property specifies the list of extensions that are installed
and used by the application. By default, it will take the array returned by
the file @vendor/yiisoft/extensions.php. The extensions.php file is generated
5
http://php.net/manual/en/function.date-default-timezone-set.php
60 CHAPTER 3. APPLICATION STRUCTURE
],
]
As you can see, the property takes an array of extension specifications. Each
extension is specified with an array consisting of name and version elements. If
an extension needs to run during the bootstrap process, a bootstrap element
may be specified with a bootstrapping class name or a configuration array.
An extension may also define a few aliases.
layout This property specifies the name of the default layout that should
be used when rendering a view. The default value is ’main’, meaning the
layout file main.php under the layout path should be used. If both of the
layout path and the view path are taking the default values, the default
layout file can be represented as the path alias @app/views/layouts/main.php.
You may configure this property to be false if you want to disable layout
by default, although this is very rare.
layoutPath This property specifies the path where layout files should be
looked for. The default value is the layouts sub-directory under the view
path. If the view path is taking its default value, the default layout path
can be represented as the path alias @app/views/layouts.
You may configure it as a directory or a path alias.
runtimePath This property specifies the path where temporary files, such
as log files and cache files, can be generated. The default value is the direct-
ory represented by the alias @app/runtime.
6
https://getcomposer.org
3.3. APPLICATIONS 61
You may configure it as a directory or a path alias. Note that the runtime
path must be writable by the process running the application. And the path
should be protected from being accessed by end users, because the temporary
files under it may contain sensitive information.
To simplify access to this path, Yii has predefined a path alias named
@runtime for it.
viewPath This property specifies the root directory where view files are
located. The default value is the directory represented by the alias @app/
views. You may configure it as a directory or a path alias.
7
https://getcomposer.org
62 CHAPTER 3. APPLICATION STRUCTURE
EVENT_BEFORE_REQUEST
This event is triggered before an application handles a request. The actual
event name is beforeRequest.
When this event is triggered, the application instance has been configured
and initialized. So it is a good place to insert your custom code via the
event mechanism to intercept the request handling process. For example, in
the event handler, you may dynamically set the yii\base\Application::
$language property based on some parameters.
EVENT_AFTER_REQUEST
This event is triggered after an application finishes handling a request but
before sending the response. The actual event name is afterRequest.
When this event is triggered, the request handling is completed and you
may take this chance to do some postprocessing of the request or customize
the response.
Note that the response component also triggers some events while it is
sending out response content to end users. Those events are triggered after
this event.
EVENT_BEFORE_ACTION
This event is triggered before running every controller action. The actual
event name is beforeAction.
The event parameter is an instance of yii\base\ActionEvent. An event
handler may set the yii\base\ActionEvent::$isValid property to be false
to stop running the action. For example:
[
’on beforeAction’ => function ($event) {
if (some condition) {
$event->isValid = false;
} else {
}
},
]
Note that the same beforeAction event is also triggered by modules and con-
trollers. Application objects are the first ones triggering this event, followed
by modules (if any), and finally controllers. If an event handler sets yii\base
\ActionEvent::$isValid to be false, all of the subsequent events will NOT
be triggered.
EVENT_AFTER_ACTION
This event is triggered after running every controller action. The actual
event name is afterAction.
3.3. APPLICATIONS 63
Note that the same afterAction event is also triggered by modules and con-
trollers. These objects trigger this event in the reverse order as for that of
beforeAction. That is, controllers are the first objects triggering this event,
followed by modules (if any), and finally applications.
4. The entry script receives the exit status from the application and com-
pletes the request processing.
For example, you can use \Yii::$app->db to get the DB connection, and \Yii
::$app->cache to get the primary cache registered with the application.
An application component is created the first time it is accessed through
the above expression. Any further accesses will return the same component
instance.
Application components can be any objects. You can register them by
configuring the yii\base\Application::$components property in applica-
tion configurations. For example,
[
’components’ => [
// register "cache" component using a class name
’cache’ => ’yii\caching\ApcCache’,
3.4. APPLICATION COMPONENTS 65
],
],
]
3.5 Controllers
Controllers are part of the MVC8 architecture. They are objects of classes
extending from yii\base\Controller and are responsible for processing
requests and generating responses. In particular, after taking over the control
from applications, controllers will analyze incoming request data, pass them
to models, inject model results into views, and finally generate outgoing
responses.
3.5.1 Actions
Controllers are composed of actions which are the most basic units that end
users can address and request for execution. A controller can have one or
multiple actions.
The following example shows a post controller with two actions: view and
create:
namespace app\controllers;
use Yii;
use app\models\Post;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
return $this->render(’view’, [
’model’ => $model,
]);
}
8
http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller
68 CHAPTER 3. APPLICATION STRUCTURE
In the view action (defined by the actionView() method), the code first loads
the model according to the requested model ID; If the model is loaded suc-
cessfully, it will display it using a view named view. Otherwise, it will throw
an exception.
In the create action (defined by the actionCreate() method), the code
is similar. It first tries to populate a new instance of the model using the
request data and save the model. If both succeed it will redirect the browser
to the view action with the ID of the newly created model. Otherwise it will
display the create view through which users can provide the needed input.
3.5.2 Routes
End users address actions through the so-called routes. A route is a string
that consists of the following parts:
• a module ID: this exists only if the controller belongs to a non-application
module;
• a controller ID: a string that uniquely identifies the controller among
all controllers within the same application (or the same module if the
controller belongs to a module);
• an action ID: a string that uniquely identifies the action among all
actions within the same controller.
Routes take the following format:
ControllerID/ActionID
use yii\web\Controller;
Controller IDs
Usually, a controller is designed to handle the requests regarding a particular
type of resource. For this reason, controller IDs are often nouns referring to
the types of the resources that they are handling. For example, you may use
article as the ID of a controller that handles article data.
By default, controller IDs should contain these characters only: English
letters in lower case, digits, underscores, hyphens, and forward slashes. For
example, article and post-comment are both valid controller IDs, while article
?, PostComment, admin\post are not.
A controller ID may also contain a subdirectory prefix. For example,
admin/article stands for an article controller in the admin subdirectory un-
der the controller namespace. Valid characters for subdirectory prefixes
include: English letters in lower and upper cases, digits, underscores, and
forward slashes, where forward slashes are used as separators for multi-level
subdirectories (e.g. panels/admin).
1. Turn the first letter in each word separated by hyphens into upper case.
Note that if the controller ID contains slashes, this rule only applies to
the part after the last slash in the ID.
2. Remove hyphens and replace any forward slashes with backward slashes.
The following are some examples, assuming the controller namespace takes
the default value app\controllers:
• article becomes app\controllers\ArticleController;
• post-comment becomes app\controllers\PostCommentController;
70 CHAPTER 3. APPLICATION STRUCTURE
Controller Map
You can configure the controller map to overcome the constraints of the
controller IDs and class names described above. This is mainly useful when
you are using third-party controllers and you do not have control over their
class names.
You may configure the controller map in the application configuration.
For example:
[
’controllerMap’ => [
// declares "account" controller using a class name
’account’ => ’app\controllers\UserController’,
Default Controller
Each application has a default controller specified via the yii\base\Application
::$defaultRoute property. When a request does not specify a route, the
route specified by this property will be used. For Web applications, its
value is ’site’, while for console applications, it is help. Therefore, if
a URL is http://hostname/index.php, then the site controller will handle the
request.
You may change the default controller with the following application
configuration:
3.5. CONTROLLERS 71
[
’defaultRoute’ => ’main’,
]
namespace app\controllers;
use yii\web\Controller;
Action IDs
Inline Actions
Inline actions refer to the actions that are defined in terms of action methods
as we just described.
The names of the action methods are derived from action IDs according
to the following procedure:
1. Turn the first letter in each word of the action ID into upper case.
2. Remove hyphens.
Inline actions are the most commonly defined actions because they take little
effort to create. However, if you plan to reuse the same action in different
places, or if you want to redistribute an action, you should consider defining
it as a standalone action.
Standalone Actions
Standalone actions are defined in terms of action classes extending yii\base
\Action or its child classes. For example, in the Yii releases, there are yii
\web\ViewAction and yii\web\ErrorAction, both of which are standalone
actions.
To use a standalone action, you should declare it in the action map by
overriding the yii\base\Controller::actions() method in your controller
classes like the following:
public function actions()
{
return [
// declares "error" action using a class name
’error’ => ’yii\web\ErrorAction’,
];
}
As you can see, the actions() method should return an array whose keys are
action IDs and values the corresponding action class names or configurations.
Unlike inline actions, action IDs for standalone actions can contain arbitrary
characters, as long as they are declared in the actions() method.
To create a standalone action class, you should extend yii\base\Action
or a child class, and implement a public method named run(). The role of
the run() method is similar to that of an action method. For example,
<?php
namespace app\components;
use yii\base\Action;
Action Results
Action Parameters
The action methods for inline actions and the run() methods for standalone
actions can take parameters, called action parameters. Their values are
obtained from requests. For Web applications, the value of each action
parameter is retrieved from $_GET using the parameter name as the key; for
console applications, they correspond to the command line arguments.
In the following example, the view action (an inline action) has declared
two parameters: $id and $version.
namespace app\controllers;
use yii\web\Controller;
Default Action
use yii\web\Controller;
• If one of the calls returns false, the rest of the uncalled beforeAction
() methods will be skipped and the action execution will be can-
celled.
• By default, each beforeAction() method call will trigger a beforeAction
event to which you can attach a handler.
6. The application will take the action result and assign it to the response.
3.6 Models
Models are part of the MVC9 architecture. They are objects representing
business data, rules and logic.
You can create model classes by extending yii\base\Model or its child
classes. The base class yii\base\Model supports many useful features:
• Attributes: represent the business data and can be accessed like normal
object properties or array elements;
• Attribute labels: specify the display labels for attributes;
9
http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller
3.6. MODELS 77
Info: You are not required to base your model classes on yii
\base\Model. However, because there are many Yii components
built to support yii\base\Model, it is usually the preferable base
class for a model.
3.6.1 Attributes
Models represent business data in terms of attributes. Each attribute is like
a publicly accessible property of a model. The method yii\base\Model::
attributes() specifies what attributes a model class has.
You can access an attribute like accessing a normal object property:
$model = new \app\models\ContactForm;
You can also access attributes like accessing array elements, thanks to the
support for ArrayAccess10 and Traversable11 by yii\base\Model:
$model = new \app\models\ContactForm;
Defining Attributes
By default, if your model class extends directly from yii\base\Model, all
its non-static public member variables are attributes. For example, the
10
http://php.net/manual/en/class.arrayaccess.php
11
http://php.net/manual/en/class.traversable.php
78 CHAPTER 3. APPLICATION STRUCTURE
model class below has four attributes: name, email, subject and
ContactForm
body. The ContactForm model is used to represent the input data received
from an HTML form.
namespace app\models;
use yii\base\Model;
Attribute Labels
When displaying values or getting input for attributes, you often need to dis-
play some labels associated with attributes. For example, given an attribute
named firstName, you may want to display a label First Name which is more
user-friendly when displayed to end users in places such as form inputs and
error messages.
You can get the label of an attribute by calling yii\base\Model::getAttributeLabel().
For example,
$model = new \app\models\ContactForm;
// displays "Name"
echo $model->getAttributeLabel(’name’);
use yii\base\Model;
3.6. MODELS 79
You may even conditionally define attribute labels. For example, based on
the scenario the model is being used in, you may return different labels for
the same attribute.
3.6.2 Scenarios
A model may be used in different scenarios. For example, a User model
may be used to collect user login inputs, but it may also be used for the
user registration purpose. In different scenarios, a model may use different
business rules and logic. For example, the email attribute may be required
during user registration, but not so during user login.
A model uses the yii\base\Model::$scenario property to keep track
of the scenario it is being used in. By default, a model supports only a single
scenario named default. The following code shows two ways of setting the
scenario of a model:
80 CHAPTER 3. APPLICATION STRUCTURE
use yii\db\ActiveRecord;
The scenarios() method returns an array whose keys are the scenario names
and values the corresponding active attributes. An active attribute can be
massively assigned and is subject to validation. In the above example, the
username and password attributes are active in the login scenario; while in the
register scenario, email is also active besides username and password.
The default implementation of scenarios() will return all scenarios found
in the validation rule declaration method yii\base\Model::rules(). When
overriding scenarios(), if you want to introduce new scenarios in addition to
the default ones, you may write code like the following:
namespace app\models;
use yii\db\ActiveRecord;
if ($model->validate()) {
// all inputs are valid
} else {
// validation failed: $errors is an array containing error messages
$errors = $model->errors;
}
If you do not specify the on property, the rule would be applied in all scen-
arios. A rule is called an active rule if it can be applied in the current
scenario.
An attribute will be validated if and only if it is an active attribute
declared in scenarios() and is associated with one or multiple active rules
declared in rules().
Safe Attributes
Massive assignment only applies to the so-called safe attributes which are the
attributes listed in yii\base\Model::scenarios() for the current scenario
of a model. For example, if the User model has the following scenario declar-
ation, then when the current scenario is login, only the username and password
can be massively assigned. Any other attributes will be kept untouched.
public function scenarios()
{
return [
self::SCENARIO_LOGIN => [’username’, ’password’],
self::SCENARIO_REGISTER => [’username’, ’email’, ’password’],
];
}
Unsafe Attributes
As described above, the yii\base\Model::scenarios() method serves for
two purposes: determining which attributes should be validated, and de-
termining which attributes are safe. In some rare cases, you may want to
validate an attribute but do not want to mark it safe. You can do so by
84 CHAPTER 3. APPLICATION STRUCTURE
When the model is in the login scenario, all three attributes will be validated.
However, only the username and password attributes can be massively assigned.
To assign an input value to the secret attribute, you have to do it explicitly
as follows,
$model->secret = $secret;
In this case attributes username, password and secret are required, but secret
must be assigned explicitly.
Fields
A field is simply a named element in the array that is obtained by calling
the yii\base\Model::toArray() method of a model.
By default, field names are equivalent to attribute names. However, you
can change this behavior by overriding the fields() and/or extraFields()
methods. Both methods should return a list of field definitions. The fields
defined by fields() are default fields, meaning that toArray() will return these
fields by default. The extraFields() method defines additionally available
fields which can also be returned by toArray() as long as you specify them
via the $expand parameter. For example, the following code will return all
fields defined in fields() and the prettyName and fullAddress fields if they are
defined in extraFields().
$array = $model->toArray([], [’prettyName’, ’fullAddress’]);
You can override fields() to add, remove, rename or redefine fields. The
return value of fields() should be an array. The array keys are the field
names, and the array values are the corresponding field definitions which
can be either property/attribute names or anonymous functions returning
the corresponding field values. In the special case when a field name is the
same as its defining attribute name, you can omit the array key. For example,
// explicitly list every field, best used when you want to make sure the
changes
// in your DB table or model attributes do not cause your field changes (to
keep API backward compatibility).
public function fields()
{
return [
// field name is the same as the attribute name
’id’,
// filter out some fields, best used when you want to inherit the parent
implementation
// and blacklist some sensitive fields.
public function fields()
86 CHAPTER 3. APPLICATION STRUCTURE
{
$fields = parent::fields();
return $fields;
}
For example, in the Advanced Project Template12 , you may define a base
model class common\models\Post. Then for the front end application, you
define and use a concrete model class frontend\models\Post which extends
from common\models\Post. And similarly for the back end application, you
define backend\models\Post. With this strategy, you will be sure that the code
in frontend\models\Post is only specific to the front end application, and if
you make any change to it, you do not need to worry if the change may
break the back end application.
3.7 Views
Views are part of the MVC13 architecture. They are code responsible for
presenting data to end users. In a Web application, views are usually created
in terms of view templates which are PHP script files containing mainly
HTML code and presentational PHP code. They are managed by the view
application component which provides commonly used methods to facilitate
view composition and rendering. For simplicity, we often call view templates
or view template files as views.
$this->title = ’Login’;
?>
<h1><?= Html::encode($this->title) ?></h1>
Within a view, you can access $this which refers to the view component
managing and rendering this view template.
Besides $this, there may be other predefined variables in a view, such
as $model in the above example. These variables represent the data that are
pushed into the view by controllers or other objects which trigger the view
rendering.
Security
When creating views that generate HTML pages, it is important that you
encode and/or filter the data coming from end users before presenting them.
Otherwise, your application may be subject to cross-site scripting14 attacks.
To display a plain text, encode it first by calling yii\helpers\Html::
encode(). For example, the following code encodes the user name before
displaying it:
<?php
use yii\helpers\Html;
?>
<div class="username">
<?= Html::encode($user->name) ?>
</div>
<div class="post">
<?= HtmlPurifier::process($post->text) ?>
</div>
Organizing Views
Like controllers and models, there are conventions to organize views.
• For views rendered by a controller, they should be put under the dir-
ectory @app/views/ControllerID by default, where ControllerID refers to
the controller ID. For example, if the controller class is PostController,
the directory would be @app/views/post; if it is PostCommentController,
the directory would be @app/views/post-comment. In case the controller
belongs to a module, the directory would be views/ControllerID under
the module directory.
• For views rendered in a widget, they should be put under the WidgetPath
/views directory by default, where WidgetPath stands for the directory
containing the widget class file.
• For views rendered by other objects, it is recommended that you follow
the similar convention as that for widgets.
You may customize these default view directories by overriding the yii\base
\ViewContextInterface::getViewPath() method of controllers or widgets.
Rendering in Controllers
Within controllers, you may call the following controller methods to render
views:
• render(): renders a named view and applies a layout to the rendering
result.
• renderPartial(): renders a named view without any layout.
• renderAjax(): renders a named view without any layout, and injects
all registered JS/CSS scripts and files. It is usually used in response
to AJAX Web requests.
• renderFile(): renders a view specified in terms of a view file path or
alias.
• renderContent(): renders a static string by embedding it into the
currently applicable layout. This method is available since version
2.0.1.
90 CHAPTER 3. APPLICATION STRUCTURE
For example,
namespace app\controllers;
use Yii;
use app\models\Post;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
Rendering in Widgets
Within widgets, you may call the following widget methods to render views.
• render(): renders a named view.
• renderFile(): renders a view specified in terms of a view file path or
alias.
For example,
namespace app\components;
use yii\base\Widget;
use yii\helpers\Html;
Rendering in Views
You can render a view within another view by calling one of the following
methods provided by the view component:
• render(): renders a named view.
• renderAjax(): renders a named view and injects all registered JS/CSS
scripts and files. It is usually used in response to AJAX Web requests.
• renderFile(): renders a view specified in terms of a view file path or
alias.
For example, the following code in a view renders the _overview.php view
file which is in the same directory as the view being currently rendered.
Remember that $this in a view refers to the view component:
<?= $this->render(’_overview’) ?>
Named Views
When you render a view, you can specify the view using either a view name
or a view file path/alias. In most cases, you would use the former because it
is more concise and flexible. We call views specified using names as named
views.
A view name is resolved into the corresponding view file path according
to the following rules:
• A view name may omit the file extension name. In this case, .php will be
used as the extension. For example, the view name about corresponds
to the file name about.php.
• If the view name starts with double slashes //, the corresponding view
file path would be @app/views/ViewName. That is, the view is looked for
under the application’s view path. For example, //site/about will
be resolved into @app/views/site/about.php.
• If the view name starts with a single slash /, the view file path is formed
by prefixing the view name with the view path of the currently active
module. If there is no active module, @app/views/ViewName will be used.
For example, /user/create will be resolved into @app/modules/user/views
/user/create.php, if the currently active module is user. If there is no
active module, the view file path would be @app/views/user/create.php.
92 CHAPTER 3. APPLICATION STRUCTURE
• If the view is rendered with a context and the context implements yii
\base\ViewContextInterface, the view file path is formed by prefix-
ing the view path of the context to the view name. This mainly applies
to the views rendered within controllers and widgets. For example,
about will be resolved into @app/views/site/about.php if the context is
the controller SiteController.
• If a view is rendered within another view, the directory containing the
other view file will be prefixed to the new view name to form the actual
view file path. For example, item will be resolved into @app/views/post
/item.php if it is being rendered in the view @app/views/post/index.php.
According to the above rules, calling $this->render(’view’) in a controller app
\controllers\PostController will actually render the view file @app/views/post/
view.php, while calling $this->render(’_overview’) in that view will render the
view file @app/views/post/_overview.php.
There are two approaches to access data within a view: push and pull.
By passing the data as the second parameter to the view rendering meth-
ods, you are using the push approach. The data should be represented as
an array of name-value pairs. When the view is being rendered, the PHP
extract() function will be called on this array so that the array is extracted
into variables in the view. For example, the following view rendering code in
a controller will push two variables to the report view: $foo = 1 and $bar = 2.
echo $this->render(’report’, [
’foo’ => 1,
’bar’ => 2,
]);
The pull approach actively retrieves data from the view component or other
objects accessible in views (e.g. Yii::$app). Using the code below as an
example, within the view you can get the controller object by the expression
$this->context. And as a result, it is possible for you to access any properties
or methods of the controller in the report view, such as the controller ID
shown in the following:
The controller ID is: <?= $this->context->id ?>
The push approach is usually the preferred way of accessing data in views,
because it makes views less dependent on context objects. Its drawback is
that you need to manually build the data array all the time, which could
become tedious and error prone if a view is shared and rendered in different
places.
3.7. VIEWS 93
Then, in the layout file, which is also a view, you can display the breadcrumbs
using the data passed along params:
<?= yii\widgets\Breadcrumbs::widget([
’links’ => isset($this->params[’breadcrumbs’]) ? $this->params[’
breadcrumbs’] : [],
]) ?>
3.7.3 Layouts
Layouts are a special type of views that represent the common parts of
multiple views. For example, the pages for most Web applications share the
same page header and footer. While you can repeat the same page header
and footer in every view, a better way is to do this once in a layout and
embed the rendering result of a content view at an appropriate place in the
layout.
Creating Layouts
Because layouts are also views, they can be created in the similar way as
normal views. By default, layouts are stored in the directory @app/views/
layouts. For layouts used within a module, they should be stored in the views
/layouts directory under the module directory. You may customize the
default layout directory by configuring the yii\base\Module::$layoutPath
property of the application or modules.
The following example shows how a layout looks like. Note that for
illustrative purpose, we have greatly simplified the code in the layout. In
practice, you may want to add more content to it, such as head tags, main
menu, etc.
<?php
use yii\helpers\Html;
As you can see, the layout generates the HTML tags that are common to
all pages. Within the <body> section, the layout echoes the $content variable
which represents the rendering result of content views and is pushed into the
layout when yii\base\Controller::render() is called.
Most layouts should call the following methods like shown in the above
code. These methods mainly trigger events about the rendering process so
that scripts and tags registered in other places can be properly injected into
the places where these methods are called.
• beginPage(): This method should be called at the very beginning of
the layout. It triggers the EVENT_BEGIN_PAGE event which indicates
the beginning of a page.
• endPage(): This method should be called at the end of the layout. It
triggers the EVENT_END_PAGE event which indicates the end of a page.
• head(): This method should be called within the <head> section of an
HTML page. It generates a placeholder which will be replaced with
the registered head HTML code (e.g. link tags, meta tags) when a
page finishes rendering.
• beginBody(): This method should be called at the beginning of the
<body> section. It triggers the EVENT_BEGIN_BODY event and generates
a placeholder which will be replaced by the registered HTML code (e.g.
JavaScript) targeted at the body begin position.
• endBody(): This method should be called at the end of the <body> sec-
tion. It triggers the EVENT_END_BODY event and generates a placeholder
which will be replaced by the registered HTML code (e.g. JavaScript)
targeted at the body end position.
Using Layouts
As described in the Rendering in Controllers subsection, when you render a
view by calling the render() method in a controller, a layout will be applied
to the rendering result. By default, the layout @app/views/layouts/main.php
will be used.
You may use a different layout by configuring either yii\base\Application
::$layout or yii\base\Controller::$layout. The former governs the lay-
out used by all controllers, while the latter overrides the former for individual
controllers. For example, the following code makes the post controller to use
@app/views/layouts/post.php as the layout when rendering its views. Other
controllers, assuming their layout property is untouched, will still use the
default @app/views/layouts/main.php as the layout.
namespace app\controllers;
use yii\web\Controller;
// ...
}
For controllers belonging to a module, you may also configure the module’s
layout property to use a particular layout for these controllers.
Because the layout property may be configured at different levels (control-
lers, modules, application), behind the scene Yii takes two steps to determine
what is the actual layout file being used for a particular controller.
In the first step, it determines the layout value and the context module:
• If the yii\base\Controller::$layout property of the controller is
not null, use it as the layout value and the module of the controller as
the context module.
• If the yii\base\Controller::$layout property of the controller is
null, search through all ancestor modules (including the application
itself) of the controller and find the first module whose layout property
is not null. Use that module and its layout value as the context
module and the chosen layout value. If such a module cannot be found,
it means no layout will be applied.
In the second step, it determines the actual layout file according to the layout
value and the context module determined in the first step. The layout value
can be:
96 CHAPTER 3. APPLICATION STRUCTURE
Nested Layouts
Sometimes you may want to nest one layout in another. For example, in
different sections of a Web site, you want to use different layouts, while all
these layouts share the same basic layout that generates the overall HTML5
page structure. You can achieve this goal by calling beginContent() and
endContent() in the child layouts like the following:
<?php $this->beginContent(’@app/views/layouts/base.php’); ?>
As shown above, the child layout content should be enclosed within beginContent()
and endContent(). The parameter passed to beginContent() specifies what
is the parent layout. It can be either a layout file or alias.
Using the above approach, you can nest layouts in more than one levels.
Using Blocks
Blocks allow you to specify the view content in one place while displaying
it in another. They are often used together with layouts. For example, you
can define a block in a content view and display it in the layout.
You call beginBlock() and endBlock() to define a block. The block
can then be accessed via $view->blocks[$blockID], where $blockID stands for
a unique ID that you assign to the block when defining it.
The following example shows how you can use blocks to customize specific
parts of a layout in a content view.
First, in a content view, define one or multiple blocks:
...
...content of block1...
3.7. VIEWS 97
...
...content of block3...
Then, in the layout view, render the blocks if they are available, or display
some default content if a block is not defined.
...
<?php if (isset($this->blocks[’block1’])): ?>
<?= $this->blocks[’block1’] ?>
<?php else: ?>
... default content for block1 ...
<?php endif; ?>
...
...
Every Web page should have a title. Normally the title tag is being displayed
in a layout. However, in practice the title is often determined in content
views rather than layouts. To solve this problem, yii\web\View provides
the title property for you to pass the title information from content views
to layouts.
To make use of this feature, in each content view, you can set the page
title like the following:
<?php
$this->title = ’My page title’;
?>
Then in the layout, make sure you have the following code in the <head>
section:
<title><?= Html::encode($this->title) ?></title>
Web pages usually need to generate various meta tags needed by different
parties. Like page titles, meta tags appear in the <head> section and are
usually generated in layouts.
If you want to specify what meta tags to generate in content views,
you can call yii\web\View::registerMetaTag() in a content view, like the
following:
15
http://twig.sensiolabs.org/
16
http://www.smarty.net/
3.7. VIEWS 99
<?php
$this->registerMetaTag([’name’ => ’keywords’, ’content’ => ’yii, framework,
php’]);
?>
The above code will register a “keywords” meta tag with the view component.
The registered meta tag is rendered after the layout finishes rendering. The
following HTML code will be generated and inserted at the place where you
call yii\web\View::head() in the layout:
<meta name="keywords" content="yii, framework, php">
Note that if you call yii\web\View::registerMetaTag() multiple times, it
will register multiple meta tags, regardless whether the meta tags are the
same or not.
To make sure there is only a single instance of a meta tag type, you can
specify a key as a second parameter when calling the method. For example,
the following code registers two “description” meta tags. However, only the
second one will be rendered.
$this->registerMetaTag([’name’ => ’description’, ’content’ => ’This is my
cool website made with Yii!’], ’description’);
$this->registerMetaTag([’name’ => ’description’, ’content’ => ’This website
is about funny raccoons.’], ’description’);
If a Web site contains many static pages, it would be very tedious repeating
the similar code many times. To solve this problem, you may introduce a
standalone action called yii\web\ViewAction in a controller. For example,
namespace app\controllers;
use yii\web\Controller;
];
}
}
Now if you create a view named about under the directory @app/views/site/
pages, you will be able to display this view by the following URL:
http://localhost/index.php?r=site%2Fpage&view=about
3.8 Modules
Modules are self-contained software units that consist of models, views, con-
trollers, and other supporting components. End users can access the con-
trollers of a module when it is installed in application. For these reasons,
modules are often viewed as mini-applications. Modules differ from applic-
ations in that modules cannot be deployed alone and must reside within
applications.
102 CHAPTER 3. APPLICATION STRUCTURE
Module Classes
Each module should have a unique module class which extends from yii
\base\Module. The class should be located directly under the module’s base
path and should be autoloadable. When a module is being accessed, a single
instance of the corresponding module class will be created. Like application
instances, module instances are used to share data and components for code
within modules.
The following is an example how a module class may look like:
namespace app\modules\forum;
$this->params[’foo’] = ’bar’;
// ... other initialization code ...
}
}
If the init() method contains a lot of code initializing the module’s proper-
ties, you may also save them in terms of a configuration and load it with the
following code in init():
public function init()
{
parent::init();
// initialize the module with the configuration loaded from config.php
\Yii::configure($this, require __DIR__ . ’/config.php’);
}
where the configuration file config.php may contain the following content,
similar to that in an application configuration.
3.8. MODULES 103
<?php
return [
’components’ => [
// list of component configurations
],
’params’ => [
// list of parameters
],
];
Controllers in Modules
use yii\web\Controller;
Views in Modules
Views in a module should be put in the views directory within the module’s
base path. For views rendered by a controller in the module, they should
be put under the directory views/ControllerID, where ControllerID refers to
the controller ID. For example, if the controller class is PostController, the
directory would be views/post within the module’s base path.
A module can specify a layout that is applied to the views rendered by the
module’s controllers. The layout should be put in the views/layouts directory
by default, and you should configure the yii\base\Module::$layout prop-
erty to point to the layout name. If you do not configure the layout property,
the application’s layout will be used instead.
104 CHAPTER 3. APPLICATION STRUCTURE
Your commands will then be available from the command line using the
following route:
yii <module_id>/<command>/<sub_command>
Routes
Like accessing controllers in an application, routes are used to address con-
trollers in a module. A route for a controller within a module must begin with
the module ID followed by the controller ID and action ID. For example, if an
application uses a module named forum, then the route forum/post/index would
3.8. MODULES 105
represent the index action of the post controller in the module. If the route
only contains the module ID, then the yii\base\Module::$defaultRoute
property, which defaults to default, will determine which controller/action
should be used. This means a route forum would represent the default con-
troller in the forum module.
The URL manager rules for the modules should be added before yii
\web\UrlManager::parseRequest() is fired. That means doing it in mod-
ule’s init() won’t work because module will be initialized when routes were
already processed. Thus, the rules should be added at bootstrap stage.
It is a also a good practice to wrap module’s URL rules with yii\web
\GroupUrlRule.
In case a module is used to version API, its URL rules should be added
directly in urlManager section of the application config.
Accessing Modules
Within a module, you may often need to get the instance of the module
class so that you can access the module ID, module parameters, module
components, etc. You can do so by using the following statement:
$module = MyModuleClass::getInstance();
where MyModuleClass refers to the name of the module class that you are
interested in. The getInstance() method will return the currently requested
instance of the module class. If the module is not requested, the method will
return null. Note that you do not want to manually create a new instance
of the module class because it will be different from the one created by Yii
in response to a request.
Info: When developing a module, you should not assume the
module will use a fixed ID. This is because a module can be
associated with an arbitrary ID when used in an application or
within another module. In order to get the module ID, you should
use the above approach to get the module instance first, and then
get the ID via $module->id.
You may also access the instance of a module using the following approaches:
// get the child module whose ID is "forum"
$module = \Yii::$app->getModule(’forum’);
The first approach is only useful when you know the module ID, while the
second approach is best used when you know about the controllers being
requested.
Once you have the module instance, you can access parameters and com-
ponents registered with the module. For example,
106 CHAPTER 3. APPLICATION STRUCTURE
$maxPostCount = $module->params[’maxPostCount’];
Bootstrapping Modules
Some modules may need to be run for every request. The yii\debug\Module
module is such an example. To do so, list the IDs of such modules in the
bootstrap property of the application.
For example, the following application configuration makes sure the debug
module is always loaded:
[
’bootstrap’ => [
’debug’,
],
’modules’ => [
’debug’ => ’yii\debug\Module’,
],
]
$this->modules = [
’admin’ => [
// you should consider using a shorter namespace here!
’class’ => ’app\modules\forum\modules\admin\Module’,
],
];
}
}
For a controller within a nested module, its route should include the IDs of
all its ancestor modules. For example, the route forum/admin/dashboard/index
represents the index action of the dashboard controller in the admin module
which is a child module of the forum module.
The application database tables will be prefixed with main_, while all module
tables will be prefixed with module_. Note that configuration above is not
merged; the modules’ component for example will have the query cache
enabled since that is the default value.
3.9 Filters
Filters are objects that run before and/or after controller actions. For ex-
ample, an access control filter may run before actions to ensure that they are
allowed to be accessed by particular end users; a content compression filter
may run after actions to compress the response content before sending them
out to end users.
A filter may consist of a pre-filter (filtering logic applied before actions)
and/or a post-filter (logic applied after actions).
When multiple filters are configured for a single action, they are applied
according to the rules described below:
3.9. FILTERS 109
• Pre-filtering
– Apply filters declared in the application in the order they are
listed in behaviors().
– Apply filters declared in the module in the order they are listed
in behaviors().
– Apply filters declared in the controller in the order they are listed
in behaviors().
– If any of the filters cancel the action execution, the filters (both
pre-filters and post-filters) after it will not be applied.
• Running the action if it passes the pre-filtering.
• Post-filtering
– Apply filters declared in the controller in the reverse order they
are listed in behaviors().
– Apply filters declared in the module in the reverse order they are
listed in behaviors().
– Apply filters declared in the application in the reverse order they
are listed in behaviors().
use Yii;
use yii\base\ActionFilter;
AccessControl
AccessControl provides simple access control based on a set of rules. In
particular, before an action is executed, AccessControl will examine the listed
rules and find the first one that matches the current context variables (such
as user IP address, user login status, etc.) The matching rule will dictate
whether to allow or deny the execution of the requested action. If no rule
matches, the access will be denied.
The following example shows how to allow authenticated users to access
the create and update actions while denying all other users from accessing
these two actions.
use yii\filters\AccessControl;
For more details about access control in general, please refer to the Author-
ization section.
ContentNegotiator
besides being used as a filter. For example, you may configure it in the
application configuration like the following:
use yii\filters\ContentNegotiator;
use yii\web\Response;
[
’bootstrap’ => [
[
’class’ => ContentNegotiator::className(),
’formats’ => [
’application/json’ => Response::FORMAT_JSON,
’application/xml’ => Response::FORMAT_XML,
],
’languages’ => [
’en-US’,
’de’,
],
],
],
];
HttpCache
use yii\filters\HttpCache;
Please refer to the HTTP Caching section for more details about using Ht-
tpCache.
3.9. FILTERS 113
PageCache
PageCache implements server-side caching of whole pages. In the following
example, PageCache is applied to the index action to cache the whole page for
maximum 60 seconds or until the count of entries in the post table changes. It
also stores different versions of the page depending on the chosen application
language.
use yii\filters\PageCache;
use yii\caching\DbDependency;
Please refer to the Page Caching section for more details about using PageCache.
RateLimiter
RateLimiter implements a rate limiting algorithm based on the leaky bucket
algorithm19 . It is primarily used in implementing RESTful APIs. Please
refer to the Rate Limiting section for details about using this filter.
VerbFilter
VerbFilter checks if the HTTP request methods are allowed by the requested
actions. If not allowed, it will throw an HTTP 405 exception. In the following
example, VerbFilter is declared to specify a typical set of allowed request
methods for CRUD actions.
use yii\filters\VerbFilter;
Cors
Also check the section on REST Controllers if you want to add the CORS
filter to an yii\rest\ActiveController class in your API.
The Cors filtering could be tuned using the $cors property.
• cors[’Origin’]: array used to define allowed origins. Can be [’*’
] (everyone) or [’http://www.myserver.net’, ’http://www.myotherserver.
com’]. Default to [’*’].
• cors[’Access-Control-Request-Method’]: array of allowed verbs like [’
GET’, ’OPTIONS’, ’HEAD’]. Default to [’GET’, ’POST’, ’PUT’, ’PATCH’, ’
DELETE’, ’HEAD’, ’OPTIONS’].
20
https://developer.mozilla.org/en-US/docs/HTTP/Access_control_CORS
3.9. FILTERS 115
You may tune the CORS headers by overriding default parameters on a per
action basis. For example adding the Access-Control-Allow-Credentials for
the login action could be done like this :
use yii\filters\Cors;
use yii\helpers\ArrayHelper;
3.10 Widgets
Widgets are reusable building blocks used in views to create complex and
configurable user interface elements in an object-oriented fashion. For ex-
ample, a date picker widget may generate a fancy date picker that allows
users to pick a date as their input. All you need to do is just to insert the
code in a view like the following:
<?php
use yii\jui\DatePicker;
?>
<?= DatePicker::widget([’name’ => ’date’]) ?>
There are a good number of widgets bundled with Yii, such as active form,
menu, jQuery UI widgets, Twitter Bootstrap widgets. In the following, we
will introduce the basic knowledge about widgets. Please refer to the class
API documentation if you want to learn about the usage of a particular
widget.
Some widgets can take a block of content which should be enclosed between
the invocation of yii\base\Widget::begin() and yii\base\Widget::end().
For example, the following code uses the yii\widgets\ActiveForm widget
to generate a login form. The widget will generate the opening and clos-
ing <form> tags at the place where begin() and end() are called, respectively.
Anything in between will be rendered as is.
<?php
use yii\widgets\ActiveForm;
use yii\helpers\Html;
?>
<div class="form-group">
<?= Html::submitButton(’Login’) ?>
</div>
use yii\base\Widget;
use yii\helpers\Html;
public $message;
use yii\base\Widget;
use yii\helpers\Html;
The following code shows how to use this new variant of HelloWidget:
<?php
use app\components\HelloWidget;
?>
<?php HelloWidget::begin(); ?>
Sometimes, a widget may need to render a big chunk of content. While you
can embed the content within the run() method, a better approach is to put
it in a view and call yii\base\Widget::render() to render it. For example,
public function run()
{
return $this->render(’hello’);
}
3.11 Assets
An asset in Yii is a file that may be referenced in a Web page. It can be a
CSS file, a JavaScript file, an image or video file, etc. Assets are located in
Web-accessible directories and are directly served by Web servers.
120 CHAPTER 3. APPLICATION STRUCTURE
namespace app\assets;
use yii\web\AssetBundle;
The above AppAsset class specifies that the asset files are located under the
directory which corresponds to the URL @web; the bundle contains a
@webroot
3.11. ASSETS 121
single CSS file css/site.css and no JavaScript file; the bundle depends on two
other bundles: yii\web\YiiAsset and yii\bootstrap\BootstrapAsset.
More detailed explanation about the properties of yii\web\AssetBundle
can be found in the following:
• sourcePath: specifies the root directory that contains the asset files
in this bundle. This property should be set if the root directory is not
Web accessible. Otherwise, you should set the basePath property and
baseUrl, instead. Path aliases can be used here.
• basePath: specifies a Web-accessible directory that contains the asset
files in this bundle. When you specify the sourcePath property, the
asset manager will publish the assets in this bundle to a Web-accessible
directory and overwrite this property accordingly. You should set this
property if your asset files are already in a Web-accessible directory
and do not need asset publishing. Path aliases can be used here.
• baseUrl: specifies the URL corresponding to the directory basePath.
Like basePath, if you specify the sourcePath property, the asset man-
ager will publish the assets and overwrite this property accordingly.
Path aliases can be used here.
• css: an array listing the CSS files contained in this bundle. Note that
only forward slash “/” should be used as directory separators. Each
file can be specified on its own as a string or in an array together with
attribute tags and their values.
• js: an array listing the JavaScript files contained in this bundle. The
format of this array is the same as that of css. Each JavaScript file
can be specified in one of the following two formats:
– a relative path representing a local JavaScript file (e.g. js/main.
js). The actual path of the file can be determined by prepending
yii\web\AssetManager::$basePath to the relative path, and the
actual URL of the file can be determined by prepending yii\web
\AssetManager::$baseUrl to the relative path.
– an absolute URL representing an external JavaScript file. For ex-
ample, http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min
.js or //ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js.
• depends: an array listing the names of the asset bundles that this
bundle depends on (to be explained shortly).
• jsOptions: specifies the options that will be passed to the yii\web
\View::registerJsFile() method when it is called to register every
JavaScript file in this bundle.
• cssOptions: specifies the options that will be passed to the yii\web
\View::registerCssFile() method when it is called to register every
CSS file in this bundle.
• publishOptions: specifies the options that will be passed to the yii
\web\AssetManager::publish() method when it is called to publish
source asset files to a Web directory. This is only used if you specify
122 CHAPTER 3. APPLICATION STRUCTURE
Asset Locations
Assets, based on their location, can be classified as:
• source assets: the asset files are located together with PHP source code
which cannot be directly accessed via Web. In order to use source assets
in a page, they should be copied to a Web directory and turned into
the so-called published assets. This process is called asset publishing
which will be described in detail shortly.
• published assets: the asset files are located in a Web directory and can
thus be directly accessed via Web.
• external assets: the asset files are located on a Web server that is
different from the one hosting your Web application.
When defining an asset bundle class, if you specify the sourcePath property,
it means any assets listed using relative paths will be considered as source
assets. If you do not specify this property, it means those assets are published
assets (you should therefore specify basePath and baseUrl to let Yii know
where they are located).
It is recommended that you place assets belonging to an application in
a Web directory to avoid the unnecessary asset publishing process. This is
why AppAsset in the prior example specifies basePath instead of sourcePath.
For extensions, because their assets are located together with their source
code in directories that are not Web accessible, you have to specify the
sourcePath property when defining asset bundle classes for them.
Asset Dependencies
When you include multiple CSS or JavaScript files in a Web page, they have
to follow a certain order to avoid overriding issues. For example, if you are
using a jQuery UI widget in a Web page, you have to make sure the jQuery
JavaScript file is included before the jQuery UI JavaScript file. We call such
ordering the dependencies among assets.
Asset dependencies are mainly specified through the yii\web\AssetBundle
::$depends property. In the AppAsset example, the asset bundle depends on
two other asset bundles: yii\web\YiiAsset and yii\bootstrap\BootstrapAsset,
which means the CSS and JavaScript files in AppAsset will be included after
those files in the two dependent bundles.
3.11. ASSETS 123
Asset Options
You can specify the cssOptions and jsOptions properties to customize the
way that CSS and JavaScript files are included in a page. The values of
these properties will be passed to the yii\web\View::registerCssFile()
and yii\web\View::registerJsFile() methods, respectively, when they
are called by the view to include CSS and JavaScript files.
For example, to conditionally include a CSS file for browsers that are IE9 or
below, you can use the following option:
public $cssOptions = [’condition’ => ’lte IE9’];
This will cause a CSS file in the bundle to be included using the following
HTML tags:
<!--[if lte IE9]>
<link rel="stylesheet" href="path/to/foo.css">
<![endif]-->
To wrap the generated CSS link tags within <noscript>, you can configure
cssOptions as follows,
public $cssOptions = [’noscript’ => true];
To include a JavaScript file in the head section of a page (by default, JavaS-
cript files are included at the end of the body section), use the following
option:
public $jsOptions = [’position’ => \yii\web\View::POS_HEAD];
By default, when an asset bundle is being published, all contents in the dir-
ectory specified by yii\web\AssetBundle::$sourcePath will be published.
You can customize this behavior by configuring the publishOptions prop-
erty. For example, to publish only one or a few subdirectories of yii\web
\AssetBundle::$sourcePath, you can do the following in the asset bundle
class:
<?php
namespace app\assets;
use yii\web\AssetBundle;
124 CHAPTER 3. APPLICATION STRUCTURE
The above example defines an asset bundle for the “fontawesome” package22 .
By specifying the only publishing option, only the fonts and css subdirect-
ories will be published.
$config = [
...
’aliases’ => [
’@bower’ => ’@vendor/bower-asset’,
’@npm’ => ’@vendor/npm-asset’,
],
...
];
This command installs composer asset plugin26 globally which allows man-
aging Bower and NPM package dependencies through Composer. After the
plugin installation, every single project on your computer will support Bower
and NPM packages through composer.json.
Add the following lines to composer.json of your project to adjust direct-
ories where the installed packages will be placed, if you want to publish them
using Yii:
"extra": {
"asset-installer-paths": {
"npm-asset-library": "vendor/npm",
"bower-asset-library": "vendor/bower"
}
}
3. Create an asset bundle class and list the JavaScript/CSS files that you
plan to use in your application or extension. You should specify the
sourcePath property as @bower/PackageName or @npm/PackageName. This
is because Composer will install the Bower or NPM package in the
directory corresponding to this alias.
Note: Some packages may put all their distributed files in a sub-
directory. If this is the case, you should specify the subdirectory
as the value of sourcePath. For example, yii\web\JqueryAsset
uses @bower/jquery/dist instead of @bower/jquery.
If you are registering an asset bundle in other places, you should provide the
needed view object. For example, to register an asset bundle in a widget
class, you can get the view object by $this->view.
When an asset bundle is registered with a view, behind the scenes Yii will
register all its dependent asset bundles. And if an asset bundle is located in a
directory inaccessible through the Web, it will be published to a Web direct-
ory. Later, when the view renders a page, it will generate <link> and <script>
tags for the CSS and JavaScript files listed in the registered bundles. The
order of these tags is determined by the dependencies among the registered
bundles and the order of the assets listed in the yii\web\AssetBundle::
$css and yii\web\AssetBundle::$js properties.
namespace app\assets;
use yii\web\AssetBundle;
use Yii;
Particular asset bundle can also be adjusted via its instance returned by yii
\web\AssetBundle::register(). For example:
use app\assets\SophisticatedAssetBundle;
use Yii;
$bundle = SophisticatedAssetBundle::register(Yii::$app->view);
$bundle->js[] = ’i18n/’ . Yii::$app->language . ’.js’; // dynamic file added
’js’ => [
’//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery
.min.js’,
]
],
],
],
],
];
You can disable one or multiple asset bundles by associating false with the
names of the asset bundles that you want to disable. When you register
a disabled asset bundle with a view, none of its dependent bundles will be
registered, and the view also will not include any of the assets in the bundle
in the page it renders. For example, to disable yii\web\JqueryAsset, you
can use the following configuration:
return [
// ...
’components’ => [
’assetManager’ => [
’bundles’ => [
’yii\web\JqueryAsset’ => false,
],
],
],
];
or over the registered bundle object will take precedence over AssetManager
configuration. Here are the examples, where mapping set via yii\web
\AssetManager::$bundles makes no effect:
// Program source code:
namespace app\assets;
use yii\web\AssetBundle;
use Yii;
$bundle = \app\assets\LargeFileAssetBundle::register(Yii::$app->view);
$bundle->baseUrl = YII_DEBUG ? ’@web/large-files’: ’@web/large-files/
minified’; // can NOT be handled by ‘AssetManager‘!
// Application config :
return [
// ...
’components’ => [
’assetManager’ => [
’bundles’ => [
’app\assets\LanguageAssetBundle’ => [
’baseUrl’ => ’http://some.cdn.com/files/i18n/en’ //
makes NO effect!
],
’app\assets\LargeFileAssetBundle’ => [
’baseUrl’ => ’http://some.cdn.com/files/large-files’ //
makes NO effect!
],
],
],
],
];
Asset Mapping
Sometimes you may want to “fix” incorrect/incompatible asset file paths used
in multiple asset bundles. For example, bundle A uses jquery.min.js version
130 CHAPTER 3. APPLICATION STRUCTURE
1.11.1, and bundle B uses jquery.js version 2.1.1. While you can fix the
problem by customizing each bundle, an easier way is to use the asset map
feature to map incorrect assets to the desired ones. To do so, configure the
yii\web\AssetManager::$assetMap property like the following:
return [
// ...
’components’ => [
’assetManager’ => [
’assetMap’ => [
’jquery.js’ => ’//ajax.googleapis.com/ajax/libs/jquery
/2.1.1/jquery.min.js’,
],
],
],
];
The keys of assetMap are the asset names that you want to fix, and the
values are the desired asset paths. When you register an asset bundle with a
view, each relative asset file in its css and js arrays will be examined against
this map. If any of the keys are found to be the last part of an asset file
(which is prefixed with yii\web\AssetBundle::$sourcePath if available),
the corresponding value will replace the asset and be registered with the view.
For example, the asset file my/path/to/jquery.js matches the key jquery.js.
Asset Publishing
As aforementioned, if an asset bundle is located in a directory that is not
Web accessible, its assets will be copied to a Web directory when the bundle
is being registered with a view. This process is called asset publishing, and
is done automatically by the asset manager.
By default, assets are published to the directory @webroot/assets which
corresponds to the URL @web/assets. You may customize this location by
configuring the basePath and baseUrl properties.
Instead of publishing assets by file copying, you may consider using sym-
bolic links, if your OS and Web server allow. This feature can be enabled by
setting linkAssets to be true.
return [
// ...
’components’ => [
’assetManager’ => [
’linkAssets’ => true,
],
],
];
3.11. ASSETS 131
With the above configuration, the asset manager will create a symbolic link
to the source path of an asset bundle when it is being published. This is
faster than file copying and can also ensure that the published assets are
always up-to-date.
Cache Busting
For Web application running in production mode, it is a common practice
to enable HTTP caching for assets and other static resources. A drawback
of this practice is that whenever you modify an asset and deploy it to pro-
duction, a user client may still use the old version due to the HTTP caching.
To overcome this drawback, you may use the cache busting feature, which
was introduced in version 2.0.3, by configuring yii\web\AssetManager like
the following:
return [
// ...
’components’ => [
’assetManager’ => [
’appendTimestamp’ => true,
],
],
];
By doing so, the URL of every published asset will be appended with its
last modification timestamp. For example, the URL to yii.js may look like
/assets/5515a87c/yii.js?v=1423448645", where the parameter v represents the
last modification timestamp of the yii.js file. Now if you modify an asset, its
URL will be changed, too, which causes the client to fetch the latest version
of the asset.
JavaScript plugins.
• yii\jui\JuiAsset: It includes the CSS and JavaScript files from the
jQuery UI library.
If your code depends on jQuery, jQuery UI or Bootstrap, you should use
these predefined asset bundles rather than creating your own versions. If the
default setting of these bundles do not satisfy your needs, you may customize
them as described in the Customizing Asset Bundle subsection.
When you register such an asset bundle with a view, the asset manager
will automatically run the pre-processor tools to convert assets in recognized
extended syntax into CSS/JavaScript. When the view finally renders a page,
it will include the CSS/JavaScript files in the page, instead of the original
assets in extended syntax.
Yii uses the file name extensions to identify which extended syntax an
asset is in. By default it recognizes the following syntax and file name ex-
tensions:
• LESS30 : .less
• SCSS31 : .scss
27
http://lesscss.org/
28
http://sass-lang.com/
29
http://www.typescriptlang.org/
30
http://lesscss.org/
31
http://sass-lang.com/
3.11. ASSETS 133
• Stylus32 : .styl
• CoffeeScript33 : .coffee
• TypeScript34 : .ts
Yii relies on the installed pre-processor tools to convert assets. For example,
to use LESS35 you should install the lessc pre-processor command.
You can customize the pre-processor commands and the supported ex-
tended syntax by configuring yii\web\AssetManager::$converter like the
following:
return [
’components’ => [
’assetManager’ => [
’converter’ => [
’class’ => ’yii\web\AssetConverter’,
’commands’ => [
’less’ => [’css’, ’lessc {from} {to} --no-color’],
’ts’ => [’js’, ’tsc --out {to} {from}’],
],
],
],
],
];
In the above, we specify the supported extended syntax via the yii\web
\AssetConverter::$commands property. The array keys are the file exten-
sion names (without leading dot), and the array values are the resulting asset
file extension names and the commands for performing the asset conversion.
The tokens {from} and {to} in the commands will be replaced with the source
asset file paths and the target asset file paths.
Info: There are other ways of working with assets in extended
syntax, besides the one described above. For example, you can
use build tools such as grunt36 to monitor and automatically
convert assets in extended syntax. In this case, you should list
the resulting CSS/JavaScript files in asset bundles rather than
the original files.
1. Find all the asset bundles in your application that you plan to combine
and compress.
2. Divide these bundles into one or a few groups. Note that each bundle
can only belong to a single group.
Using this approach, when you register an asset bundle in a view, it causes
the automatic registration of the new asset bundle for the group that the
original bundle belongs to. And as a result, the combined/compressed asset
files are included in the page, instead of the original ones.
An Example
That is, the asset bundle configuration array is saved in assets-prod.php for
production mode, and assets-dev.php for non-production mode.
You should modify this file and specify which bundles you plan to combine in
the bundles option. In the targets option you should specify how the bundles
should be divided into groups. You can specify one or multiple groups, as
aforementioned.
Note: Because the alias @webroot and @web are not available in
the console application, you should explicitly define them in the
configuration.
Info: Using the asset command is not the only option to auto-
mate the asset combining and compressing process. You can use
the excellent task runner tool grunt41 to achieve the same goal.
’app\assets\AdminAsset’
],
],
’allFrontEnd’ => [
’js’ => ’js/all-{hash}.js’,
’css’ => ’css/all-{hash}.css’,
’depends’ => [], // Include all remaining assets
],
],
...
];
As you can see, the asset bundles are divided into three groups: allShared,
allBackEndand allFrontEnd. They each depends on an appropriate set of asset
bundles. For example, allBackEnd depends on app\assets\AdminAsset. When
running asset command with this configuration, it will combine asset bundles
according to the above specification.
Info: You may leave the depends configuration empty for one of
the target bundle. By doing so, that particular asset bundle will
depend on all of the remaining asset bundles that other target
bundles do not depend on.
3.12 Extensions
Extensions are redistributable software packages specifically designed to be
used in Yii applications and provide ready-to-use features. For example, the
yiisoft/yii2-debug42 extension adds a handy debug toolbar at the bottom
of every page in your application to help you more easily grasp how the
pages are generated. You can use extensions to accelerate your development
process. You can also package your code as extensions to share with other
people your great work.
1. modify the composer.json file of your application and specify which ex-
tensions (Composer packages) you want to install.
Note that you may need to install Composer44 if you do not have it.
By default, Composer installs packages registered on Packagist45 - the
biggest repository for open source Composer packages. You can look for
extensions on Packagist. You may also create your own repository46 and
configure Composer to use it. This is useful if you are developing private
extensions that you want to share within your projects only.
Extensions installed by Composer are stored in the BasePath/vendor direct-
ory, where BasePath refers to the application’s base path. Because Composer
is a dependency manager, when it installs a package, it will also install all
its dependent packages.
For example, to install the yiisoft/yii2-imagine extension, modify your
composer.json like the following:
{
// ...
"require": {
// ... other dependencies
"yiisoft/yii2-imagine": "*"
}
}
After the installation, you should see the directory yiisoft/yii2-imagine under
BasePath/vendor. You should also see another directory imagine/imagine which
contains the installed dependent package.
Now you can use the installed extensions like they are part of your applic-
ation. The following example shows how you can use the yii\imagine\Image
class provided by the yiisoft/yii2-imagine extension:
use Yii;
use yii\imagine\Image;
1. download the extension archive files and unpack them in the vendor
directory.
If an extension does not have a class autoloader but follows the PSR-4 stand-
ard48 , you may use the class autoloader provided by Yii to autoload the
extension classes. All you need to do is just to declare a root alias for the
extension root directory. For example, assuming you have installed an ex-
tension in the directory vendor/mycompany/myext, and the extension classes are
under the myext namespace, then you can include the following code in your
application configuration:
[
’aliases’ => [
’@myext’ => ’@vendor/mycompany/myext’,
],
]
2. Under the root directory of the project, create a file named composer.
json as required by Composer. Please refer to the next subsection for
more details.
composer.json
Each Composer package must have a composer.json file in its root directory.
The file contains the metadata about the package. You may find complete
specification about this file in the Composer Manual52 . The following ex-
ample shows the composer.json file for the yiisoft/yii2-imagine extension:
{
// package name
"name": "yiisoft/yii2-imagine",
// package type
"type": "yii2-extension",
// package dependencies
"require": {
"yiisoft/yii2": "~2.0.0",
"imagine/imagine": "v0.5.0"
50
https://github.com
51
https://packagist.org/
52
https://getcomposer.org/doc/01-basic-usage.md#composer-json-project-setup
3.12. EXTENSIONS 143
},
Package Type It is important that you specify the package type of your
extension as yii2-extension so that the package can be recognized as a Yii
extension when being installed.
When a user runs composer install to install an extension, the file vendor
/yiisoft/extensions.php will be automatically updated to include the inform-
ation about the new extension. From this file, Yii applications can know
which extensions are installed (the information can be accessed via yii\base
\Application::$extensions).
{
// package dependencies
"require": {
"bower-asset/jquery": ">=1.11.*"
}
}
The above code states that the extension depends on the jquery Bower pack-
age. In general, you can use bower-asset/PackageName to refer to a Bower pack-
age in composer.json, and use npm-asset/PackageName to refer to a NPM package.
When Composer installs a Bower or NPM package, by default the package
content will be installed under the @vendor/bower/PackageName and @vendor/npm
/Packages directories, respectively. These two directories can also be referred
to using the shorter aliases @bower/PackageName and @npm/PackageName.
For more details about asset management, please refer to the Assets
section.
{
// ....
"autoload": {
"psr-4": {
"yii\\imagine\\": ""
}
}
}
You may list one or multiple root namespaces and their corresponding file
paths.
When the extension is installed in an application, Yii will create for each
listed root namespace an alias that refers to the directory corresponding to
the namespace. For example, the above autoload declaration will correspond
to an alias named @yii/imagine.
Recommended Practices
Because extensions are meant to be used by other people, you often need to
make an extra effort during development. Below we introduce some common
and recommended practices in creating high quality extensions.
Namespaces To avoid name collisions and make the classes in your ex-
tension autoloadable, you should use namespaces and name the classes in
3.12. EXTENSIONS 145
use yii\base\BootstrapInterface;
use yii\base\Application;
You then list this class in the composer.json file of your extension like follows,
{
// ...
"extra": {
"bootstrap": "myname\\mywidget\\MyBootstrapClass"
}
}
Testing You want your extension to run flawlessly without bringing prob-
lems to other people. To reach this goal, you should test your extension
before releasing it to public.
3.12. EXTENSIONS 147
It is recommended that you create various test cases to cover your exten-
sion code rather than relying on manual tests. Each time before you release
a new version of your extension, you may simply run these test cases to make
sure everything is in good shape. Yii provides testing support, which can
help you to more easily write unit tests, acceptance tests and functionality
tests. For more details, please refer to the Testing section.
Versioning You should give each release of your extension a version num-
ber (e.g. 1.0.1). We recommend you follow the semantic versioning58 prac-
tice when determining what version numbers should be used.
Releasing To let other people know about your extension, you need to
release it to the public.
If it is the first time you are releasing an extension, you should register
it on a Composer repository, such as Packagist59 . After that, all you need
to do is simply create a release tag (e.g. v1.0.1) on the VCS repository of
your extension and notify the Composer repository about the new release.
People will then be able to find the new release, and install or update the
extension through the Composer repository.
In the releases of your extension, in addition to code files, you should
also consider including the following to help other people learn about and
use your extension:
• A readme file in the package root directory: it describes what your
extension does and how to install and use it. We recommend you write
it in Markdown60 format and name the file as readme.md.
• A changelog file in the package root directory: it lists what changes
are made in each release. The file may be written in Markdown format
and named as changelog.md.
• An upgrade file in the package root directory: it gives the instructions
on how to upgrade from older releases of the extension. The file may
be written in Markdown format and named as upgrade.md.
• Tutorials, demos, screenshots, etc.: these are needed if your extension
provides many features that cannot be fully covered in the readme file.
• API documentation: your code should be well documented to allow
other people to more easily read and understand it. You may refer to
the BaseObject class file61 to learn how to document your code.
76
https://github.com/yiisoft/yii2-httpclient
77
https://github.com/yiisoft/yii2-imagine
78
http://imagine.readthedocs.org/
79
https://github.com/yiisoft/yii2-jui
80
http://jqueryui.com/
81
https://github.com/yiisoft/yii2-mongodb
82
http://www.mongodb.org/
83
https://github.com/yiisoft/yii2-redis
84
http://redis.io/
85
https://github.com/yiisoft/yii2-smarty
86
http://www.smarty.net/
87
https://github.com/yiisoft/yii2-sphinx
88
http://sphinxsearch.com
89
https://github.com/yiisoft/yii2-swiftmailer
90
http://swiftmailer.org/
91
https://github.com/yiisoft/yii2-twig
92
http://twig.sensiolabs.org/
150 CHAPTER 3. APPLICATION STRUCTURE
Chapter 4
Handling Requests
4.1 Overview
Each time when a Yii application handles a request, it undergoes a similar
workflow.
3. The application resolves the requested route with the help of the re-
quest application component.
5. The controller creates an action instance and performs the filters for
the action.
11. The response component sends the rendered result to the user’s browser.
151
152 CHAPTER 4. HANDLING REQUESTS
In this section, we will describe in detail how some of these steps work.
4.2 Bootstrapping
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.
In the entry script, class autoloaders for different libraries are registered.
This includes the Composer autoloader through its autoload.php file and the
Yii autoloader through its Yii class file. The entry script then loads the
application configuration and creates an application instance.
In the constructor of the application, the following bootstrapping work
is done:
Because the bootstrapping work has to be done before handling every re-
quest, it is very important to keep this process light and optimize it as much
as possible.
Try not to register too many bootstrapping components. A bootstrap-
ping component is needed only if it wants to participate the whole life cycle
of requesting handling. For example, if a module needs to register additional
URL parsing rules, it should be listed in the bootstrap property so that the
new URL rules can take effect before they are used to resolve requests.
In production mode, enable a bytecode cache, such as PHP OPcache1 or
APC2 , to minimize the time needed for including and parsing PHP files.
Some large applications have very complex application configurations
which are divided into many smaller configuration files. If this is the case,
consider caching the whole configuration array and loading it directly from
cache before creating the application instance in the entry script.
Depending on the urlManager configuration, the created URL may look like
one of the following (or other format). And if the created URL is requested
later, it will still be parsed back into the original route and query parameter
value.
/index.php?r=post%2Fview&id=100
/index.php/post/100
/posts/100
4.3.2 Routing
Routing involves two steps:
• the incoming request is parsed into a route and the associated query
parameters;
• a controller action corresponding to the parsed route is created to
handle the request.
When using the default URL format, parsing a request into a route is as
simple as getting the value of a GET query parameter named r.
When using the pretty URL format, the URL manager will examine the
registered URL rules to find matching one that can resolve the request into
4.3. ROUTING AND URL CREATION 155
2. Check if the controller map of the current module contains the cur-
rent ID. If so, a controller object will be created according to the
controller configuration found in the map, and Step 5 will be taken to
handle the rest part of the route.
5. The controller looks for the current ID in its action map. If found,
it creates an action according to the configuration found in the map.
Otherwise, the controller will attempt to create an inline action which
is defined by an action method corresponding to the current action ID.
Default Route
When a request is parsed into an empty route, the so-called default route
will be used, instead. By default, the default route is site/index, which
refers to the index action of the site controller. You may customize it by
configuring the defaultRoute property of the application in the application
configuration like the following:
[
// ...
’defaultRoute’ => ’main/index’,
];
Similar to the default route of the application, there is also a default route
for modules, so for example if there is a user module and the request is
156 CHAPTER 4. HANDLING REQUESTS
parsed into the route user the module’s defaultRoute is used to determine
the controller. By default the controller name is default. If no action is
specified in defaultRoute, the defaultAction property of the controller
is used to determine the action. In this example, the full route would be
user/default/index.
catchAll Route
Sometimes, you may want to put your Web application in maintenance mode
temporarily and display the same informational page for all requests. There
are many ways to accomplish this goal. But one of the simplest ways is to
configure the yii\web\Application::$catchAll property like the following
in the application configuration:
[
// ...
’catchAll’ => [’site/offline’],
];
With the above configuration, the site/offline action will be used to handle
all incoming requests.
The catchAll property should take an array whose first element specifies a
route, and the rest of the elements (name-value pairs) specify the parameters
to be bound to the action.
3
https://github.com/yiisoft/yii2-debug/blob/master/docs/guide/README.md
4.3. ROUTING AND URL CREATION 157
Note that in the above example, we assume the default URL format is being
used. If the pretty URL format is enabled, the created URLs will be different,
according to the URL rules in use.
The route passed to the yii\helpers\Url::to() method is context sens-
itive. It can be either a relative route or an absolute route which will be
normalized according to the following rules:
• If the route is an empty string, the currently requested route will be
used;
• If the route contains no slashes at all, it is considered to be an action
ID of the current controller and will be prepended with the uniqueId
value of the current controller;
• If the route has no leading slash, it is considered to be a route relative
to the current module and will be prepended with the uniqueId value
of the current module.
Starting from version 2.0.2, you may specify a route in terms of an alias. If
this is the case, the alias will first be converted into the actual route which
will then be turned into an absolute route according to the above rules.
For example, assume the current module is admin and the current con-
troller is post,
use yii\helpers\Url;
use yii\helpers\Url;
Besides the to() method, the yii\helpers\Url helper class also provides
several other convenient URL creation methods. For example,
use yii\helpers\Url;
URL Rules
A URL rule is a class implementing the yii\web\UrlRuleInterface, usually
yii\web\UrlRule. Each URL rule consists of a pattern used for matching
the path info part of URLs, a route, and a few query parameters. A URL
rule can be used to parse a request if its pattern matches the requested URL.
A URL rule can be used to create a URL if its route and query parameter
names match those that are given.
When the pretty URL format is enabled, the URL manager uses the URL
rules declared in its rules property to parse incoming requests and create
URLs. In particular, to parse an incoming request, the URL manager exam-
ines the rules in the order they are declared and looks for the first rule that
matches the requested URL. The matching rule is then used to parse the
URL into a route and its associated parameters. Similarly, to create a URL,
the URL manager looks for the first rule that matches the given route and
parameters and uses that to create a URL.
You can configure yii\web\UrlManager::$rules as an array with keys
being the patterns and values the corresponding routes. Each pattern-
route pair constructs a URL rule. For example, the following rules con-
figuration declares two URL rules. The first rule matches a URL posts and
160 CHAPTER 4. HANDLING REQUESTS
maps it into the route post/index. The second rule matches a URL matching
the regular expression post/(\d+) and maps it into the route post/view and
defines a query parameter named id.
’rules’ => [
’posts’ => ’post/index’,
’post/<id:\d+>’ => ’post/view’,
]
Info: The pattern in a rule is used to match the path info part of
a URL. For example, the path info of /index.php/post/100?source=
ad is post/100 (the leading and ending slashes are ignored) which
matches the pattern post/(\d+).
Besides declaring URL rules as pattern-route pairs, you may also declare
them as configuration arrays. Each configuration array is used to configure
a single URL rule object. This is often needed when you want to configure
other properties of a URL rule. For example,
’rules’ => [
// ...other url rules...
[
’pattern’ => ’posts’,
’route’ => ’post/index’,
’suffix’ => ’.json’,
],
]
By default if you do not specify the class option for a rule configuration,
it will take the default class yii\web\UrlRule, which is the default value
defined in yii\web\UrlManager::$ruleConfig.
Named Parameters
A URL rule can be associated with named query parameters which are spe-
cified in the pattern in the format of <ParamName:RegExp>, where ParamName
specifies the parameter name and RegExp is an optional regular expression
used to match parameter values. If RegExp is not specified, it means the
parameter value should be a string without any slash.
Note: You can only use regular expressions inside of parameters.
The rest of a pattern is considered plain text.
When a rule is used to parse a URL, it will fill the associated parameters with
values matching the corresponding parts of the URL, and these parameters
will be made available in $_GET later by the request application component.
When the rule is used to create a URL, it will take the values of the provided
parameters and insert them at the places where the parameters are declared.
Let’s use some examples to illustrate how named parameters work. As-
sume we have declared the following three URL rules:
4.3. ROUTING AND URL CREATION 161
’rules’ => [
’posts/<year:\d{4}>/<category>’ => ’post/index’,
’posts’ => ’post/index’,
’post/<id:\d+>’ => ’post/view’,
]
Parameterizing Routes
You can embed parameter names in the route of a URL rule. This allows a
URL rule to be used for matching multiple routes. For example, the following
rules embed controller and action parameters in the routes.
’rules’ => [
’<controller:(post|comment)>/create’ => ’<controller>/create’,
’<controller:(post|comment)>/<id:\d+>/<action:(update|delete)>’ => ’<
controller>/<action>’,
’<controller:(post|comment)>/<id:\d+>’ => ’<controller>/view’,
’<controller:(post|comment)>s’ => ’<controller>/index’,
162 CHAPTER 4. HANDLING REQUESTS
The above rule can be used to parse or create any of the following URLs:
• /index.php/posts: page is 1, tag is ‘’.
• /index.php/posts/2: page is 2, tag is ‘’.
• /index.php/posts/2/news: page is 2, tag is ’news’.
• /index.php/posts/news: page is 1, tag is ’news’.
Without using optional parameters, you would have to create 4 rules to
achieve the same result.
different Web server names. For example, the following rules will parse the
URL http://admin.example.com/login into the route admin/user/login and http
://www.example.com/login into site/login.
’rules’ => [
’http://admin.example.com/login’ => ’admin/user/login’,
’http://www.example.com/login’ => ’site/login’,
]
You can also embed parameters in the server names to extract dynamic in-
formation from them. For example, the following rule will parse the URL http
://en.example.com/posts into the route post/index and the parameter language
=en.
’rules’ => [
’http://<language:\w+>.example.com/posts’ => ’post/index’,
]
Since version 2.0.11, you may also use protocol relative patterns that work
for both, http and https. The syntax is the same as above but skipping the
http: part, e.g.: ’//www.example.com/login’ => ’site/login’.
Note: Rules with server names should not include the subfolder
of the entry script in their patterns. For example, if the applica-
tions entry script is at http://www.example.com/sandbox/blog/index.
php, then you should use the pattern http://www.example.com/posts
instead of http://www.example.com/sandbox/blog/posts. This will al-
low your application to be deployed under any directory without
the need to change your url rules. Yii will automatically detect
the base url of the application.
URL Suffixes
You may want to add suffixes to the URLs for various purposes. For example,
you may add .html to the URLs so that they look like URLs for static HTML
pages; you may also add .json to the URLs to indicate the expected content
type of the response. You can achieve this goal by configuring the yii
\web\UrlManager::$suffix property like the following in the application
configuration:
[
// ...
’components’ => [
’urlManager’ => [
’enablePrettyUrl’ => true,
// ...
’suffix’ => ’.html’,
’rules’ => [
// ...
],
],
164 CHAPTER 4. HANDLING REQUESTS
],
]
The above configuration will allow the URL manager to recognize requested
URLs and also create URLs with .html as their suffix.
Tip: You may set / as the URL suffix so that the URLs all end
with a slash.
Sometimes you may want to use different suffixes for different URLs. This
can be achieved by configuring the suffix property of individual URL rules.
When a URL rule has this property set, it will override the suffix setting at
the URL manager level. For example, the following configuration contains a
customized URL rule which uses .json as its suffix instead of the global .html
suffix.
[
’components’ => [
’urlManager’ => [
’enablePrettyUrl’ => true,
// ...
’suffix’ => ’.html’,
’rules’ => [
// ...
[
’pattern’ => ’posts’,
’route’ => ’post/index’,
’suffix’ => ’.json’,
],
],
],
],
]
HTTP Methods
When implementing RESTful APIs, it is commonly needed that the same
URL be parsed into different routes according to the HTTP methods being
used. This can be easily achieved by prefixing the supported HTTP methods
to the patterns of the rules. If a rule supports multiple HTTP methods,
separate the method names with commas. For example, the following rules
have the same pattern post/<id:\d+> with different HTTP method support.
A request for PUT post/100 will be parsed into post/update, while a request for
GET post/100 will be parsed into post/view.
4.3. ROUTING AND URL CREATION 165
’rules’ => [
’PUT,POST post/<id:\d+>’ => ’post/update’,
’DELETE post/<id:\d+>’ => ’post/delete’,
’post/<id:\d+>’ => ’post/view’,
]
URL rules can be dynamically added to the URL manager. This is often
needed by redistributable modules which want to manage their own URL
rules. In order for the dynamically added rules to take effect during the
routing process, you should add them during the bootstrapping stage of
the application. For modules, this means they should implement yii\base
\BootstrapInterface and add the rules in the bootstrap() method like
the following:
public function bootstrap($app)
{
$app->getUrlManager()->addRules([
// rule declarations here
], false);
}
Despite the fact that the default yii\web\UrlRule class is flexible enough
for the majority of projects, there are situations when you have to create
your own rule classes. For example, in a car dealer Web site, you may want
to support the URL format like /Manufacturer/Model, where both Manufacturer
and Model must match some data stored in a database table. The default
rule class will not work here because it relies on statically declared patterns.
We can create the following URL rule class to solve this problem.
166 CHAPTER 4. HANDLING REQUESTS
<?php
namespace app\components;
use yii\web\UrlRuleInterface;
use yii\base\BaseObject;
adds or removes trailing slashes depending on whether the suffix has a trail-
ing slash or not, and redirects to the normalized version of the URL using
permanent redirection4 . The normalizer can be configured globally for the
URL manager or individually for each rule - by default each rule will use
the normalizer from URL manager. You can set UrlRule::$normalizer to
false to disable normalization for particular URL rule.
The following shows an example configuration for the UrlNormalizer:
’urlManager’ => [
’enablePrettyUrl’ => true,
’showScriptName’ => false,
’enableStrictParsing’ => true,
’suffix’ => ’.html’,
’normalizer’ => [
’class’ => ’yii\web\UrlNormalizer’,
// use temporary redirection instead of permanent for debugging
’action’ => UrlNormalizer::ACTION_REDIRECT_TEMPORARY,
],
’rules’ => [
// ...other rules...
[
’pattern’ => ’posts’,
’route’ => ’post/index’,
’suffix’ => ’/’,
’normalizer’ => false, // disable normalizer for this rule
],
[
’pattern’ => ’tags’,
’route’ => ’tag/index’,
’normalizer’ => [
// do not collapse consecutive slashes for this rule
’collapseSlashes’ => false,
],
],
],
]
the URL rules so that more specific and/or more commonly used rules are
placed before less used ones.
If some URL rules share the same prefix in their patterns or routes,
you may consider using yii\web\GroupUrlRule so that they can be more
efficiently examined by URL manager as a group. This is often the case when
your application is composed by modules, each having its own set of URL
rules with module ID as their common prefixes.
4.4 Requests
Requests made to an application are represented in terms of yii\web\Request
objects which provide information such as request parameters, HTTP head-
ers, cookies, etc. For a given request, you can get access to the corresponding
request object via the request application component which is an instance of
yii\web\Request, by default. In this section, we will describe how you can
make use of this component in your applications.
$get = $request->get();
// equivalent to: $get = $_GET;
$id = $request->get(’id’);
// equivalent to: $id = isset($_GET[’id’]) ? $_GET[’id’] : null;
$post = $request->post();
// equivalent to: $post = $_POST;
$name = $request->post(’name’);
// equivalent to: $name = isset($_POST[’name’]) ? $_POST[’name’] : null;
• queryString: returns id=100, which is the part after the question mark.
• baseUrl: returns /admin, which is the part after the host info and before
the entry script name.
• scriptUrl: returns /admin/index.php, which is the URL without path
info and query string.
• serverName: returns example.com, which is the host name in the URL.
• serverPort: returns 80, which is the port used by the Web server.
$userHost = Yii::$app->request->userHost;
$userIP = Yii::$app->request->userIP;
The IP is sent by the proxy in the X-Forwarded-For header by default, and the
protocol (http or https) is sent in X-Forwarded-Proto.
In case your proxies are using different headers you can use the request
configuration to adjust these, e.g.:
’request’ => [
// ...
’trustedHosts’ => [
’10.0.2.0/24’ => [
’X-ProxyUser-Ip’,
’Front-End-Https’,
],
],
’secureHeaders’ => [
’X-Forwarded-For’,
’X-Forwarded-Host’,
’X-Forwarded-Proto’,
’X-Proxy-User-Ip’,
’Front-End-Https’,
],
’ipHeaders’ => [
’X-Proxy-User-Ip’,
],
’secureProtocolHeaders’ => [
’Front-End-Https’ => [’on’]
],
172 CHAPTER 4. HANDLING REQUESTS
],
With the above configuration, all headers listed in secureHeaders are filtered
from the request, except the X-ProxyUser-Ip and Front-End-Https headers in
case the request is made by the proxy. In that case the former is used to
retrieve the user IP as configured in ipHeaders and the latter will be used to
determine the result of yii\web\Request::getIsSecureConnection().
4.5 Responses
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. The ultimate goal
of Web application development is essentially to build such response objects
upon various requests.
In most cases you should mainly deal with the response application com-
ponent which is an instance of yii\web\Response, by default. However, Yii
also allows you to create your own response objects and send them to end
users as we will explain in the following.
In this section, we will describe how to compose and send responses to
end users.
However, in most cases you do not need to explicitly set the status code.
This is because the default value of yii\web\Response::$statusCode is
200. And if you want to indicate the request is unsuccessful, you may throw
an appropriate HTTP exception like the following:
throw new \yii\web\NotFoundHttpException;
When the error handler catches an exception, it will extract the status
code from the exception and assign it to the response. For the yii\web
\NotFoundHttpException above, it is associated with the HTTP status 404.
The following HTTP exceptions are predefined in Yii:
• yii\web\BadRequestHttpException: status code 400.
• yii\web\ConflictHttpException: status code 409.
5
https://tools.ietf.org/html/rfc2616#section-10
4.5. RESPONSES 173
// remove Pragma header(s) and return the removed Pragma header values in an
array
$values = $headers->remove(’Pragma’);
Info: Header names are case insensitive. And the newly re-
gistered headers are not sent to the user until the yii\web\Response
::send() method is called.
If your data needs to be formatted before sending it to end users, you should
set both of the format and data properties. The format property specifies
in which format the data should be formatted. For example,
174 CHAPTER 4. HANDLING REQUESTS
$response = Yii::$app->response;
$response->format = \yii\web\Response::FORMAT_JSON;
$response->data = [’message’ => ’hello world’];
Yii supports the following formats out of the box, each implemented by a
formatter class. You can customize these formatters or add new ones by
configuring the yii\web\Response::$formatters property.
• HTML: implemented by yii\web\HtmlResponseFormatter.
• XML: implemented by yii\web\XmlResponseFormatter.
• JSON: implemented by yii\web\JsonResponseFormatter.
• JSONP: implemented by yii\web\JsonResponseFormatter.
• RAW: use this format if you want to send the response directly without
applying any formatting.
While the response body can be set explicitly as shown above, in most cases
you may set it implicitly by the return value of action methods. A common
use case is like the following:
public function actionIndex()
{
return $this->render(’index’);
}
The index action above returns the rendering result of the index view. The
return value will be taken by the response component, formatted and then
sent to end users.
Because by default the response format is HTML, you should only return
a string in an action method. If you want to use a different response format,
you should set it first before returning the data. For example,
public function actionInfo()
{
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
return [
’message’ => ’hello world’,
’code’ => 100,
];
}
]);
}
Note: If you are creating your own response objects, you will
not be able to take advantage of the configurations that you
set for the response component in the application configuration.
You can, however, use dependency injection to apply a common
configuration to your new response objects.
In the above code, the action method returns the result of the redirect()
method. As explained before, the response object returned by an action
method will be used as the response sending to end users.
In places other than an action method, you should call yii\web\Response
::redirect() directly followed by a chained call to the yii\web\Response
::send() method to ensure no extra content will be appended to the re-
sponse.
\Yii::$app->response->redirect(’http://example.com/new’, 301)->send();
When the current request is an AJAX request, sending a Location header will
not automatically cause the browser to redirect. To solve this problem, the
yii\web\Response::redirect() method sets an X-Redirect header with the
redirection URL as its value. On the client-side, you may write JavaScript
code to read this header value and redirect the browser accordingly.
176 CHAPTER 4. HANDLING REQUESTS
If you are calling the file sending method in places other than an action
method, you should also call the yii\web\Response::send() method after-
wards to ensure no extra content will be appended to the response.
\Yii::$app->response->sendFile(’path/to/file.txt’)->send();
Some Web servers have a special file sending support called X-Sendfile. The
idea is to redirect the request for a file to the Web server which will directly
serve the file. As a result, the Web application can terminate earlier while
the Web server is sending the file. To use this feature, you may call the
yii\web\Response::xSendFile(). The following list summarizes how to
enable the X-Sendfile feature for some popular Web servers:
• Apache: X-Sendfile6
• Lighttpd v1.4: X-LIGHTTPD-send-file7
• Lighttpd v1.5: X-Sendfile8
6
http://tn123.org/mod_xsendfile
7
http://redmine.lighttpd.net/projects/lighttpd/wiki/X-LIGHTTPD-send-file
8
http://redmine.lighttpd.net/projects/lighttpd/wiki/X-LIGHTTPD-send-file
4.6. SESSIONS AND COOKIES 177
• Nginx: X-Accel-Redirect9
• Cherokee: X-Sendfile and X-Accel-Redirect10
4.6.1 Sessions
Like requests and responses, you can get access to sessions via the session
application component which is an instance of yii\web\Session, by default.
// open a session
$session->open();
// close a session
$session->close();
You can call open() and close() multiple times without causing errors;
internally the methods will first check if the session is already open.
Info: When you access session data through the session compon-
ent, a session will be automatically opened if it has not been done
so before. This is different from accessing session data through
$_SESSION, which requires an explicit call of session_start().
When working with session data that are arrays, the session component has
a limitation which prevents you from directly modifying an array element.
For example,
$session = Yii::$app->session;
You can use one of the following workarounds to solve this problem:
$session = Yii::$app->session;
// get the whole array first, modify it and then save it back
$captcha = $session[’captcha’];
$captcha[’number’] = 5;
$captcha[’lifetime’] = 3600;
$session[’captcha’] = $captcha;
For better performance and code readability, we recommend the last work-
around. That is, instead of storing an array as a single session variable, you
180 CHAPTER 4. HANDLING REQUESTS
store each array element as a session variable which shares the same key
prefix with other array elements.
To learn how to configure and use these component classes, please refer to
their API documentation. Below is an example showing how to configure
yii\web\DbSession in the application configuration to use a database table
for session storage:
return [
’components’ => [
’session’ => [
’class’ => ’yii\web\DbSession’,
// ’db’ => ’mydb’, // the application component ID of the DB
connection. Defaults to ’db’.
// ’sessionTable’ => ’my_session’, // session table name.
Defaults to ’session’.
],
],
];
You also need to create the following database table to store session data:
CREATE TABLE session
(
id CHAR(40) NOT NULL PRIMARY KEY,
expire INTEGER,
11
http://redis.io/
12
http://www.mongodb.org/
4.6. SESSIONS AND COOKIES 181
data BLOB
)
where ‘BLOB’ refers to the BLOB-type of your preferred DBMS. Below are
the BLOB types that can be used for some popular DBMS:
• MySQL: LONGBLOB
• PostgreSQL: BYTEA
• MSSQL: BLOB
use yii\db\Migration;
Flash Data
Flash data is a special kind of session data which, once set in one request, will
only be available during the next request and will be automatically deleted
afterwards. Flash data is most commonly used to implement messages that
should only be displayed to end users once, such as a confirmation message
displayed after a user successfully submits a form.
You can set and access flash data through the session application com-
ponent. For example,
$session = Yii::$app->session;
182 CHAPTER 4. HANDLING REQUESTS
// Request #1
// set a flash message named as "postDeleted"
$session->setFlash(’postDeleted’, ’You have successfully deleted your post.’
);
// Request #2
// display the flash message named "postDeleted"
echo $session->getFlash(’postDeleted’);
// Request #3
// $result will be false since the flash message was automatically deleted
$result = $session->hasFlash(’postDeleted’);
Like regular session data, you can store arbitrary data as flash data.
When you call yii\web\Session::setFlash(), it will overwrite any ex-
isting flash data that has the same name. To append new flash data to
an existing message of the same name, you may call yii\web\Session::
addFlash() instead. For example:
$session = Yii::$app->session;
// Request #1
// add a few flash messages under the name of "alerts"
$session->addFlash(’alerts’, ’You have successfully deleted your post.’);
$session->addFlash(’alerts’, ’You have successfully added a new friend.’);
$session->addFlash(’alerts’, ’You are promoted.’);
// Request #2
// $alerts is an array of the flash messages under the name of "alerts"
$alerts = $session->getFlash(’alerts’);
4.6.2 Cookies
Yii represents each cookie as an object of yii\web\Cookie. Both yii\web
\Request and yii\web\Response maintain a collection of cookies via the
4.6. SESSIONS AND COOKIES 183
property named cookies. The cookie collection in the former represents the
cookies submitted in a request, while the cookie collection in the latter rep-
resents the cookies that are to be sent to the user.
The part of the application dealing with request and response directly is
controller. Therefore, cookies should be read and sent in controller.
Reading Cookies
You can get the cookies in the current request using the following code:
// get the cookie collection (yii\web\CookieCollection) from the "request"
component
$cookies = Yii::$app->request->cookies;
// get the "language" cookie value. If the cookie does not exist, return "en
" as the default value.
$language = $cookies->getValue(’language’, ’en’);
Sending Cookies
You can send cookies to end users using the following code:
// get the cookie collection (yii\web\CookieCollection) from the "response"
component
$cookies = Yii::$app->response->cookies;
// remove a cookie
$cookies->remove(’language’);
// equivalent to the following
unset($cookies[’language’]);
184 CHAPTER 4. HANDLING REQUESTS
Besides the name, value properties shown in the above examples, the yii
\web\Cookie class also defines other properties to fully represent all avail-
able cookie information, such as domain, expire. You may configure these
properties as needed to prepare a cookie and then add it to the response’s
cookie collection.
Cookie Validation
When you are reading and sending cookies through the request and response
components as shown in the last two subsections, you enjoy the added
security of cookie validation which protects cookies from being modified on
the client-side. This is achieved by signing each cookie with a hash string,
which allows the application to tell if a cookie has been modified on the
client-side. If so, the cookie will NOT be accessible through the cookie
collection of the request component.
Cookie validation is enabled by default. You can disable it by setting the yii
\web\Request::$enableCookieValidation property to be false, although
we strongly recommend you do not do so.
Note: Cookies that are directly read/sent via $_COOKIE and setcookie
()will NOT be validated.
With the above configuration, the number of source code lines to be displayed
in exception pages will be up to 20.
As aforementioned, the error handler turns all non-fatal PHP errors into
catchable exceptions. This means you can use the following code to deal
with PHP errors:
use Yii;
use yii\base\ErrorException;
try {
10/0;
} catch (ErrorException $e) {
Yii::warning("Division by zero.");
}
// execution continues...
186 CHAPTER 4. HANDLING REQUESTS
If you want to show an error page telling the user that his request is invalid
or unexpected, you may simply throw an HTTP exception, such as yii\web
\NotFoundHttpException. The error handler will correctly set the HTTP
status code of the response and use an appropriate error view to display the
error message.
use yii\web\NotFoundHttpException;
use Yii;
use yii\web\Controller;
The above code defines the error action using the yii\web\ErrorAction
class which renders an error using a view named error.
Besides using yii\web\ErrorAction, you may also define the error ac-
tion using an action method like the following,
public function actionError()
{
$exception = Yii::$app->errorHandler->exception;
if ($exception !== null) {
return $this->render(’error’, [’exception’ => $exception]);
}
}
Info: If you are using the basic project template or the ad-
vanced project template14 , the error action and the error view
are already defined for you.
14
https://github.com/yiisoft/yii2-app-advanced/blob/master/docs/guide/
README.md
188 CHAPTER 4. HANDLING REQUESTS
{
"name": "Not Found Exception",
"message": "The requested resource was not found.",
"code": 0,
"status": 404
}
You may customize the error response format by responding to the beforeSend
event of the response component in the application configuration:
return [
// ...
’components’ => [
’response’ => [
’class’ => ’yii\web\Response’,
’on beforeSend’ => function ($event) {
$response = $event->sender;
if ($response->data !== null) {
$response->data = [
’success’ => $response->isSuccessful,
’data’ => $response->data,
];
$response->statusCode = 200;
}
},
],
],
];
The above code will reformat the error response like the following:
4.8. LOGGING 189
HTTP/1.1 200 OK
Date: Sun, 02 Mar 2014 05:31:43 GMT
Server: Apache/2.2.26 (Unix) DAV/2 PHP/5.4.20 mod_ssl/2.2.26 OpenSSL/0.9.8y
Transfer-Encoding: chunked
Content-Type: application/json; charset=UTF-8
{
"success": false,
"data": {
"name": "Not Found Exception",
"message": "The requested resource was not found.",
"code": 0,
"status": 404
}
}
4.8 Logging
Yii provides a powerful logging framework that is highly customizable and ex-
tensible. Using this framework, you can easily log various types of messages,
filter them, and gather them at different targets, such as files, databases,
emails.
Using the Yii logging framework involves the following steps:
• Record log messages at various places in your code;
• Configure log targets in the application configuration to filter and ex-
port log messages;
• Examine the filtered logged messages exported by different targets (e.g.
the Yii debugger).
In this section, we will mainly describe the first two steps.
the following example records a trace message under the default category
application:
Yii::debug(’start calculating average revenue’);
To better organize and filter log messages, it is recommended that you specify
an appropriate category for each log message. You may choose a hierarchical
naming scheme for categories, which will make it easier for log targets to filter
messages based on their categories. A simple yet effective naming scheme is
to use the PHP magic constant __METHOD__ for the category names. This is
also the approach used in the core Yii framework code. For example,
Yii::debug(’start calculating average revenue’, __METHOD__);
The __METHOD__ constant evaluates as the name of the method (prefixed with
the fully qualified class name) where the constant appears. For example,
it is equal to the string ’app\controllers\RevenueController::calculate’ if the
above line of code is called within this method.
In the above code, two log targets are registered in the yii\log\Dispatcher
::$targets property:
• the first target selects error and warning messages and saves them in
a database table;
• the second target selects error messages under the categories whose
names start with yii\db\, and sends them in an email to both admin@example
.com and [email protected].
Yii comes with the following built-in log targets. Please refer to the API
documentation about these classes to learn how to configure and use them.
• yii\log\DbTarget: stores log messages in a database table.
• yii\log\EmailTarget: sends log messages to pre-specified email ad-
dresses.
• yii\log\FileTarget: saves log messages in files.
• yii\log\SyslogTarget: saves log messages to syslog by calling the
PHP function syslog().
In the following, we will describe the features common to all log targets.
Message Filtering
For each log target, you can configure its levels and categories properties
to specify which severity levels and categories of the messages the target
192 CHAPTER 4. HANDLING REQUESTS
should process.
The levels property takes an array consisting of one or several of the
following values:
• error: corresponding to messages logged by Yii::error().
• warning: corresponding to messages logged by Yii::warning().
• info: corresponding to messages logged by Yii::info().
• trace: corresponding to messages logged by Yii::debug().
• profile: corresponding to messages logged by Yii::beginProfile()
and Yii::endProfile(), which will be explained in more details in
the Profiling subsection.
If you do not specify the levels property, it means the target will process
messages of any severity level.
The categories property takes an array consisting of message category
names or patterns. A target will only process messages whose category can
be found or match one of the patterns in this array. A category pattern
is a category name prefix with an asterisk * at its end. A category name
matches a category pattern if it starts with the same prefix of the pattern.
For example, yii\db\Command::execute and yii\db\Command::query are used as
category names for the log messages recorded in the yii\db\Command class.
They both match the pattern yii\db\*.
If you do not specify the categories property, it means the target will
process messages of any category.
Besides whitelisting the categories by the categories property, you may
also blacklist certain categories by the except property. If the category of
a message is found or matches one of the patterns in this property, it will
NOT be processed by the target.
The following target configuration specifies that the target should only
process error and warning messages under the categories whose names match
either yii\db\* or yii\web\HttpException:*, but not yii\web\HttpException:404.
[
’class’ => ’yii\log\FileTarget’,
’levels’ => [’error’, ’warning’],
’categories’ => [
’yii\db\*’,
’yii\web\HttpException:*’,
],
’except’ => [
’yii\web\HttpException:404’,
],
]
Message Formatting
Log targets export the filtered log messages in a certain format. For example,
if you install a log target of the class yii\log\FileTarget, you may find a
log message similar to the following in the runtime/log/app.log file:
2014-10-04 18:10:15 [::1][][-][trace][yii\base\Module::getModule] Loading
module: debug
Besides message prefixes, log targets also append some context informa-
tion to each batch of log messages. By default, the values of these global
PHP variables are included: $_GET, $_POST, $_FILES, $_COOKIE, $_SESSION and
$_SERVER. You may adjust this behavior by configuring the yii\log\Target
::$logVars property with the names of the global variables that you want
to include by the log target. For example, the following log target configur-
ation specifies that only the value of the $_SERVER variable will be appended
to the log messages.
[
’class’ => ’yii\log\FileTarget’,
’logVars’ => [’_SERVER’],
]
You may configure logVars to be an empty array to totally disable the in-
clusion of context information. Or if you want to implement your own way
of providing context information, you may override the yii\log\Target::
getContextMessage() method.
194 CHAPTER 4. HANDLING REQUESTS
When the logger object flushes log messages to log targets, they do not
get exported immediately. Instead, the message exporting only occurs when
a log target accumulates certain number of the filtered messages. You can
4.8. LOGGING 195
Because of the flushing and exporting level setting, by default when you call
Yii::debug() or any other logging method, you will NOT see the log message
immediately in the log targets. This could be a problem for some long-
running console applications. To make each log message appear immediately
in the log targets, you should set both flushInterval and exportInterval
to be 1, as shown below:
return [
’bootstrap’ => [’log’],
’components’ => [
’log’ => [
’flushInterval’ => 1,
’targets’ => [
[
’class’ => ’yii\log\FileTarget’,
’exportInterval’ => 1,
],
],
],
],
];
The above code requires you to name a target as file, as shown below by
using string keys in the targets array:
return [
’bootstrap’ => [’log’],
’components’ => [
’log’ => [
’targets’ => [
’file’ => [
’class’ => ’yii\log\FileTarget’,
],
’db’ => [
196 CHAPTER 4. HANDLING REQUESTS
Since version 2.0.13, you may configure enabled with a callable to define a
dynamic condition for whether the log target should be enabled or not. See
the documentation of yii\log\Target::setEnabled() for an example.
Tip: Instead of creating your own loggers you may try any PSR-
3 compatible logger such as Monolog15 by using PSR log target
extension16 .
\Yii::endProfile(’myBenchmark’);
where myBenchmark stands for a unique token identifying a code block. Later
when you examine the profiling result, you will use this token to locate the
time spent by the corresponding code block.
It is important to make sure that the pairs of beginProfile and endProfile
are properly nested. For example,
\Yii::beginProfile(’block1’);
15
https://github.com/Seldaek/monolog
16
https://github.com/samdark/yii2-psr-log-target
4.8. LOGGING 197
\Yii::beginProfile(’block2’);
// some other code to be profiled
\Yii::endProfile(’block2’);
\Yii::endProfile(’block1’);
Key Concepts
5.1 Components
Components are the main building blocks of Yii applications. Components
are instances of yii\base\Component, or an extended class. The three main
features that components provide to other classes are:
• Properties
• Events
• Behaviors
Separately and combined, these features make Yii classes much more custom-
izable and easier to use. For example, the included yii\jui\DatePicker, a
user interface component, can be used in a view to generate an interactive
date picker:
use yii\jui\DatePicker;
echo DatePicker::widget([
’language’ => ’ru’,
’name’ => ’country’,
’clientOptions’ => [
’dateFormat’ => ’yy-mm-dd’,
],
]);
The widget’s properties are easily writable because the class extends yii
\base\Component.
While components are very powerful, they are a bit heavier than normal
objects, due to the fact that it takes extra memory and CPU time to support
event and behavior functionality in particular. If your components do not
need these two features, you may consider extending your component class
from yii\base\BaseObject instead of yii\base\Component. Doing so will
make your components as efficient as normal PHP objects, but with added
support for properties.
When extending your class from yii\base\Component or yii\base\BaseObject,
199
200 CHAPTER 5. KEY CONCEPTS
namespace yii\components\MyClass;
use yii\base\BaseObject;
parent::__construct($config);
}
The first three steps all happen within the object’s constructor. This means
that once you get a class instance (i.e., an object), that object has already
been initialized to a proper, reliable state.
5.2 Properties
In PHP, class member variables are also called properties. These variables
are part of the class definition, and are used to represent the state of a class
instance (i.e., to differentiate one instance of the class from another). In
practice, you may often want to handle the reading or writing of properties
in special ways. For example, you may want to always trim a string when
it is being assigned to a label property. You could use the following code to
achieve this task:
$object->label = trim($label);
The drawback of the above code is that you would have to call trim() every-
where in your code where you might set the label property. If, in the future,
the label property gets a new requirement, such as the first letter must be
capitalized, you would again have to modify every bit of code that assigns a
value to label. The repetition of code leads to bugs, and is a practice you
want to avoid as much as possible.
To solve this problem, Yii introduces a base class called yii\base\BaseObject
that supports defining properties based on getter and setter class meth-
ods. If a class needs that functionality, it should extend from yii\base
\BaseObject, or from a child class.
A getter method is a method whose name starts with the word get; a setter
method starts with set. The name after the get or set prefix defines the name
of a property. For example, a getter getLabel() and/or a setter setLabel()
defines a property named label, as shown in the following code:
namespace app\components;
use yii\base\BaseObject;
To be clear, the getter and setter methods create the property label, which
in this case internally refers to a private property named _label.
Properties defined by getters and setters can be used like class member
variables. The main difference is that when such property is being read,
the corresponding getter method will be called; when the property is be-
ing assigned a value, the corresponding setter method will be called. For
example:
// equivalent to $label = $object->getLabel();
$label = $object->label;
// equivalent to $object->setLabel(’abc’);
$object->label = ’abc’;
5.3 Events
Events allow you to inject custom code into existing code at certain execution
points. You can attach custom code to an event so that when the event is
triggered, the code gets executed automatically. For example, a mailer object
may trigger a messageSent event when it successfully sends a message. If you
want to keep track of the messages that are successfully sent, you could then
simply attach the tracking code to the messageSent event.
Yii introduces a base class called yii\base\Component to support events.
If a class needs to trigger events, it should extend from yii\base\Component,
or from a child class.
Through the $event parameter, an event handler may get the following in-
formation about the event that occurred:
• event name;
• event sender: the object whose trigger() method was called;
• custom data: the data that is provided when attaching the event
handler (to be explained next).
You may also attach event handlers through configurations. For more details,
please refer to the Configurations section.
When attaching an event handler, you may provide additional data as
the third parameter to yii\base\Component::on(). The data will be made
available to the handler when the event is triggered and the handler is called.
For example:
// The following code will display "abc" when the event is triggered
// because $event->data contains the data passed as the 3rd argument to "on"
$foo->on(Foo::EVENT_HELLO, ’function_name’, ’abc’);
function function_name($event) {
echo $event->data;
}
5.3. EVENTS 205
use yii\base\Component;
use yii\base\Event;
With the above code, any calls to bar() will trigger an event named hello.
Tip: It is recommended to use class constants to represent event
names. In the above example, the constant EVENT_HELLO repres-
ents the hello event. This approach has three benefits. First,
it prevents typos. Second, it can make events recognizable for
IDE auto-completion support. Third, you can tell what events
are supported in a class by simply checking its constant declara-
tions.
206 CHAPTER 5. KEY CONCEPTS
Sometimes when triggering an event you may want to pass along additional
information to the event handlers. For example, a mailer may want to pass
the message information to the handlers of the messageSent event so that the
handlers can know the particulars of the sent messages. To do so, you can
provide an event object as the second parameter to the yii\base\Component
::trigger() method. The event object must be an instance of the yii\base
\Event class or a child class. For example:
namespace app\components;
use yii\base\Component;
use yii\base\Event;
Note that in general you should not try to detach an anonymous function
unless you store it somewhere when it is attached to the event. In the above
example, it is assumed that the anonymous function is stored as a variable
$anonymousFunction.
To detach all handlers from an event, simply call yii\base\Component
::off() without the second parameter:
$foo->off(Foo::EVENT_HELLO);
Event::on(ActiveRecord::className(), ActiveRecord::EVENT_AFTER_INSERT,
function ($event) {
Yii::debug(get_class($event->sender) . ’ is inserted’);
});
Event::trigger(Foo::className(), Foo::EVENT_HELLO);
interface DanceEventInterface
{
const EVENT_DANCE = ’dance’;
}
But please notice, that you can not trigger all the classes, that implement
the interface:
// DOES NOT WORK. Classes that implement this interface will NOT be
triggered.
Event::trigger(’app\interfaces\DanceEventInterface’, DanceEventInterface::
EVENT_DANCE);
A benefit of using global events is that you do not need an object when at-
taching a handler to the event which will be triggered by the object. Instead,
the handler attachment and the event triggering are both done through the
Singleton (e.g. the application instance).
210 CHAPTER 5. KEY CONCEPTS
Wildcard patterns can be used for class-level events as well. For example:
use yii\base\Event;
use Yii;
This allows you catching all application events by single handler using fol-
lowing code:
use yii\base\Event;
use Yii;
Note: usage wildcards for event handlers setup may reduce the
application performance. It is better to be avoided if possible.
use Yii;
5.4 Behaviors
Behaviors are instances of yii\base\Behavior, or of a child class. Beha-
viors, also known as mixins2 , allow you to enhance the functionality of an
existing component class without needing to change the class’s inheritance.
Attaching a behavior to a component “injects” the behavior’s methods and
properties into the component, making those methods and properties ac-
cessible as if they were defined in the component class itself. Moreover, a
behavior can respond to the events triggered by the component, which allows
behaviors to also customize the normal code execution of the component.
use yii\base\Behavior;
private $_prop2;
use yii\db\ActiveRecord;
use yii\base\Behavior;
];
}
The events() method should return a list of events and their corresponding
handlers. The above example declares that the EVENT_BEFORE_VALIDATE
event exists and defines its handler, beforeValidate(). When specifying an
event handler, you may use one of the following formats:
• a string that refers to the name of a method of the behavior class, like
the example above
• an array of an object or class name, and a method name as a string
(without parentheses), e.g., [$object, ’methodName’];
• an anonymous function
The signature of an event handler should be as follows, where $event refers
to the event parameter. Please refer to the Events section for more details
about events.
function ($event) {
}
use yii\db\ActiveRecord;
use app\components\MyBehavior;
You may associate a name with a behavior by specifying the array key cor-
responding to the behavior configuration. In this case, the behavior is called
a named behavior. In the above example, there are two named behaviors:
myBehavior2 and myBehavior4. If a behavior is not associated with a name, it
is called an anonymous behavior.
To attach a behavior dynamically, call the yii\base\Component::attachBehavior()
method of the component to which the behavior is being attached:
use app\components\MyBehavior;
You may also attach behaviors through configurations like the following:
[
’as myBehavior2’ => MyBehavior::className(),
As you can see, although $component does not define prop1 and foo(), they can
be used as if they are part of the component definition due to the attached
behavior.
If two behaviors define the same property or method and they are both
attached to the same component, the behavior that is attached to the com-
ponent first will take precedence when the property or method is accessed.
A behavior may be associated with a name when it is attached to a
component. If this is the case, you may access the behavior object using the
name:
$behavior = $component->getBehavior(’myBehavior’);
use yii\db\ActiveRecord;
use yii\behaviors\TimestampBehavior;
the behavior attached to the component first. Name conflicts caused by dif-
ferent traits requires manual resolution by renaming the affected properties
or methods.
5.5 Configurations
Configurations are widely used in Yii when creating new objects or initial-
izing existing objects. Configurations usually include the class name of the
object being created, and a list of initial values that should be assigned to
the object’s properties. Configurations may also include a list of handlers
that should be attached to the object’s events and/or a list of behaviors that
should also be attached to the object.
In the following, a configuration is used to create and initialize a database
connection:
$config = [
’class’ => ’yii\db\Connection’,
’dsn’ => ’mysql:host=127.0.0.1;dbname=demo’,
’username’ => ’root’,
’password’ => ’’,
’charset’ => ’utf8’,
];
$db = Yii::createObject($config);
Note that, in this case, the configuration array should not contain a class
element.
where
• The class element specifies a fully qualified class name for the object
being created.
• The propertyName elements specify the initial values for the named prop-
erty. The keys are the property names, and the values are the corres-
ponding initial values. Only public member variables and properties
defined by getters/setters can be configured.
• The on eventName elements specify what handlers should be attached to
the object’s events. Notice that the array keys are formed by prefixing
event names with on . Please refer to the Events section for supported
event handler formats.
• The as behaviorName elements specify what behaviors should be at-
tached to the object. Notice that the array keys are formed by pre-
fixing behavior names with as ; the value, $behaviorConfig, represents
the configuration for creating a behavior, like a normal configuration
described here.
Below is an example showing a configuration with initial property values,
event handlers, and behaviors:
[
’class’ => ’app\components\SearchEngine’,
’apiKey’ => ’xxxxxxxx’,
’on search’ => function ($event) {
Yii::info("Keyword searched: " . $event->keyword);
},
’as indexer’ => [
’class’ => ’app\components\IndexerBehavior’,
// ... property init values ...
],
]
Application Configurations
The configuration for an application is probably one of the most complex
arrays in Yii. This is because the application class has a lot of configur-
220 CHAPTER 5. KEY CONCEPTS
able properties and events. More importantly, its components property can
receive an array of configurations for creating components that are registered
through the application. The following is an abstract from the application
configuration file for the Basic Project Template.
$config = [
’id’ => ’basic’,
’basePath’ => dirname(__DIR__),
’extensions’ => require __DIR__ . ’/../vendor/yiisoft/extensions.php’,
’components’ => [
’cache’ => [
’class’ => ’yii\caching\FileCache’,
],
’mailer’ => [
’class’ => ’yii\swiftmailer\Mailer’,
],
’log’ => [
’class’ => ’yii\log\Dispatcher’,
’traceLevel’ => YII_DEBUG ? 3 : 0,
’targets’ => [
[
’class’ => ’yii\log\FileTarget’,
],
],
],
’db’ => [
’class’ => ’yii\db\Connection’,
’dsn’ => ’mysql:host=localhost;dbname=stay2’,
’username’ => ’root’,
’password’ => ’’,
’charset’ => ’utf8’,
],
],
];
The configuration does not have a class key. This is because it is used as
follows in an entry script, where the class name is already given,
(new yii\web\Application($config))->run();
To know more about the possible values of definitions and singletons config-
uration arrays and real-life examples, please read Advanced Practical Usage
subsection of the Dependency Injection Container article.
Widget Configurations
When using widgets, you often need to use configurations to customize the
widget properties. Both of the yii\base\Widget::widget() and yii\base
\Widget::begin() methods can be used to create a widget. They take a
configuration array, like the following,
use yii\widgets\Menu;
echo Menu::widget([
’activateItems’ => false,
’items’ => [
[’label’ => ’Home’, ’url’ => [’site/index’]],
[’label’ => ’Products’, ’url’ => [’product/index’]],
[’label’ => ’Login’, ’url’ => [’site/login’], ’visible’ => Yii::$app
->user->isGuest],
],
]);
The above code creates a Menu widget and initializes its activateItems property
to be false. The items property is also configured with menu items to be
displayed.
Note that because the class name is already given, the configuration array
should NOT have the class key.
return [
’cache’ => [
’class’ => ’yii\caching\FileCache’,
],
’mailer’ => [
’class’ => ’yii\swiftmailer\Mailer’,
],
’log’ => [
’class’ => ’yii\log\Dispatcher’,
’traceLevel’ => YII_DEBUG ? 3 : 0,
’targets’ => [
[
’class’ => ’yii\log\FileTarget’,
],
],
],
’db’ => [
’class’ => ’yii\db\Connection’,
’dsn’ => ’mysql:host=localhost;dbname=stay2’,
’username’ => ’root’,
’password’ => ’’,
’charset’ => ’utf8’,
],
];
if (YII_ENV_DEV) {
// configuration adjustments for ’dev’ environment
$config[’bootstrap’][] = ’debug’;
$config[’modules’][’debug’] = ’yii\debug\Module’;
}
return $config;
5.6 Aliases
Aliases are used to represent file paths or URLs so that you don’t have to
hard-code absolute paths or URLs in your project. An alias must start with
the @ character to be differentiated from normal file paths and URLs. Alias
defined without leading @ will be prefixed with @ character.
Yii has many pre-defined aliases already available. For example, the alias
@yii represents the installation path of the Yii framework; @web represents
the base URL for the currently running Web application.
// an alias of a URL
Yii::setAlias(’@bar’, ’http://www.example.com’);
Note: The file path or URL being aliased may not necessarily
refer to an existing file or resource.
Given a defined alias, you may derive a new alias (without the need of calling
Yii::setAlias()) by appending a slash / followed with one or more path
segments. The aliases defined via Yii::setAlias() becomes the root alias,
while aliases derived from it are derived aliases. For example, @foo is a root
alias, while @foo/bar/file.php is a derived alias.
You can define an alias using another alias (either root or derived):
Yii::setAlias(’@foobar’, ’@foo/bar’);
Root aliases are usually defined during the bootstrapping stage. For ex-
ample, you may call Yii::setAlias() in the entry script. For convenience,
Application provides a writable property named aliases that you can con-
figure in the application configuration:
return [
// ...
’aliases’ => [
’@foo’ => ’/path/to/foo’,
’@bar’ => ’http://www.example.com’,
],
];
If @foo/bar is not defined as a root alias, the last statement would display
/path/to/foo/bar/file.php.
7
https://www.npmjs.org/
8
http://www.php.net/manual/en/language.oop5.autoload.php
9
https://github.com/php-fig/fig-standards/blob/master/accepted/
PSR-4-autoloader.md
10
http://php.net/manual/en/language.namespaces.php
5.7. CLASS AUTOLOADING 227
For example, if a class name and namespace is foo\bar\MyClass, the alias for
the corresponding class file path would be @foo/bar/MyClass.php. In order for
this alias to be resolvable into a file path, either @foo or @foo/bar must be a
root alias.
When using the Basic Project Template, you may put your classes under
the top-level namespace app so that they can be autoloaded by Yii without
the need of defining a new alias. This is because @app is a predefined alias,
and a class name like app\components\MyClass can be resolved into the class
file AppBasePath/components/MyClass.php, according to the algorithm just de-
scribed.
In the Advanced Project Template11 , each tier has its own root alias. For
example, the front-end tier has a root alias @frontend, while the back-end tier
root alias is @backend. As a result, you may put the front-end classes under
the namespace frontend while the back-end classes are under backend. This
will allow these classes to be autoloaded by the Yii autoloader.
To add a custom namespace to the autoloader you need to define an
alias for the base directory of the namespace using Yii::setAlias(). For
example to load classes in the foo namespace that are located in the path/to
/foo directory you will call Yii::setAlias(’@foo’, ’path/to/foo’).
Aliases can be used to specify class file paths. You should set the class map
in the bootstrapping process so that the map is ready before your classes are
used.
will make the Yii autoloader the first one responding to any class autoloading
request. For example, the following code is extracted from the entry script of
the Basic Project Template. The first line installs the Composer autoloader,
while the second line installs the Yii autoloader:
require __DIR__ . ’/../vendor/autoload.php’;
require __DIR__ . ’/../vendor/yiisoft/yii2/Yii.php’;
You may use the Composer autoloader alone without the Yii autoloader.
However, by doing so, the performance of your class autoloading may be
degraded, and you must follow the rules set by Composer in order for your
classes to be autoloadable.
Info: If you do not want to use the Yii autoloader, you must
create your own version of the Yii.php file and include it in your
entry script.
use yii\di\ServiceLocator;
use yii\caching\FileCache;
Once a component has been registered, you can access it using its ID, in one
of the two following ways:
$cache = $locator->get(’cache’);
// or alternatively
$cache = $locator->cache;
return [
// ...
’components’ => [
// ...
’search’ => SolrServiceBuilder::build(’127.0.0.1’),
],
];
This alternative approach is most preferable when you are releasing a Yii
component which encapsulates some non-Yii 3rd-party library. You use the
static method like shown above to represent the complex logic of building
the 3rd-party object, and the user of your component only needs to call the
static method to configure the component.
children to have access to their parent. This allows modules to use $this->
get(’db’) instead of referencing the root service locator Yii::$app->get(’db’).
Added benefit is the option for a developer to override configuration in a
module.
Any request for a service to be retrieved from a module will be passed
on to its parent in case the module is not able to satisfy it.
Note that configuration from components in a module is never merged
with configuration from a component in a parent module. The Service Loc-
ator pattern allows us to define named services but one cannot assume ser-
vicees with the same name use the same configuration parameters.
Constructor Injection
The DI container supports constructor injection with the help of type hints
for constructor parameters. The type hints tell the container which classes
or interfaces are dependent when it is used to create a new object. The
container will try to get the instances of the dependent classes or interfaces
and then inject them into the new object through the constructor. For
example,
class Foo
{
public function __construct(Bar $bar)
{
}
}
$foo = $container->get(’Foo’);
// which is equivalent to the following:
13
http://martinfowler.com/articles/injection.html
232 CHAPTER 5. KEY CONCEPTS
Method Injection
Usually the dependencies of a class are passed to the constructor and are
available inside of the class during the whole lifecycle. With Method Injection
it is possible to provide a dependency that is only needed by a single method
of the class and passing it to the constructor may not be possible or may
cause too much overhead in the majority of use cases.
A class method can be defined like the doSomething() method in the fol-
lowing example:
class MyClass extends \yii\base\Component
{
public function __construct(/*Some lightweight dependencies here*/,
$config = [])
{
// ...
}
private $_qux;
return $this->_qux;
}
$container->get(’Foo’, [], [
’bar’ => $container->get(’Bar’),
’qux’ => $container->get(’Qux’),
]);
$foo = $container->get(’Foo’);
To hide the complex logic for building a new object, you may use a static
class method as callable. For example,
class FooBuilder
{
public static function build($container, $params, $config)
{
$foo = new Foo(new Bar);
// ... other initializations ...
return $foo;
}
}
234 CHAPTER 5. KEY CONCEPTS
$foo = $container->get(’Foo’);
By doing so, the person who wants to configure the Foo class no longer needs
to be aware of how it is built.
// register an interface
// When a class depends on the interface, the corresponding class
// will be instantiated as the dependent object
$container->set(’yii\mail\MailInterface’, ’yii\swiftmailer\Mailer’);
});
A dependency registered via set() will generate an instance each time the
dependency is needed. You can use yii\di\Container::setSingleton()
to register a dependency that only generates a single instance:
$container->setSingleton(’yii\db\Connection’, [
’dsn’ => ’mysql:host=127.0.0.1;dbname=demo’,
’username’ => ’root’,
’password’ => ’’,
’charset’ => ’utf8’,
]);
Behind the scene, the DI container does much more work than just creating
a new object. The container will first inspect the class constructor to find
out dependent class or interface names and then automatically resolve those
dependencies recursively.
236 CHAPTER 5. KEY CONCEPTS
use yii\base\BaseObject;
use yii\db\Connection;
use yii\di\Container;
interface UserFinderInterface
{
function findUser();
}
$lister = $container->get(’userLister’);
Now if you use the widget in a view with the following code, the maxButtonCount
property will be initialized as 5 instead of the default value 10 as defined in
the class.
echo \yii\widgets\LinkPager::widget();
You can still override the value set via DI container, though:
echo \yii\widgets\LinkPager::widget([’maxButtonCount’ => 20]);
use yii\web\Controller;
use app\components\BookingInterface;
238 CHAPTER 5. KEY CONCEPTS
If you access this controller from browser, you will see an error complaining
the BookingInterface cannot be instantiated. This is because you need to tell
the DI container how to deal with this dependency:
\Yii::$container->set(’app\components\BookingInterface’, ’app\components\
BookingService’);
class DocumentsReader
{
public function __construct(FileStorage $fs) {
// whatever
}
}
$reader = $container->get(’app\storage\DocumentsReader’);
// Will create DocumentReader object with its dependencies as described in
the config
],
’app\storage\DocumentsReader’ => [
[’class’ => ’app\storage\DocumentsReader’],
[Instance::of(’tempFileStorage’)]
],
’app\storage\DocumentsWriter’ => [
[’class’ => ’app\storage\DocumentsWriter’],
[Instance::of(’tempFileStorage’)]
]
]);
$reader = $container->get(’app\storage\DocumentsReader);
// Will behave exactly the same as in the previous example.
$container->setDefinitions([
’app\storage\DocumentsReader’ => [
[’class’ => ’app\storage\DocumentsReader’],
[Instance::of(’tempFileStorage’)]
],
’app\storage\DocumentsWriter’ => [
[’class’ => ’app\storage\DocumentsWriter’],
[Instance::of(’tempFileStorage’)]
]
]);
$reader = $container->get(’app\storage\DocumentsReader’);
5.9. DEPENDENCY INJECTION CONTAINER 241
5.9.7 Summary
Both dependency injection and service locator are popular design patterns
that allow building software in a loosely-coupled and more testable fash-
ion. We highly recommend you to read Martin’s article14 to get a deeper
understanding of dependency injection and service locator.
Yii implements its service locator on top of the dependency injection
(DI) container. When a service locator is trying to create a new object
instance, it will forward the call to the DI container. The latter will resolve
the dependencies automatically as described above.
14
http://martinfowler.com/articles/injection.html
242 CHAPTER 5. KEY CONCEPTS
Chapter 6
243
244 CHAPTER 6. WORKING WITH DATABASES
You can then access the DB connection via the expression Yii::$app->db.
• Oracle: oci:dbname=//localhost:1521/mydatabase
Note that if you are connecting with a database via ODBC, you should
configure the yii\db\Connection::$driverName property so that Yii can
know the actual database type. For example,
’db’ => [
’class’ => ’yii\db\Connection’,
’driverName’ => ’mysql’,
’dsn’ => ’odbc:Driver={MySQL};Server=localhost;Database=test’,
’username’ => ’root’,
’password’ => ’’,
],
Besides the dsn property, you often need to configure username and password.
Please refer to yii\db\Connection for the full list of configurable properties.
The following example shows various ways of fetching data from a database:
// return a set of rows. each row is an associative array of column names
and values.
// an empty array is returned if the query returned no results
$posts = Yii::$app->db->createCommand(’SELECT * FROM post’)
->queryAll();
Binding Parameters
When creating a DB command from a SQL with parameters, you should
almost always use the approach of binding parameters to prevent SQL in-
jection attacks. For example,
$post = Yii::$app->db->createCommand(’SELECT * FROM post WHERE id=:id AND
status=:status’)
->bindValue(’:id’, $_GET[’id’])
->bindValue(’:status’, 1)
->queryOne();
In the SQL statement, you can embed one or multiple parameter placeholders
(e.g. :id in the above example). A parameter placeholder should be a string
starting with a colon. You may then call one of the following parameter
binding methods to bind the parameter values:
• bindValue(): bind a single parameter value
• bindValues(): bind multiple parameter values in one call
• bindParam(): similar to bindValue() but also support binding para-
meter references.
The following example shows alternative ways of binding parameters:
$params = [’:id’ => $_GET[’id’], ’:status’ => 1];
6.1. DATABASE ACCESS OBJECTS 247
$id = 1;
$post1 = $command->queryOne();
$id = 2;
$post2 = $command->queryOne();
// ...
Notice that you bind the placeholder to the $id variable before the execution,
and then change the value of that variable before each subsequent execution
(this is often done with loops). Executing queries in this manner can be
vastly more efficient than running a new query for every different parameter
value.
You may also call batchInsert() to insert multiple rows in one shot, which
is much more efficient than inserting one row at a time:
// table name, column names, column values
Yii::$app->db->createCommand()->batchInsert(’user’, [’name’, ’age’], [
[’Tom’, 30],
[’Jane’, 20],
[’Linda’, 25],
])->execute();
The code above will either insert a new page record or increment its visit
counter atomically.
Note that the aforementioned methods only create the query and you
always have to call execute() to actually run them.
6.1. DATABASE ACCESS OBJECTS 249
Then in your code, whenever you need to refer to a table whose name contains
such a prefix, use the syntax {{%table_name}}. The percentage character will
be automatically replaced with the table prefix that you have specified when
configuring the DB connection. For example,
// executes this SQL for MySQL: SELECT COUNT(‘id‘) FROM ‘tbl_employee‘
$count = Yii::$app->db->createCommand("SELECT COUNT([[id]]) FROM {{%employee
}}")
->queryScalar();
base. If any of the queries fails, the database will be rolled back to the state
as if none of these queries were executed.
The following code shows a typical way of using transactions:
Yii::$app->db->transaction(function($db) {
$db->createCommand($sql1)->execute();
$db->createCommand($sql2)->execute();
// ... executing other SQL statements ...
});
The above code is equivalent to the following, which gives you more control
about the error handling code:
$db = Yii::$app->db;
$transaction = $db->beginTransaction();
try {
$db->createCommand($sql1)->execute();
$db->createCommand($sql2)->execute();
// ... executing other SQL statements ...
$transaction->commit();
} catch(\Exception $e) {
$transaction->rollBack();
throw $e;
} catch(\Throwable $e) {
$transaction->rollBack();
throw $e;
}
your database system. You can override the default isolation level as follows,
$isolationLevel = \yii\db\Transaction::REPEATABLE_READ;
Yii::$app->db->transaction(function ($db) {
....
}, $isolationLevel);
// or alternatively
$transaction = Yii::$app->db->beginTransaction($isolationLevel);
Yii provides four constants for the most common isolation levels:
• yii\db\Transaction::READ_UNCOMMITTED - the weakest level, Dirty
reads, non-repeatable reads and phantoms may occur.
• yii\db\Transaction::READ_COMMITTED - avoid dirty reads.
• yii\db\Transaction::REPEATABLE_READ - avoid dirty reads and non-
repeatable reads.
• yii\db\Transaction::SERIALIZABLE - the strongest level, avoids all
of the above named problems.
Besides using the above constants to specify isolation levels, you may also use
strings with a valid syntax supported by the DBMS that you are using. For
example, in PostgreSQL, you may use "SERIALIZABLE READ ONLY DEFERRABLE".
Note that some DBMS allow setting the isolation level only for the whole
connection. Any subsequent transactions will get the same isolation level
even if you do not specify any. When using this feature you may need to set
the isolation level for all transactions explicitly to avoid conflicting settings.
At the time of this writing, only MSSQL and SQLite are affected by this
limitation.
Note: SQLite only supports two isolation levels, so you can only
use READ UNCOMMITTED and SERIALIZABLE. Usage of other levels will
result in an exception being thrown.
Note: PostgreSQL does not allow setting the isolation level be-
fore the transaction starts so you can not specify the isolation
level directly when starting the transaction. You have to call
yii\db\Transaction::setIsolationLevel() in this case after
the transaction has started.
Nesting Transactions
If your DBMS supports Savepoint, you may nest multiple transactions like
the following:
Yii::$app->db->transaction(function ($db) {
// outer transaction
$db->transaction(function ($db) {
252 CHAPTER 6. WORKING WITH DATABASES
// inner transaction
});
});
Or alternatively,
$db = Yii::$app->db;
$outerTransaction = $db->beginTransaction();
try {
$db->createCommand($sql1)->execute();
$innerTransaction = $db->beginTransaction();
try {
$db->createCommand($sql2)->execute();
$innerTransaction->commit();
} catch (\Exception $e) {
$innerTransaction->rollBack();
throw $e;
} catch (\Throwable $e) {
$innerTransaction->rollBack();
throw $e;
}
$outerTransaction->commit();
} catch (\Exception $e) {
$outerTransaction->rollBack();
throw $e;
} catch (\Throwable $e) {
$outerTransaction->rollBack();
throw $e;
}
15
http://en.wikipedia.org/wiki/Replication_(computing)#Database_
replication
6.1. DATABASE ACCESS OBJECTS 253
The above configuration specifies a setup with a single master and multiple
slaves. One of the slaves will be connected and used to perform read queries,
while the master will be used to perform write queries. Such read-write
splitting is accomplished automatically with this configuration. For example,
// create a Connection instance using the above configuration
Yii::$app->db = Yii::createObject($config);
You can also configure multiple masters with multiple slaves. For example,
[
’class’ => ’yii\db\Connection’,
The above configuration specifies two masters and four slaves. The Connection
component also supports load balancing and failover between masters just
as it does between slaves. A difference is that when none of the masters are
available an exception will be thrown.
try {
// both queries are performed against the master
$rows = $db->createCommand(’SELECT * FROM user LIMIT 10’)->queryAll();
$db->createCommand("UPDATE user SET username=’demo’ WHERE id=1")->
execute();
$transaction->commit();
} catch(\Exception $e) {
$transaction->rollBack();
throw $e;
} catch(\Throwable $e) {
$transaction->rollBack();
throw $e;
}
If you want to start a transaction with the slave connection, you should
explicitly do so, like the following:
$transaction = Yii::$app->db->slave->beginTransaction();
Sometimes, you may want to force using the master connection to perform
a read query. This can be achieved with the useMaster() method:
$rows = Yii::$app->db->useMaster(function ($db) {
return $db->createCommand(’SELECT * FROM user LIMIT 10’)->queryAll();
});
The above array describes the name and types of the columns to be created.
For the column types, Yii provides a set of abstract data types, that allow
you to define a database agnostic schema. These are converted to DBMS
specific type definitions dependent on the database, the table is created in.
Please refer to the API documentation of the createTable()-method for
more information.
Besides changing the database schema, you can also retrieve the definition
information about a table through the getTableSchema() method of a DB
connection. For example,
$table = Yii::$app->db->getTableSchema(’post’);
The above code generates and executes the following SQL query, where the
:last_name parameter is bound with the string ’Smith’.
SELECT ‘id‘, ‘email‘
FROM ‘user‘
WHERE ‘last_name‘ = :last_name
LIMIT 10
select()
The select() method specifies the SELECT fragment of a SQL statement.
You can specify columns to be selected in either an array or a string, like the
following. The column names being selected will be automatically quoted
when the SQL statement is being generated from a query object.
$query->select([’id’, ’email’]);
// equivalent to:
$query->select(’id, email’);
The column names being selected may include table prefixes and/or column
aliases, like you do when writing raw SQL queries. For example,
$query->select([’user.id AS user_id’, ’email’]);
// equivalent to:
If you are using the array format to specify columns, you can also use the
array keys to specify the column aliases. For example, the above code can
be rewritten as follows,
$query->select([’user_id’ => ’user.id’, ’email’]);
If you do not call the select() method when building a query, * will be
selected, which means selecting all columns.
Besides column names, you can also select DB expressions. You must
use the array format when selecting a DB expression that contains commas
to avoid incorrect automatic name quoting. For example,
$query->select(["CONCAT(first_name, ’ ’, last_name) AS full_name", ’email’])
;
As with all places where raw SQL is involved, you may use the DBMS ag-
nostic quoting syntax for table and column names when writing DB expres-
sions in select.
Starting from version 2.0.1, you may also select sub-queries. You should
specify each sub-query in terms of a yii\db\Query object. For example,
$subQuery = (new Query())->select(’COUNT(*)’)->from(’user’);
To select distinct rows, you may call distinct(), like the following:
// SELECT DISTINCT ‘user_id‘ ...
$query->select(’user_id’)->distinct();
from()
The from() method specifies the FROM fragment of a SQL statement. For
example,
// SELECT * FROM ‘user‘
$query->from(’user’);
You can specify the table(s) being selected from in either a string or an array.
The table names may contain schema prefixes and/or table aliases, like you
do when writing raw SQL statements. For example,
$query->from([’public.user u’, ’public.post p’]);
// equivalent to:
If you are using the array format, you can also use the array keys to specify
the table aliases, like the following:
$query->from([’u’ => ’public.user’, ’p’ => ’public.post’]);
Besides table names, you can also select from sub-queries by specifying them
in terms of yii\db\Query objects. For example,
$subQuery = (new Query())->select(’id’)->from(’user’)->where(’status=1’);
where()
The where() method specifies the WHERE fragment of a SQL query. You can
use one of the four formats to specify a WHERE condition:
• string format, e.g., ’status=1’
• hash format, e.g. [’status’ => 1, ’type’ => 2]
• operator format, e.g. [’like’, ’name’, ’test’]
• object format, e.g. new LikeCondition(’name’, ’LIKE’, ’test’)
String Format String format is best used to specify very simple condi-
tions or if you need to use built-in functions of the DBMS. It works as if you
are writing a raw SQL. For example,
$query->where(’status=1’);
Do NOT embed variables directly in the condition like the following, espe-
cially if the variable values come from end user inputs, because this will make
your application subject to SQL injection attacks.
// Dangerous! Do NOT do this unless you are very certain $status must be an
integer.
$query->where("status=$status");
As with all places where raw SQL is involved, you may use the DBMS ag-
nostic quoting syntax for table and column names when writing conditions
in string format.
As you can see, the query builder is intelligent enough to properly handle
values that are nulls or arrays.
You can also use sub-queries with hash format like the following:
$userQuery = (new Query())->select(’id’)->from(’user’);
Using the Hash Format, Yii internally applies parameter binding for values,
so in contrast to the string format, here you do not have to add parameters
manually. However, note that Yii never escapes column names, so if you pass
a variable obtained from user side as a column name without any additional
checks, the application will become vulnerable to SQL injection attack. In
order to keep the application secure, either do not use variables as column
names or filter variable against white list. In case you need to get column
name from user, read the Filtering Data guide article. For example the
following code is vulnerable:
// Vulnerable code:
$column = $request->get(’column’);
$value = $request->get(’value);
$query->where([$column => $value]);
// $value is safe, but $column name won’t be encoded!
where the operands can each be specified in string format, hash format or
operator format recursively, while the operator can be one of the following:
• and: the operands should be concatenated together using AND. For
example, [’and’, ’id=1’, ’id=2’] will generate id=1 AND id=2. If an op-
erand is an array, it will be converted into a string using the rules
6.2. QUERY BUILDER 261
Object Format Object Form is available since 2.0.14 and is both most
powerful and most complex way to define conditions. You need to follow it
either if you want to build your own abstraction over query builder or if you
want to implement your own complex conditions.
Instances of condition classes are immutable. Their only purpose is to
store condition data and provide getters for condition builders. Condition
builder is a class that holds the logic that transforms data stored in condition
into the SQL expression.
16
http://www.postgresql.org/docs/8.3/static/functions-matching.html#
FUNCTIONS-LIKE
6.2. QUERY BUILDER 263
if (!empty($search)) {
$query->andWhere([’like’, ’title’, $search]);
}
The only difference between filterWhere() and where() is that the former
will ignore empty values provided in the condition in hash format. So if
$email is empty while $username is not, the above code will result in the SQL
condition WHERE username=:username.
Info: A value is considered empty if it is null, an empty array,
an empty string or a string consisting of whitespaces only.
Like andWhere() and orWhere(), you can use andFilterWhere() and orFilterWhere()
to append additional filter conditions to the existing one.
Additionally, there is yii\db\Query::andFilterCompare() that can in-
telligently determine operator based on what’s in the value:
$query->andFilterCompare(’name’, ’John Doe’);
$query->andFilterCompare(’rating’, ’>9’);
$query->andFilterCompare(’value’, ’<=100’);
Since Yii 2.0.11 there are similar methods for HAVING condition:
• filterHaving()
• andFilterHaving()
• orFilterHaving()
orderBy()
The orderBy() method specifies the ORDER BY fragment of a SQL query. For
example,
// ... ORDER BY ‘id‘ ASC, ‘name‘ DESC
$query->orderBy([
’id’ => SORT_ASC,
’name’ => SORT_DESC,
]);
In the above code, the array keys are column names while the array values are
the corresponding order by directions. The PHP constant SORT_ASC specifies
ascending sort and SORT_DESC descending sort.
If ORDER BY only involves simple column names, you can specify it using a
string, just like you do when writing raw SQL statements. For example,
$query->orderBy(’id ASC, name DESC’);
Note: You should use the array format if ORDER BY involves some
DB expression.
You can call addOrderBy() to add additional columns to the ORDER BY frag-
ment. For example,
$query->orderBy(’id ASC’)
->addOrderBy(’name DESC’);
6.2. QUERY BUILDER 265
groupBy()
The groupBy() method specifies the GROUP BY fragment of a SQL query. For
example,
// ... GROUP BY ‘id‘, ‘status‘
$query->groupBy([’id’, ’status’]);
If GROUP BY only involves simple column names, you can specify it using a
string, just like you do when writing raw SQL statements. For example,
$query->groupBy(’id, status’);
Note: You should use the array format if GROUP BY involves some
DB expression.
You can call addGroupBy() to add additional columns to the GROUP BY frag-
ment. For example,
$query->groupBy([’id’, ’status’])
->addGroupBy(’age’);
having()
The having() method specifies the HAVING fragment of a SQL query. It takes
a condition which can be specified in the same way as that for where(). For
example,
// ... HAVING ‘status‘ = 1
$query->having([’status’ => 1]);
Please refer to the documentation for where() for more details about how to
specify a condition.
You can call andHaving() or orHaving() to append additional conditions
to the HAVING fragment. For example,
// ... HAVING (‘status‘ = 1) AND (‘age‘ > 30)
$query->having([’status’ => 1])
->andHaving([’>’, ’age’, 30]);
join()
The join() method specifies the JOIN fragment of a SQL query. For example,
// ... LEFT JOIN ‘post‘ ON ‘post‘.‘user_id‘ = ‘user‘.‘id‘
$query->join(’LEFT JOIN’, ’post’, ’post.user_id = user.id’);
To join with multiple tables, call the above join methods multiple times,
once for each table.
Besides joining with tables, you can also join with sub-queries. To do so,
specify the sub-queries to be joined as yii\db\Query objects. For example,
$subQuery = (new \yii\db\Query())->from(’post’);
$query->leftJoin([’u’ => $subQuery], ’u.id = author_id’);
In this case, you should put the sub-query in an array and use the array key
to specify the alias.
union()
The union() method specifies the UNION fragment of a SQL query. For ex-
ample,
$query1 = (new \yii\db\Query())
->select("id, category_id AS type, name")
->from(’post’)
->limit(10);
->limit(10);
$query1->union($query2);
You can call union() multiple times to append more UNION fragments.
Note: The one() method only returns the first row of the query
result. It does NOT add LIMIT 1 to the generated SQL statement.
This is fine and preferred if you know the query will return only
one or a few rows of data (e.g. if you are querying with some
primary keys). However, if the query may potentially result in
many rows of data, you should call limit(1) explicitly to improve
the performance, e.g., (new \yii\db\Query())->from(’user’)->limit
(1)->one().
All these query methods take an optional $db parameter representing the DB
connection that should be used to perform a DB query. If you omit this
parameter, the db application component will be used as the DB connection.
Below is another example using the count() query method:
268 CHAPTER 6. WORKING WITH DATABASES
When you call a query method of yii\db\Query, it actually does the follow-
ing work internally:
• Call yii\db\QueryBuilder to generate a SQL statement based on the
current construct of yii\db\Query;
• Create a yii\db\Command object with the generated SQL statement;
• Call a query method (e.g. queryAll()) of yii\db\Command to execute
the SQL statement and retrieve the data.
Sometimes, you may want to examine or use the SQL statement built from
a yii\db\Query object. You can achieve this goal with the following code:
$command = (new \yii\db\Query())
->select([’id’, ’email’])
->from(’user’)
->where([’last_name’ => ’Smith’])
->limit(10)
->createCommand();
The anonymous function takes a parameter $row which contains the current
row data and should return a scalar value which will be used as the index
value for the current row.
Batch Query
When working with large amounts of data, methods such as yii\db\Query
::all() are not suitable because they require loading the whole query result
into the client’s memory. To solve this issue Yii provides batch query support.
The server holds the query result, and the client uses a cursor to iterate over
the result set one batch at a time.
remaining iterations. By default, the batch size is 100, meaning 100 rows
of data are being fetched in each batch. You can change the batch size by
passing the first parameter to the batch() or each() method.
Compared to the yii\db\Query::all(), the batch query only loads 100
rows of data at a time into the memory.
If you specify the query result to be indexed by some column via yii\db
\Query::indexBy(), the batch query will still keep the proper index.
For example:
$query = (new \yii\db\Query())
->from(’user’)
->indexBy(’username’);
// Do batch query
17
http://php.net/manual/en/mysqlinfo.concepts.buffering.php
6.2. QUERY BUILDER 271
Yii::$app->db->pdo->setAttribute(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
If you want to ensure that the $unbufferedDb has exactly the same PDO attrib-
utes like the original buffered $db but the PDO::MYSQL_ATTR_USE_BUFFERED_QUERY
is false, consider a deep copy of $db18 , set it to false manually.
Then, queries are created normally. The new connection is used to run
batch queries and retrieve results either in batches or one by one:
// getting data in batches of 1000
foreach ($query->batch(1000, $unbufferedDb) as $users) {
// ...
}
When the connection is no longer necessary and the result set has been
retrieved, it can be closed:
$unbufferedDb->close();
When such condition applied once, it is fine. In case it is used multiple times
in a single query it can be optimized a lot. Let’s create a custom condition
object to demonstrate it.
Yii has a ConditionInterface, that must be used to mark classes, that
represent a condition. It requires fromArrayDefinition() method implementa-
tion, in order to make possible to create condition from array format. In case
you don’t need it, you can implement this method with exception throwing.
Since we create our custom condition class, we can build API that suits
our task the most.
namespace app\db\conditions;
/**
* @param string[] $columns Array of columns that must be greater, than
$value
* @param mixed $value the value to compare each $column against.
*/
public function __construct(array $columns, $value)
{
$this->columns = $columns;
$this->value = $value;
}
But QueryBuilder still does not know, to make an SQL condition out of this
object. Now we need to create a builder for this condition. It must implement
yii\db\ExpressionBuilderInterface that requires us to implement a build
() method.
namespace app\db\conditions;
/**
* @param AllGreaterCondition $condition the condition to be built
* @param array $params the binding parameters.
*/
public function build(ConditionInterface $condition, &$params)
{
$value = $condition->getValue();
$conditions = [];
foreach ($condition->getColumns() as $column) {
$conditions[] = new SimpleCondition($column, ’>’, $value);
}
Then simple let QueryBuilder know about our new condition – add a map-
ping for it to the expressionBuilders array. It could be done right from the
application configuration:
’db’ => [
’class’ => ’yii\db\mysql\Connection’,
// ...
’queryBuilder’ => [
’expressionBuilders’ => [
’app\db\conditions\AllGreaterCondition’ => ’app\db\conditions\
AllGreaterConditionBuilder’,
],
],
],
After that, we can create our custom condition using shorter operator format:
$query->andWhere([’ALL>’, [’posts’, ’comments’, ’reactions’, ’subscriptions’
], $minValue]);
You might notice, that there was two concepts used: Expressions and Con-
ditions. There is a yii\db\ExpressionInterface that should be used to
mark objects, that require an Expression Builder class, that implements
yii\db\ExpressionBuilderInterface to be built. Also there is a yii\db
\condition\ConditionInterface, that extends ExpressionInterface and
should be used to objects, that can be created from array definition as it was
shown above, but require builder as well.
To summarise:
• Expression – is a Data Transfer Object (DTO) for a dataset, that can
be somehow compiled to some SQL statement (an operator, string,
array, JSON, etc).
• Condition – is an Expression superset, that aggregates multiple Expres-
sions (or scalar values) that can be compiled to a single SQL condition.
You can create your own classes that implement ExpressionInterface to
hide the complexity of transforming data to SQL statements. You will learn
more about other examples of Expressions in the next article;
6.3. ACTIVE RECORD 275
The above code is equivalent to using the following raw SQL statement
for MySQL, which is less intuitive, more error prone, and may even have
compatibility problems if you are using a different kind of database:
$db->createCommand(’INSERT INTO ‘customer‘ (‘name‘) VALUES (:name)’, [
’:name’ => ’Qiang’,
])->execute();
Yii provides the Active Record support for the following relational databases:
• MySQL 4.1 or later: via yii\db\ActiveRecord
• PostgreSQL 7.3 or later: via yii\db\ActiveRecord
• SQLite 2 and 3: via yii\db\ActiveRecord
• Microsoft SQL Server 2008 or later: via yii\db\ActiveRecord
• Oracle: via yii\db\ActiveRecord
• CUBRID 9.3 or later: via yii\db\ActiveRecord (Note that due to a
bug21 in the cubrid PDO extension, quoting of values will not work,
so you need CUBRID 9.3 as the client as well as the server)
• Sphinx: via yii\sphinx\ActiveRecord, requires the yii2-sphinx ex-
tension
• ElasticSearch: via yii\elasticsearch\ActiveRecord, requires the
yii2-elasticsearch extension
Additionally, Yii also supports using Active Record with the following NoSQL
databases:
• Redis 2.6.12 or later: via yii\redis\ActiveRecord, requires the yii2
-redis extension
• MongoDB 1.3.0 or later: via yii\mongodb\ActiveRecord, requires the
yii2-mongodb extension
20
http://en.wikipedia.org/wiki/Active_record_pattern
21
http://jira.cubrid.org/browse/APIS-658
276 CHAPTER 6. WORKING WITH DATABASES
In this tutorial, we will mainly describe the usage of Active Record for rela-
tional databases. However, most content described here are also applicable
to Active Record for NoSQL databases.
By default each Active Record class is associated with its database table. The
tableName() method returns the table name by converting the class name
via yii\helpers\Inflector::camel2id(). You may override this method
if the table is not named after this convention.
Also a default tablePrefix can be applied. For example if tablePrefix
is tbl_, Customer becomes tbl_customer and OrderItem becomes tbl_order_item.
If a table name is given as {{%TableName}}, then the percentage character
% will be replaced with the table prefix. For example, {{%post}} becomes
{{tbl_post}}. The brackets around the table name are used for quoting in an
SQL query.
In the following example, we declare an Active Record class named
Customer for the customer database table.
namespace app\models;
use yii\db\ActiveRecord;
/**
* @return string the name of the table associated with this
ActiveRecord class.
*/
public static function tableName()
{
return ’{{customer}}’;
}
}
Active Record instances are considered as models. For this reason, we usu-
ally put Active Record classes under the app\models namespace (or other
namespaces for keeping model classes).
6.3. ACTIVE RECORD 277
If you want to use a different database connection other than the db com-
ponent, you should override the getDb() method:
class Customer extends ActiveRecord
{
// ...
As you can see, this is very similar to the procedure with query builder.
The only difference is that instead of using the new operator to create a
query object, you call yii\db\ActiveRecord::find() to return a new query
object which is of class yii\db\ActiveQuery.
Below are some examples showing how to use Active Query to query
data:
// return a single customer whose ID is 123
// SELECT * FROM ‘customer‘ WHERE ‘id‘ = 123
$customer = Customer::find()
->where([’id’ => 123])
->one();
Besides using query building methods, you can also write raw SQLs to query
data and populate the results into Active Record objects. You can do so by
calling the yii\db\ActiveRecord::findBySql() method:
// returns all inactive customers
$sql = ’SELECT * FROM customer WHERE status=:status’;
$customers = Customer::findBySql($sql, [’:status’ => Customer::
STATUS_INACTIVE])->all();
Do not call extra query building methods after calling findBySql() as they
will be ignored.
Note: The Active Record attributes are named after the associ-
ated table columns in a case-sensitive manner. Yii automatically
defines an attribute in Active Record for every column of the as-
sociated table. You should NOT redeclare any of the attributes.
Because Active Record attributes are named after table columns, you may
find you are writing PHP code like $customer->first_name, which uses under-
scores to separate words in attribute names if your table columns are named
in this way. If you are concerned about code style consistency, you should
rename your table columns accordingly (to use camelCase, for example).
Data Transformation
It often happens that the data being entered and/or displayed are in a format
which is different from the one used in storing the data in a database. For
example, in the database you are storing customers’ birthdays as UNIX
timestamps (which is not a good design, though), while in most cases you
would like to manipulate birthdays as strings in the format of ’YYYY/MM/DD’
. To achieve this goal, you can define data transformation methods in the
Customer Active Record class like the following:
class Customer extends ActiveRecord
{
// ...
6.3. ACTIVE RECORD 281
For example,
// insert a new row of data
$customer = new Customer();
$customer->name = ’James’;
$customer->email = ’[email protected]’;
$customer->save();
The save() method can either insert or update a row of data, depending
on the state of the Active Record instance. If the instance is newly created
via the new operator, calling save() will cause insertion of a new row; If the
instance is the result of a query method, calling save() will update the row
associated with the instance.
You can differentiate the two states of an Active Record instance by
checking its isNewRecord property value. This property is also used by
save() internally as follows:
public function save($runValidation = true, $attributeNames = null)
{
if ($this->getIsNewRecord()) {
6.3. ACTIVE RECORD 283
Data Validation
Because yii\db\ActiveRecord extends from yii\base\Model, it shares the
same data validation feature. You can declare validation rules by overriding
the rules() method and perform data validation by calling the validate()
method.
When you call save(), by default it will call validate() automatically.
Only when the validation passes, will it actually save the data; otherwise it
will simply return false, and you can check the errors property to retrieve
the validation error messages.
Tip: If you are certain that your data do not need validation
(e.g., the data comes from trustable sources), you can call save(
false) to skip the validation.
Massive Assignment
Like normal models, Active Record instances also enjoy the massive assign-
ment feature. Using this feature, you can assign values to multiple attributes
of an Active Record instance in a single PHP statement, like shown below.
Do remember that only safe attributes can be massively assigned, though.
$values = [
’name’ => ’James’,
’email’ => ’[email protected]’,
];
$customer->attributes = $values;
$customer->save();
Updating Counters
It is a common task to increment or decrement a column in a database table.
We call these columns “counter columns”. You can use updateCounters()
to update one or multiple counter columns. For example,
284 CHAPTER 6. WORKING WITH DATABASES
$post = Post::findOne(100);
Dirty Attributes
When you call save() to save an Active Record instance, only dirty at-
tributes are being saved. An attribute is considered dirty if its value has
been modified since it was loaded from DB or saved to DB most recently.
Note that data validation will be performed regardless if the Active Record
instance has dirty attributes or not.
Active Record automatically maintains the list of dirty attributes. It
does so by maintaining an older version of the attribute values and com-
paring them with the latest one. You can call yii\db\ActiveRecord::
getDirtyAttributes() to get the attributes that are currently dirty. You
can also call yii\db\ActiveRecord::markAttributeDirty() to explicitly
mark an attribute as dirty.
If you are interested in the attribute values prior to their most recent
modification, you may call getOldAttributes() or getOldAttribute().
Note: The comparison of old and new values will be done using
the === operator so a value will be considered dirty even if it
has the same value but a different type. This is often the case
when the model receives user input from HTML forms where
every value is represented as a string. To ensure the correct
type for e.g. integer values you may apply a validation filter: [’
attributeName’, ’filter’, ’filter’ => ’intval’]. This works with
all the typecasting functions of PHP like intval()22 , floatval()23 ,
boolval24 , etc...
Attributes Typecasting
Since 2.0.14, Yii ActiveRecord supports complex data types, such as JSON
or multidimensional arrays.
To learn more about expressions building system read the Query Builder
– Adding custom Conditions and Expressions article.
1. Class constructor.
2. init(): triggers an EVENT_INIT event.
1. Class constructor.
2. init(): triggers an EVENT_INIT event.
3. afterFind(): triggers an EVENT_AFTER_FIND event.
// or alternatively
$transaction = Customer::getDb()->beginTransaction();
try {
6.3. ACTIVE RECORD 289
$customer->id = 200;
$customer->save();
// ...other DB operations...
$transaction->commit();
} catch(\Exception $e) {
$transaction->rollBack();
throw $e;
} catch(\Throwable $e) {
$transaction->rollBack();
throw $e;
}
The second way is to list the DB operations that require transactional sup-
port in the yii\db\ActiveRecord::transactions() method. For example,
class Customer extends ActiveRecord
{
public function transactions()
{
return [
’admin’ => self::OP_INSERT,
’api’ => self::OP_INSERT | self::OP_UPDATE | self::OP_DELETE,
// the above is equivalent to the following:
// ’api’ => self::OP_ALL,
];
}
}
25
http://php.net/manual/en/class.throwable.php
290 CHAPTER 6. WORKING WITH DATABASES
3. In the Web form that takes user inputs, add a hidden field to store
the current version number of the row being updated. Be sure your
version attribute has input validation rules and validates successfully.
4. In the controller action that updates the row using Active Record, try
and catch the yii\db\StaleObjectException exception. Implement
necessary business logic (e.g. merging the changes, prompting staled
data) to resolve the conflict.
For example, assume the version column is named as version. You can im-
plement optimistic locking with the code like the following.
// ------ view code -------
use yii\helpers\Html;
use yii\db\StaleObjectException;
6.3. ACTIVE RECORD 291
try {
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect([’view’, ’id’ => $model->id]);
} else {
return $this->render(’update’, [
’model’ => $model,
]);
}
} catch (StaleObjectException $e) {
// logic to resolve the conflict
}
}
Declaring Relations
To work with relational data using Active Record, you first need to declare
relations in Active Record classes. The task is as simple as declaring a
relation method for every interested relation, like the following,
class Customer extends ActiveRecord
{
// ...
In the above code, we have declared an orders relation for the Customer class,
and a customer relation for the Order class.
Each relation method must be named as getXyz. We call xyz (the first
letter is in lower case) the relation name. Note that relation names are case
sensitive.
While declaring a relation, you should specify the following information:
• the multiplicity of the relation: specified by calling either hasMany()
or hasOne(). In the above example you may easily read in the relation
declarations that a customer has many orders while an order only has
one customer.
• the name of the related Active Record class: specified as the first para-
meter to either hasMany() or hasOne(). A recommended practice is to
call Xyz::className() to get the class name string so that you can receive
IDE auto-completion support as well as error detection at compiling
stage.
• the link between the two types of data: specifies the column(s) through
which the two types of data are related. The array values are the
columns of the primary data (represented by the Active Record class
that you are declaring relations), while the array keys are the columns
of the related data.
An easy rule to remember this is, as you see in the example above, you
write the column that belongs to the related Active Record directly
next to it. You see there that customer_id is a property of Order and id
is a property of Customer.
Info: When you declare a relation named xyz via a getter method
getXyz(),you will be able to access xyz like an object property.
Note that the name is case sensitive.
6.3. ACTIVE RECORD 293
// SELECT * FROM ‘order‘ WHERE ‘customer_id‘ = 123 AND ‘subtotal‘ > 200
ORDER BY ‘id‘
$orders = $customer->getOrders()
->where([’>’, ’subtotal’, 200])
->orderBy(’id’)
->all();
Unlike accessing a relation property, each time you perform a dynamic rela-
tional query via a relation method, a SQL statement will be executed, even
if the same dynamic relational query was performed before.
Sometimes you may even want to parametrize a relation declaration so
that you can more easily perform dynamic relational query. For example,
you may declare a bigOrders relation as follows,
class Customer extends ActiveRecord
{
public function getBigOrders($threshold = 100)
294 CHAPTER 6. WORKING WITH DATABASES
{
return $this->hasMany(Order::className(), [’customer_id’ => ’id’])
->where(’subtotal > :threshold’, [’:threshold’ => $threshold])
->orderBy(’id’);
}
}
// SELECT * FROM ‘order‘ WHERE ‘customer_id‘ = 123 AND ‘subtotal‘ > 100
ORDER BY ‘id‘
$orders = $customer->bigOrders;
or alternatively,
class Order extends ActiveRecord
{
public function getOrderItems()
{
return $this->hasMany(OrderItem::className(), [’order_id’ => ’id’]);
}
}
}
The usage of relations declared with a junction table is the same as that of
normal relations. For example,
Its further possible to define relations via multiple tables by chaining relation
definitions using via(). Considering the examples above, we have classes
Customer, Order, and Item. We can add a relation to the Customer class that
lists all items from all the orders they placed, and name it getPurchasedItems
(), the chaining of relations is show in the following code example:
// no SQL executed
$orders2 = $customer->orders;
Lazy loading is very convenient to use. However, it may suffer from a per-
formance issue when you need to access the same relation property of mul-
tiple Active Record instances. Consider the following code example. How
many SQL statements will be executed?
// SELECT * FROM ‘customer‘ LIMIT 100
$customers = Customer::find()->limit(100)->all();
As you can see from the code comment above, there are 101 SQL statements
being executed! This is because each time you access the orders relation
property of a different Customer object in the for-loop, a SQL statement will
be executed.
To solve this performance problem, you can use the so-called eager loading
approach as shown below,
// SELECT * FROM ‘customer‘ LIMIT 100;
// SELECT * FROM ‘orders‘ WHERE ‘customer_id‘ IN (...)
$customers = Customer::find()
->with(’orders’)
->limit(100)
->all();
You can eagerly load one or multiple relations. You can even eagerly
load nested relations. A nested relation is a relation that is declared within
a related Active Record class. For example, Customer is related with Order
through the orders relation, and Order is related with Item through the items
relation. When querying for Customer, you can eagerly load items using the
nested relation notation orders.items.
The following code shows different usage of with(). We assume the
Customer class has two relations orders and country, while the Order class has
one relation items.
// eager loading both "orders" and "country"
$customers = Customer::find()->with(’orders’, ’country’)->all();
// equivalent to the array syntax below
$customers = Customer::find()->with([’orders’, ’country’])->all();
// no SQL executed
$orders= $customers[0]->orders;
// no SQL executed
$country = $customers[0]->country;
You can eagerly load deeply nested relations, such as a.b.c.d. All parent
relations will be eagerly loaded. That is, when you call with() using a.b.c.
d, you will eagerly load a, a.b, a.b.c and a.b.c.d.
When eagerly loading a relation, you can customize the corresponding rela-
tional query using an anonymous function. For example,
// find customers and bring back together their country and active orders
// SELECT * FROM ‘customer‘
// SELECT * FROM ‘country‘ WHERE ‘id‘ IN (...)
// SELECT * FROM ‘order‘ WHERE ‘customer_id‘ IN (...) AND ‘status‘ = 1
$customers = Customer::find()->with([
’country’,
’orders’ => function ($query) {
$query->andWhere([’status’ => Order::STATUS_ACTIVE]);
},
])->all();
When customizing the relational query for a relation, you should specify
the relation name as an array key and use an anonymous function as the
corresponding array value. The anonymous function will receive a $query
298 CHAPTER 6. WORKING WITH DATABASES
The relational queries that we have described so far only reference the
primary table columns when querying for the primary data. In reality we
often need to reference columns in the related tables. For example, we may
want to bring back the customers who have at least one active order. To
solve this problem, we can build a join query like the following:
// SELECT ‘customer‘.* FROM ‘customer‘
// LEFT JOIN ‘order‘ ON ‘order‘.‘customer_id‘ = ‘customer‘.‘id‘
// WHERE ‘order‘.‘status‘ = 1
//
// SELECT * FROM ‘order‘ WHERE ‘customer_id‘ IN (...)
$customers = Customer::find()
->select(’customer.*’)
->leftJoin(’order’, ’‘order‘.‘customer_id‘ = ‘customer‘.‘id‘’)
->where([’order.status’ => Order::STATUS_ACTIVE])
->with(’orders’)
->all();
$customers = Customer::find()
->joinWith(’orders’)
->where([’order.status’ => Order::STATUS_ACTIVE])
->all();
Both approaches execute the same set of SQL statements. The latter ap-
proach is much cleaner and drier, though.
By default, joinWith() will use LEFT JOIN to join the primary table with
the related table. You can specify a different join type (e.g. RIGHT JOIN) via
its third parameter $joinType. If the join type you want is INNER JOIN, you
can simply call innerJoinWith(), instead.
Calling joinWith() will eagerly load the related data by default. If you
do not want to bring in the related data, you can specify its second parameter
$eagerLoading as false.
Like with(), you can join with one or multiple relations; you may customize
the relation queries on-the-fly; you may join with nested relations; and you
may mix the use of with() and joinWith(). For example,
$customers = Customer::find()->joinWith([
’orders’ => function ($query) {
$query->andWhere([’>’, ’subtotal’, 100]);
},
])->with(’country’)
->all();
Sometimes when joining two tables, you may need to specify some extra
conditions in the ON part of the JOIN query. This can be done by calling the
yii\db\ActiveQuery::onCondition() method like the following:
// SELECT ‘customer‘.* FROM ‘customer‘
// LEFT JOIN ‘order‘ ON ‘order‘.‘customer_id‘ = ‘customer‘.‘id‘ AND ‘order
‘.‘status‘ = 1
//
// SELECT * FROM ‘order‘ WHERE ‘customer_id‘ IN (...)
$customers = Customer::find()->joinWith([
’orders’ => function ($query) {
$query->onCondition([’order.status’ => Order::STATUS_ACTIVE]);
},
])->all();
This above query brings back all customers, and for each customer it brings
back all active orders. Note that this differs from our earlier example which
only brings back customers who have at least one active order.
the query involves a JOIN query. If the query does not involve
JOIN, the on-condition will be automatically appended to the
WHERE part of the query. Thus it may only contain conditions
including columns of the related table.
This however looks very complicated and involves either hardcoding the re-
lated objects table name or calling Order::tableName(). Since version 2.0.7,
Yii provides a shortcut for this. You may now define and use the alias for
the relation table like the following:
// join the orders relation and sort the result by orders.id
$query->joinWith([’orders o’])->orderBy(’o.id’);
The above syntax works for simple relations. If you need an alias for an
intermediate table when joining over nested relations, e.g. $query->joinWith
([’orders.product’]), you need to nest the joinWith calls like in the following
example:
$query->joinWith([’orders o’ => function($q) {
$q->joinWith(’product p’);
}])
->where(’o.amount > 100’);
Inverse Relations
Relation declarations are often reciprocal between two Active Record classes.
For example, Customer is related to Order via the orders relation, and Order is
related back to Customer via the customer relation.
class Customer extends ActiveRecord
{
public function getOrders()
{
return $this->hasMany(Order::className(), [’customer_id’ => ’id’]);
}
}
{
return $this->hasOne(Customer::className(), [’id’ => ’customer_id’])
;
}
}
We would think $customer and $customer2 are the same, but they are not!
Actually they do contain the same customer data, but they are different ob-
jects. When accessing $order->customer, an extra SQL statement is executed
to populate a new object $customer2.
To avoid the redundant execution of the last SQL statement in the above
example, we should tell Yii that customer is an inverse relation of orders by
calling the inverseOf() method like shown below:
class Customer extends ActiveRecord
{
public function getOrders()
{
return $this->hasMany(Order::className(), [’customer_id’ => ’id’])->
inverseOf(’customer’);
}
}
// displays "same"
echo $customer2 === $customer ? ’same’ : ’not the same’;
Active Record provides the link() method that allows you to accomplish
this task more nicely:
$customer = Customer::findOne(123);
$order = new Order();
$order->subtotal = 100;
// ...
$order->link(’customer’, $customer);
The link() method requires you to specify the relation name and the target
Active Record instance that the relationship should be established with. The
method will modify the values of the attributes that link two Active Record
instances and save them to the database. In the above example, it will set the
customer_id attribute of the Order instance to be the value of the id attribute
of the Customer instance and then save it to the database.
Note: You cannot link two newly created Active Record in-
stances.
The benefit of using link() is even more obvious when a relation is defined
via a junction table. For example, you may use the following code to link an
Order instance with an Item instance:
$order->link(’items’, $item);
The above code will automatically insert a row in the order_item junction
table to relate the order with the item.
Info: The link() method will NOT perform any data validation
while saving the affected Active Record instance. It is your re-
sponsibility to validate any input data before calling this method.
By default, the unlink() method will set the foreign key value(s) that specify
the existing relationship to be null. You may, however, choose to delete the
table row that contains the foreign key value by passing the $delete parameter
as true to the method.
When a junction table is involved in a relation, calling unlink() will
cause the foreign keys in the junction table to be cleared, or the deletion of
the corresponding row in the junction table if $delete is true.
$customers = Customer::find()->with(’comments’)->all();
You can use most of the relational query features that have been described
in this section.
use yii\db\ActiveRecord;
Now whenever you are performing a query (e.g. find(), findOne()) or defining
a relation (e.g. hasOne()) with Comment, you will be calling an instance of
CommentQuery instead of ActiveQuery.
You now have to define the CommentQuery class, which can be customized in
many creative ways to improve your query building experience. For example,
// file CommentQuery.php
namespace app\models;
use yii\db\ActiveQuery;
$customers = Customer::find()->joinWith(’activeComments’)->all();
// or alternatively
class Customer extends \yii\db\ActiveRecord
{
public function getComments()
{
return $this->hasMany(Comment::className(), [’customer_id’ => ’id’])
;
}
}
$customers = Customer::find()->joinWith([
’comments’ => function($q) {
$q->active();
}
])->all();
// ...
}
Then you need to compose a query, which calculates volume of the room and
performs the sort:
$rooms = Room::find()
->select([
’{{room}}.*’, // select all columns
’([[length]] * [[width]] * [[height]]) AS volume’, // calculate a
volume
])
->orderBy(’volume DESC’) // apply sort
->all();
Ability to select extra fields can be exceptionally useful for aggregation quer-
ies. Assume you need to display a list of customers with the count of orders
they have made. First of all, you need to declare a Customer class with orders
relation and extra field for count storage:
class Customer extends \yii\db\ActiveRecord
{
public $ordersCount;
// ...
Then you can compose a query, which joins the orders and calculates their
count:
$customers = Customer::find()
->select([
’{{customer}}.*’, // select all customer fields
’COUNT({{order}}.id) AS ordersCount’ // calculate orders count
])
->joinWith(’orders’) // ensure table junction
->groupBy(’{{customer}}.id’) // group the result to ensure aggregation
function works
->all();
$room->volume; // this value will be ‘null‘, since it was not declared yet
Using the __get() and __set() magic methods we can emulate the behavior
of a property:
class Room extends \yii\db\ActiveRecord
{
private $_volume;
return $this->_volume;
}
// ...
308 CHAPTER 6. WORKING WITH DATABASES
When the select query doesn’t provide the volume, the model will be able to
calculate it automatically using the attributes of the model.
You can calculate the aggregation fields as well using defined relations:
class Customer extends \yii\db\ActiveRecord
{
private $_ordersCount;
return $this->_ordersCount;
}
// ...
/**
* Declares normal ’orders’ relation.
*/
public function getOrders()
{
return $this->hasMany(Order::className(), [’customer_id’ => ’id’]);
}
/**
* Declares new relation based on ’orders’, which provides aggregation.
*/
public function getOrdersAggregation()
{
return $this->getOrders()
->select([’customer_id’, ’counted’ => ’count(*)’])
->groupBy(’customer_id’)
->asArray(true);
}
// ...
}
$customer = Customer::findOne($pk);
$customer->ordersCount; // output aggregation data from lazy loaded relation
during development:
2. Tim commits the new migration into the source control system (e.g.
Git, Mercurial).
3. Doug updates his repository from the source control system and re-
ceives the new migration.
And the following steps show how to deploy a new release with database
migrations to production:
1. Scott creates a release tag for the project repository that contains some
new database migrations.
2. Scott updates the source code on the production server to the release
tag.
Yii provides a set of migration command line tools that allow you to:
• create new migrations;
• apply migrations;
• revert migrations;
• re-apply migrations;
• show migration history and status.
All these tools are accessible through the command yii migrate. In this
section we will describe in detail how to accomplish various tasks using these
tools. You may also get the usage of each tool via the help command yii
help migrate.
Tip: migrations could affect not only database schema but ad-
just existing data to fit new schema, create RBAC hierarchy or
clean up cache.
The required name argument gives a brief description about the new migra-
tion. For example, if the migration is about creating a new table named news,
you may use the name create_news_table and run the following command:
yii migrate/create create_news_table
The above command will create a new PHP class file named m150101_185401_create_news_table
.php in the @app/migrations directory. The file contains the following code
which mainly declares a migration class m150101_185401_create_news_table with
the skeleton code:
<?php
use yii\db\Migration;
return false;
}
/*
// Use safeUp/safeDown to run migration code within a transaction
public function safeUp()
{
}
}
*/
}
use yii\db\Schema;
use yii\db\Migration;
Info: Not all migrations are reversible. For example, if the up()
method deletes a row of a table, you may not be able to recover
this row in the down() method. Sometimes, you may be just too
lazy to implement the down(), because it is not very common
to revert database migrations. In this case, you should return
false in the down() method to indicate that the migration is not
reversible.
6.4. DATABASE MIGRATION 313
Since version 2.0.6, you can make use of the newly introduced schema builder
which provides more convenient way of defining column schema. So the
migration above could be written like the following:
<?php
use yii\db\Migration;
A list of all available methods for defining the column types is available in
the API documentation of yii\db\SchemaBuilderTrait.
314 CHAPTER 6. WORKING WITH DATABASES
Create Table
generates
/**
* Handles the creation for table ‘post‘.
*/
class m150811_220037_create_post_table extends Migration
{
/**
* {@inheritdoc}
*/
public function up()
{
$this->createTable(’post’, [
’id’ => $this->primaryKey()
]);
}
/**
* {@inheritdoc}
*/
public function down()
{
$this->dropTable(’post’);
}
}
To create table fields right away, specify them via --fields option.
yii migrate/create create_post_table --fields="title:string,body:text"
generates
/**
* Handles the creation for table ‘post‘.
*/
class m150811_220037_create_post_table extends Migration
{
/**
* {@inheritdoc}
*/
public function up()
6.4. DATABASE MIGRATION 315
{
$this->createTable(’post’, [
’id’ => $this->primaryKey(),
’title’ => $this->string(),
’body’ => $this->text(),
]);
}
/**
* {@inheritdoc}
*/
public function down()
{
$this->dropTable(’post’);
}
}
generates
/**
* Handles the creation for table ‘post‘.
*/
class m150811_220037_create_post_table extends Migration
{
/**
* {@inheritdoc}
*/
public function up()
{
$this->createTable(’post’, [
’id’ => $this->primaryKey(),
’title’ => $this->string(12)->notNull()->unique(),
’body’ => $this->text()
]);
}
/**
* {@inheritdoc}
*/
public function down()
{
$this->dropTable(’post’);
}
}
Foreign keys Since 2.0.8 the generator supports foreign keys using the
keyword.
foreignKey
yii migrate/create create_post_table --fields="author_id:integer:notNull:
foreignKey(user),category_id:integer:defaultValue(1):foreignKey,title:
string,body:text"
generates
/**
* Handles the creation for table ‘post‘.
* Has foreign keys to the tables:
*
* - ‘user‘
* - ‘category‘
*/
class m160328_040430_create_post_table extends Migration
{
/**
* {@inheritdoc}
*/
public function up()
{
$this->createTable(’post’, [
’id’ => $this->primaryKey(),
’author_id’ => $this->integer()->notNull(),
’category_id’ => $this->integer()->defaultValue(1),
’title’ => $this->string(),
’body’ => $this->text(),
]);
/**
* {@inheritdoc}
*/
public function down()
{
// drops foreign key for table ‘user‘
$this->dropForeignKey(
’fk-post-author_id’,
’post’
);
$this->dropTable(’post’);
}
}
parameter is passed then the table name will be deduced from the column
name.
In the example above author_id:integer:notNull:foreignKey(user) will gen-
erate a column named author_id with a foreign key to the user table while
category_id:integer:defaultValue(1):foreignKey will generate a column category_id
with a foreign key to the category table.
Since 2.0.11, foreignKey keyword accepts a second parameter, separated
by whitespace. It accepts the name of the related column for the foreign
key generated. If no second parameter is passed, the column name will be
fetched from table schema. If no schema exists, primary key isn’t set or is
composite, default name id will be used.
Drop Table
generates
class m150811_220037_drop_post_table extends Migration
{
public function up()
{
$this->dropTable(’post’);
}
Add Column
If the migration name is of the form add_xxx_column_to_yyy_table then the file
content would contain addColumn and dropColumn statements necessary.
To add column:
yii migrate/create add_position_column_to_post_table --fields="position:
integer"
generates
class m150811_220037_add_position_column_to_post_table extends Migration
{
public function up()
{
$this->addColumn(’post’, ’position’, $this->integer());
}
6.4. DATABASE MIGRATION 319
Drop Column
If the migration name is of the form drop_xxx_column_from_yyy_table then the
file content would contain addColumn and dropColumn statements necessary.
yii migrate/create drop_position_column_from_post_table --fields="position:
integer"
generates
class m150811_220037_drop_position_column_from_post_table extends Migration
{
public function up()
{
$this->dropColumn(’post’, ’position’);
}
generates
/**
* Handles the creation for table ‘post_tag‘.
* Has foreign keys to the tables:
*
* - ‘post‘
* - ‘tag‘
*/
class m160328_041642_create_junction_table_for_post_and_tag_tables extends
Migration
320 CHAPTER 6. WORKING WITH DATABASES
{
/**
* {@inheritdoc}
*/
public function up()
{
$this->createTable(’post_tag’, [
’post_id’ => $this->integer(),
’tag_id’ => $this->integer(),
’created_at’ => $this->dateTime(),
’PRIMARY KEY(post_id, tag_id)’,
]);
/**
* {@inheritdoc}
*/
public function down()
{
// drops foreign key for table ‘post‘
6.4. DATABASE MIGRATION 321
$this->dropForeignKey(
’fk-post_tag-post_id’,
’post_tag’
);
$this->dropTable(’post_tag’);
}
}
Since 2.0.11 foreign key column names for junction tables are fetched from
table schema. In case table isn’t defined in schema, or the primary key isn’t
set or is composite, default name id is used.
Transactional Migrations
While performing complex DB migrations, it is important to ensure each mi-
gration to either succeed or fail as a whole so that the database can maintain
integrity and consistency. To achieve this goal, it is recommended that you
enclose the DB operations of each migration in a transaction.
An even easier way of implementing transactional migrations is to put
migration code in the safeUp() and safeDown() methods. These two methods
differ from up() and down() in that they are enclosed implicitly in a transac-
tion. As a result, if any operation in these methods fails, all prior operations
will be rolled back automatically.
In the following example, besides creating the news table we also insert
an initial row into this table.
<?php
use yii\db\Migration;
$this->insert(’news’, [
’title’ => ’test 1’,
’content’ => ’content 1’,
]);
}
Note that usually when you perform multiple DB operations in safeUp(), you
should reverse their execution order in safeDown(). In the above example we
first create the table and then insert a row in safeUp(); while in safeDown()
we first delete the row and then drop the table.
This command will list all migrations that have not been applied so far. If
you confirm that you want to apply these migrations, it will run the up()
or safeUp() method in every new migration class, one after another, in the
order of their timestamp values. If any of the migrations fails, the command
will quit without applying the rest of the migrations.
324 CHAPTER 6. WORKING WITH DATABASES
Tip: In case you don’t have command line at your server you
may try web shell28 extension.
For each migration that has been successfully applied, the command will
insert a row into a database table named migration to record the successful
application of the migration. This will allow the migration tool to identify
which migrations have been applied and which have not.
Sometimes, you may only want to apply one or a few new migrations, instead
of all available migrations. You can do so by specifying the number of mi-
grations that you want to apply when running the command. For example,
the following command will try to apply the next three available migrations:
yii migrate 3
You can also explicitly specify a particular migration to which the database
should be migrated by using the migrate/to command in one of the following
formats:
yii migrate/to 150101_185401 # using timestamp to
specify the migration
yii migrate/to "2015-01-01 18:54:01" # using a string that can
be parsed by strtotime()
yii migrate/to m150101_185401_create_news_table # using full name
yii migrate/to 1392853618 # using UNIX timestamp
If there are any unapplied migrations earlier than the specified one, they will
all be applied before the specified migration is applied.
If the specified migration has already been applied before, any later ap-
plied migrations will be reverted.
The command will modify the migration table by adding or deleting cer-
tain rows to indicate that the database has been applied migrations to the
specified one. No migrations will be applied or reverted by this command.
326 CHAPTER 6. WORKING WITH DATABASES
The migration command comes with a few command-line options that can
be used to customize its behaviors:
• interactive: boolean (defaults to true), specifies whether to perform
migrations in an interactive mode. When this is true, the user will
be prompted before the command performs certain actions. You may
want to set this to false if the command is being used in a background
process.
• migrationPath: string|array (defaults to @app/migrations), specifies the
directory storing all migration class files. This can be specified as
either a directory path or a path alias. Note that the directory must
exist, or the command may trigger an error. Since version 2.0.12 an
array can be specified for loading migrations from multiple sources.
• migrationTable: string (defaults to migration), specifies the name of the
database table for storing migration history information. The table
will be automatically created by the command if it does not exist. You
may also manually create it using the structure version varchar(255)
primary key, apply_time integer.
• db: string (defaults to db), specifies the ID of the database application
component. It represents the database that will be migrated using this
command.
• templateFile: string (defaults to @yii/views/migration.php), specifies the
path of the template file that is used for generating skeleton migration
class files. This can be specified as either a file path or a path alias.
The template file is a PHP script in which you can use a predefined
variable named $className to get the migration class name.
• generatorTemplateFiles: array (defaults to ‘[
’create_table’ => ’@yii/views/createTableMigration.php’,
’drop_table’ => ’@yii/views/dropTableMigration.php’,
’add_column’ => ’@yii/views/addColumnMigration.php’,
’drop_column’ => ’@yii/views/dropColumnMigration.php’,
’create_junction’ => ’@yii/views/createTableMigration.php’
]‘), specifies template files for generating migration code. See “Gener-
ating Migrations“ for more details.
• fields: array of column definition strings used for creating migration
code. Defaults to []. The format of each definition is COLUMN_NAME
:COLUMN_TYPE:COLUMN_DECORATOR. For example, --fields=name:string(12):
notNull produces a string column of size 12 which is not null.
The following example shows how you can use these options.
6.4. DATABASE MIGRATION 327
With the above configuration, each time you run the migration command,
the backend_migration table will be used to record the migration history. You
no longer need to specify it via the migrationTable command-line option.
Namespaced Migrations
Since 2.0.10 you can use namespaces for the migration classes. You can spe-
cify the list of the migration namespaces via migrationNamespaces. Using
of the namespaces for migration classes allows you usage of the several source
locations for the migrations. For example:
return [
’controllerMap’ => [
’migrate’ => [
’class’ => ’yii\console\controllers\MigrateController’,
’migrationPath’ => null, // disable non-namespaced migrations if
app\migrations is listed below
’migrationNamespaces’ => [
’app\migrations’, // Common migrations for the whole
application
’module\migrations’, // Migrations for the specific project’
s module
’some\extension\migrations’, // Migrations for the specific
extension
],
],
],
];
328 CHAPTER 6. WORKING WITH DATABASES
Since version 2.0.12 the migrationPath property also accepts an array for
specifying multiple directories that contain migrations without a namespace.
This is mainly added to be used in existing projects which use migrations
from different locations. These migrations mainly come from external sources,
like Yii extensions developed by other developers, which can not be changed
to use namespaces easily when starting to use the new approach.
Separated Migrations
Sometimes using single migration history for all project migrations is not de-
sirable. For example: you may install some ‘blog’ extension, which contains
fully separated functionality and contain its own migrations, which should
not affect the ones dedicated to main project functionality.
If you want several migrations to be applied and tracked down completely
separated from each other, you can configure multiple migration commands
which will use different namespaces and migration history tables:
return [
’controllerMap’ => [
// Common migrations for the whole application
’migrate-app’ => [
’class’ => ’yii\console\controllers\MigrateController’,
’migrationNamespaces’ => [’app\migrations’],
’migrationTable’ => ’migration_app’,
’migrationPath’ => null,
],
// Migrations for the specific project’s module
’migrate-module’ => [
’class’ => ’yii\console\controllers\MigrateController’,
’migrationNamespaces’ => [’module\migrations’],
’migrationTable’ => ’migration_module’,
6.4. DATABASE MIGRATION 329
Note that to synchronize database you now need to run multiple commands
instead of one:
yii migrate-app
yii migrate-module
yii migrate-rbac
use yii\db\Migration;
The above migration will be applied to db2, even if you specify a different
database through the db command-line option. Note that the migration
history will still be recorded in the database specified by the db command-
line option.
If you have multiple migrations that use the same database, it is recom-
mended that you create a base migration class with the above init() code.
Then each migration class can extend from this base class.
330 CHAPTER 6. WORKING WITH DATABASES
Another strategy that you can take to migrate multiple databases is to keep
migrations for different databases in different migration paths. Then you
can migrate these databases in separate commands like the following:
yii migrate --migrationPath=@app/migrations/db1 --db=db1
yii migrate --migrationPath=@app/migrations/db2 --db=db2
...
In the following example, we show how a generic model can be used for a
login form:
<?php
335
336 CHAPTER 7. GETTING DATA FROM USERS
];
}
}
In the controller, we will pass an instance of that model to the view, wherein
the ActiveForm widget is used to display the form:
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
$form = ActiveForm::begin([
’id’ => ’login-form’,
’options’ => [’class’ => ’form-horizontal’],
]) ?>
<?= $form->field($model, ’username’) ?>
<?= $form->field($model, ’password’)->passwordInput() ?>
<div class="form-group">
<div class="col-lg-offset-1 col-lg-11">
<?= Html::submitButton(’Login’, [’class’ => ’btn btn-primary’])
?>
</div>
</div>
<?php ActiveForm::end() ?>
ActiveField
In order to create a form element in the form, along with the element’s
label, and any applicable JavaScript validation, the ActiveForm::field()
method is called, which returns an instance of yii\widgets\ActiveField.
When the result of this method is echoed directly, the result is a regular
(text) input. To customize the output, you can chain additional methods of
ActiveField to this call:
// a password input
<?= $form->field($model, ’password’)->passwordInput() ?>
// adding a hint and a customized label
<?= $form->field($model, ’username’)->textInput()->hint(’Please enter your
name’)->label(’Name’) ?>
7.1. CREATING FORMS 337
This will create all the <label>, <input> and other tags according to the
template defined by the form field. The name of the input field is determined
automatically from the model’s form name and the attribute name. For
example, the name for the input field for the username attribute in the above
example will be LoginForm[username]. This naming rule will result in an array
of all attributes for the login form to be available in $_POST[’LoginForm’] on
the server-side.
Tip: If you have only one model in a form and want to simplify
the input names you may skip the array part by overriding the
formName() method of the model to return an empty string. This
can be useful for filter models used in the GridView to create nicer
URLs.
Specifying the attribute of the model can be done in more sophisticated ways.
For example when an attribute may take an array value when uploading
multiple files or selecting multiple items you may specify it by appending []
to the attribute name:
// allow multiple files to be uploaded:
echo $form->field($model, ’uploadFile[]’)->fileInput([’multiple’=>’multiple’
]);
Forms and their child elements should not use input names or ids
that conflict with properties of a form, such as submit, length, or
method. Name conflicts can cause confusing failures. For a com-
plete list of rules and to check your markup for these problems,
see DOMLint2 .
Additional HTML tags can be added to the form using plain HTML or using
the methods from the Html-helper class like it is done in the above example
with Html::submitButton().
Tip: In order to style required fields with asterisks, you can use
the following CSS:
div.required label.control-label:after {
content: " *";
color: red;
}
These $items have to be processed by the different list widgets. The value of
the form field (and the current active item) will be automatically set by the
current value of the $model‘s attribute.
Pjax::begin([
// Pjax options
]);
$form = ActiveForm::begin([
’options’ => [’data’ => [’pjax’ => true]],
// more ActiveForm options
]);
// ActiveForm content
ActiveForm::end();
Pjax::end();
Tip: Be careful with the links inside the Pjax widget since the
response will also be rendered inside the widget. To prevent this,
use the data-pjax="0" HTML attribute.
Values in Submit Buttons and File Upload There are known issues
using jQuery.serializeArray() when dealing with files3 and submit button
3
https://github.com/jquery/jquery/issues/2321
340 CHAPTER 7. GETTING DATA FROM USERS
values4 which won’t be solved and are instead deprecated in favor of the
FormData class introduced in HTML5.
That means the only official support for files and submit button values
with ajax or using the Pjax widget depends on the browser support5 for the
FormData class.
if ($model->validate()) {
// all inputs are valid
} else {
// validation failed: $errors is an array containing error messages
$errors = $model->errors;
}
4
https://github.com/jquery/jquery/issues/2321
5
https://developer.mozilla.org/en-US/docs/Web/API/FormData#Browser_
compatibility
7.2. VALIDATING INPUT 341
For each rule you must specify at least which attributes the rule applies to
and what is the type of the rule. You can specify the rule type in one of the
following forms:
• the alias of a core validator, such as required, in, date, etc. Please refer
to the Core Validators for the complete list of core validators.
• the name of a validation method in the model class, or an anonymous
function. Please refer to the Inline Validators subsection for more
details.
342 CHAPTER 7. GETTING DATA FROM USERS
2. Determine which validation rules should be used by getting the rule list
from yii\base\Model::rules() using the current scenario. These
rules are called active rules.
3. Use each active rule to validate each active attribute which is associated
with the rule. The validation rules are evaluated in the order they are
listed.
Validation Events
When yii\base\Model::validate() is called, it will call two methods that
you may override to customize the validation process:
• yii\base\Model::beforeValidate(): the default implementation will
trigger a yii\base\Model::EVENT_BEFORE_VALIDATE event. You may
either override this method or respond to this event to do some pre-
processing work (e.g. normalizing data inputs) before the validation
occurs. The method should return a boolean value indicating whether
the validation should proceed or not.
• yii\base\Model::afterValidate(): the default implementation will
trigger a yii\base\Model::EVENT_AFTER_VALIDATE event. You may
either override this method or respond to this event to do some post-
processing work after the validation is completed.
Conditional Validation
To validate attributes only when certain conditions apply, e.g. the validation
of one attribute depends on the value of another attribute you can use the
when property to define such conditions. For example,
[’state’, ’required’, ’when’ => function($model) {
return $model->country == ’USA’;
}]
The when property takes a PHP callable with the following signature:
/**
* @param Model $model the model being validated
* @param string $attribute the attribute being validated
* @return bool whether the rule should be applied
344 CHAPTER 7. GETTING DATA FROM USERS
*/
function ($model, $attribute)
If you also need to support client-side conditional validation, you should con-
figure the whenClient property which takes a string representing a JavaS-
cript function whose return value determines whether to apply the rule or
not. For example,
[’state’, ’required’, ’when’ => function ($model) {
return $model->country == ’USA’;
}, ’whenClient’ => "function (attribute, value) {
return $(’#country’).val() == ’USA’;
}"]
Data Filtering
User inputs often need to be filtered or preprocessed. For example, you may
want to trim the spaces around the username input. You may use validation
rules to achieve this goal.
The following examples shows how to trim the spaces in the inputs and
turn empty inputs into nulls by using the trim and default core validators:
return [
[[’username’, ’email’], ’trim’],
[[’username’, ’email’], ’default’],
];
You may also use the more general filter validator to perform more complex
data filtering.
As you can see, these validation rules do not really validate the inputs.
Instead, they will process the values and save them back to the attributes
being validated.
A complete processing of user input is shown in the following example
code, which will ensure only integer values are stored in an attribute:
[’age’, ’trim’],
[’age’, ’default’, ’value’ => null],
[’age’, ’integer’, ’min’ => 0],
[’age’, ’filter’, ’filter’ => ’intval’, ’skipOnEmpty’ => true],
The above code will perform the following operations on the input:
4. Make sure the value is of type integer, e.g. casting a string ’42’ to
integer 42. Here we set $skipOnEmpty to true, which is false by default
on the filter validator.
if ($validator->validate($email, $error)) {
echo ’Email is valid.’;
} else {
echo $error;
}
346 CHAPTER 7. GETTING DATA FROM USERS
If you need to perform multiple validations against several values, you can
use yii\base\DynamicModel which supports declaring both attributes and
rules on the fly. Its usage is like the following:
if ($model->hasErrors()) {
// validation fails
} else {
// validation succeeds
}
}
if ($model->hasErrors()) {
// validation fails
} else {
// validation succeeds
}
}
After validation, you can check if the validation succeeded or not by calling
the hasErrors() method, and then get the validation errors from the errors
property, like you do with a normal model. You may also access the dynamic
attributes defined through the model instance, e.g., $model->name and $model
->email.
7.2. VALIDATING INPUT 347
Inline Validators
An inline validator is one defined in terms of a model method or an anonym-
ous function. The signature of the method/function is:
/**
* @param string $attribute the attribute currently being validated
* @param mixed $params the value of the "params" given in the rule
* @param \yii\validators\InlineValidator $validator related InlineValidator
instance.
* This parameter is available since version 2.0.11.
*/
function ($attribute, $params, $validator)
If an attribute fails the validation, the method/function should call yii\base
\Model::addError() to save the error message in the model so that it can
be retrieved back later to present to end users.
Below are some examples:
use yii\base\Model;
}
}
}
Standalone Validators
A standalone validator is a class extending yii\validators\Validator or
its child class. You may implement its validation logic by overriding the
yii\validators\Validator::validateAttribute() method. If an attrib-
ute fails the validation, call yii\base\Model::addError() to save the error
message in the model, like you do with inline validators.
For example, the inline validator above could be moved into new [[com-
ponents/validators/CountryValidator]] class. In this case we can use yii
\validators\Validator::addError() to set customized message for the
model.
namespace app\components;
use yii\validators\Validator;
use Yii;
use yii\base\Model;
use app\components\validators\CountryValidator;
public $personalSalary;
public $spouseSalary;
public $childrenCount;
public $description;
Creating validator
Let’s say we need to check if the family income is enough for children. We
can create inline validator validateChildrenFunds for that which will run only
when childrenCount is more than 0.
Note that we can’t use all validated attributes ([’personalSalary’, ’spouseSalary
’, ’childrenCount’]) when attaching validator. This is because the same val-
idator will run for each attribute (3 times in total) and we only need to run
it once for the whole attribute set.
You can use any of these attributes instead (or use what you think is the
most relevant):
[’childrenCount’, ’validateChildrenFunds’, ’when’ => function ($model) {
return $model->childrenCount > 0;
}],
You can ignore $attribute parameter because validation is not related to just
one attribute.
7.2. VALIDATING INPUT 351
Adding errors
Adding error in case of multiple attributes can vary depending on desired
form design:
• Select the most relevant field in your opinion and add error to it’s
attribute:
$this->addError(’childrenCount’, ’Your salary is not enough for children.’);
Or use a loop:
$attributes = [’personalSalary’, ’wifeSalary’, ’childrenCount’];
foreach ($attributes as $attribute) {
$this->addError($attribute, ’Your salary is not enough for children.’);
}
As a result, we will not see error message near form fields. To display it, we
can include the error summary in view:
<?= $form->errorSummary($model) ?>
Similar to input data coming from end users, you should never
trust client-side validation. For this reason, you should always
perform server-side validation by calling yii\base\Model::validate(),
as described in the previous subsections.
use yii\base\Model;
use app\models\User;
if (!$user || !$user->validatePassword($this->password)) {
$this->addError(’password’, ’Incorrect username or password.’);
}
}
}
The HTML form built by the following code contains two input fields username
and password. If you submit the form without entering anything, you will
find the error messages requiring you to enter something appear right away
without any communication with the server.
<?php $form = yii\widgets\ActiveForm::begin(); ?>
<?= $form->field($model, ’username’) ?>
7.2. VALIDATING INPUT 353
return $options;
}
use yii\validators\Validator;
use app\models\Status;
Tip: If you need to work with client validation manually i.e. dy-
namically add fields or do some custom UI logic, refer to Working
7.2. VALIDATING INPUT 355
Deferred Validation
deferred.push(def);
JS;
}
7
https://github.com/samdark/yii2-cookbook/blob/master/book/
forms-activeform-js.md
8
http://api.jquery.com/category/deferred-object/
356 CHAPTER 7. GETTING DATA FROM USERS
For simplicity, the deferred array is equipped with a shortcut method add()
which automatically creates a Deferred object and adds it to the deferred
array. Using this method, you can simplify the above example as follows,
public function clientValidateAttribute($model, $attribute, $view)
{
return <<<JS
deferred.add(function(def) {
var img = new Image();
img.onload = function() {
if (this.width > 150) {
messages.push(’Image too wide!!’);
}
def.resolve();
}
var reader = new FileReader();
reader.onloadend = function() {
img.src = reader.result;
}
reader.readAsDataURL(file);
});
JS;
}
$form = ActiveForm::begin([
’id’ => ’registration-form’,
]);
// ...
ActiveForm::end();
7.3. UPLOADING FILES 357
To enable AJAX validation for all inputs of the form, configure enableAjaxValidation
to be true at the form level:
$form = ActiveForm::begin([
’id’ => ’contact-form’,
’enableAjaxValidation’ => true,
]);
You also need to prepare the server so that it can handle the AJAX validation
requests. This can be achieved by a code snippet like the following in the
controller actions:
if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post()))
{
Yii::$app->response->format = Response::FORMAT_JSON;
return ActiveForm::validate($model);
}
The above code will check whether the current request is an AJAX. If yes,
it will respond to this request by running the validation and returning the
errors in JSON format.
instance. You should also declare a validation rule to validate the file upload.
For example,
namespace app\models;
use yii\base\Model;
use yii\web\UploadedFile;
In the code above, the imageFile attribute is used to keep the uploaded
file instance. It is associated with a file validation rule which uses yii
\validators\FileValidator to ensure a file with extension name png or
jpg is uploaded. The upload() method will perform the validation and save
the uploaded file on the server.
The file validator allows you to check file extensions, size, MIME type,
etc. Please refer to the Core Validators section for more details.
<button>Submit</button>
It is important to remember that you add the enctype option to the form so
that the file can be properly uploaded. The fileInput() call will render a
<input type="file"> tag which will allow users to select a file to upload.
7.3.3 Wiring Up
Now in a controller action, write the code to wire up the model and the view
to implement file uploading:
namespace app\controllers;
use Yii;
use yii\web\Controller;
use app\models\UploadForm;
use yii\web\UploadedFile;
if (Yii::$app->request->isPost) {
$model->imageFile = UploadedFile::getInstance($model, ’imageFile
’);
if ($model->upload()) {
// file is uploaded successfully
return;
}
}
use yii\base\Model;
use yii\web\UploadedFile;
10
http://php.net/manual/en/ini.core.php#ini.max-file-uploads
7.4. COLLECTING TABULAR INPUT 361
In the view file, you should add the multiple option to the fileInput() call
so that the file upload field can receive multiple files:
<?php
use yii\widgets\ActiveForm;
?>
<button>Submit</button>
use Yii;
use yii\web\Controller;
use app\models\UploadForm;
use yii\web\UploadedFile;
if (Yii::$app->request->isPost) {
$model->imageFiles = UploadedFile::getInstances($model, ’
imageFiles’);
if ($model->upload()) {
// file is uploaded successfully
return;
}
}
value pair and is represented by a Setting active record model. This kind of
form is also often referred to as “tabular input”. In contrast to this, handling
different models of different kind, is handled in the section Complex Forms
with Multiple Models.
The following shows how to implement tabular input with Yii.
There are three different situations to cover, which have to be handled
slightly different:
• Updating a fixed set of records from the database
• Creating a dynamic set of new records
• Updating, creating and deleting of records on one page
In contrast to the single model forms explained before, we are working with
an array of models now. This array is passed to the view to display the input
fields for each model in a table like style and we will use helper methods of
yii\base\Model that allow loading and validating multiple models at once:
• Model::loadMultiple() load post data into an array of models.
• Model::validateMultiple() validates an array of models.
namespace app\controllers;
use Yii;
use yii\base\Model;
use yii\web\Controller;
use app\models\Setting;
In the code above we’re using indexBy() when retrieving models from the
database to populate an array indexed by models primary keys. These will
be later used to identify form fields. Model::loadMultiple() fills multiple
models with the form data coming from POST and Model::validateMultiple()
validates all models at once. As we have validated our models before, using
validateMultiple(), we’re now passing false as a parameter to save() to not
run validation twice.
Now the form that’s in update view:
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
$form = ActiveForm::begin();
ActiveForm::end();
Here for each setting we are rendering name and an input with a value. It
is important to add a proper index to input name since that is how Model
::loadMultiple() determines which model to fill with which values.
// ...
}
TBD
use Yii;
use yii\base\Model;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use app\models\User;
use app\models\Profile;
$profile = Profile::findOne($user->profile_id);
if (!$profile) {
throw new NotFoundHttpException("The user has no profile.");
}
$user->scenario = ’update’;
$profile->scenario = ’update’;
}
}
return $this->render(’update’, [
’user’ => $user,
’profile’ => $profile,
]);
}
}
In the update action, we first load the $user and $profile models to be updated
from the database. We then call yii\base\Model::load() to populate these
two models with the user input. If loading is successful, we will validate the
two models and then save them — please note that we use save(false)
to skip over validations inside the models as the user input data have already
been validated. If loading is not successful, we will render the update view
which has the following content:
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
$form = ActiveForm::begin([
’id’ => ’user-update-form’,
’options’ => [’class’ => ’form-horizontal’],
]) ?>
<?= $form->field($user, ’username’) ?>
As you can see, in the update view you would render input fields using two
models $user and $profile.
beforeValidate
where
• event: an Event object.
• messages: an associative array with keys being attribute IDs and values
being error message arrays for the corresponding attributes.
• deferreds: an array of Deferred objects. You can use deferreds.add(
callback) to add a new deferred validation.
If the handler returns a boolean false, it will stop further form validation
after this event. And as a result, afterValidate event will not be triggered.
afterValidate
where
• event: an Event object.
• messages: an associative array with keys being attribute IDs and values
being error message arrays for the corresponding attributes.
• errorAttributes: an array of attributes that have validation errors.
Please refer to attributeDefaults for the structure of this parameter.
beforeValidateAttribute
where
• event: an Event object.
• attribute: the attribute to be validated. Please refer to attributeDefaults
for the structure of this parameter.
7.6. EXTENDING ACTIVEFORM ON THE CLIENT SIDE 367
• messages: an array to which you can add validation error messages for
the specified attribute.
• deferreds: an array of Deferred objects. You can use deferreds.add(
callback) to add a new deferred validation.
If the handler returns a boolean false, it will stop further validation of the
specified attribute. And as a result, afterValidateAttribute event will not be
triggered.
afterValidateAttribute
where
• event: an Event object.
• attribute: the attribute being validated. Please refer to attributeDefaults
for the structure of this parameter.
• messages: an array to which you can add additional validation error
messages for the specified attribute.
beforeSubmit
beforeSubmit event is triggered before submitting the form after all validations
have passed.
The signature of the event handler should be:
function (event)
ajaxBeforeSend
where
• event: an Event object.
• jqXHR: a jqXHR object
• settings: the settings for the AJAX request
368 CHAPTER 7. GETTING DATA FROM USERS
ajaxComplete
where
• event: an Event object.
• jqXHR: a jqXHR object
• textStatus: the status of the request ("success”, “notmodified”, “error”,
“timeout”, “abort”, or “parsererror”).
To learn more about the jQuery ajax() function, please refer to the jQuery
documentation11 .
$(’#contact-form’).yiiActiveForm(’add’, {
id: ’address’,
name: ’address’,
container: ’.field-address’,
input: ’#address’,
error: ’.help-block’,
validate: function (attribute, value, messages, deferred, $form) {
yii.validation.required(value, messages, {message: "Validation
Message Here"});
}
});
To remove a field from validation list so it’s not validated you can do the
following:
$(’#contact-form’).yiiActiveForm(’remove’, ’address’);
370 CHAPTER 7. GETTING DATA FROM USERS
Chapter 8
Displaying Data
// output: 12.50%
echo $formatter->asPercent(0.125, 2);
// output: Yes
echo $formatter->asBoolean(true);
// it also handles display of null values:
As you can see, all these methods are named as asXyz(), where Xyz stands for
a supported format. Alternatively, you may format data using the generic
method format(), which allows you to control the desired format program-
matically and is commonly used by widgets like yii\grid\GridView and
yii\widgets\DetailView. For example,
// output: January 1, 2014
echo Yii::$app->formatter->format(’2014-01-01’, ’date’);
// you can also use an array to specify parameters for the format method:
371
372 CHAPTER 8. DISPLAYING DATA
// PHP date()-format
echo Yii::$app->formatter->asDate(’now’, ’php:Y-m-d’); // 2014-10-06
Info: Some letters of the PHP format syntax are not supported
by ICU and thus the PHP intl extension and can not be used in
Yii formatter. Most of these (w, t, L, B, u, I, Z) are not really useful
for formatting dates but rather used when doing date math. S
and U however may be useful. Their behavior can be achived by
doing the following:
• for S, which is the English ordinal suffix for the day of the
month (e.g. st, nd, rd or th.), the following replacement can
be used:
$f = Yii::$app->formatter;
$d = $f->asOrdinal($f->asDate(’2017-05-15’, ’php:j’));
echo "On the $d day of the month."; // prints "On the 15th
day of the month."
• for U, the Unix Epoch, you can use the timestamp format.
Time Zones
When formatting date and time values, Yii will convert them to the target
time zone. The value being formatted is assumed to be in UTC, unless a
time zone is explicitly given or you have configured yii\i18n\Formatter::
$defaultTimeZone.
In the following examples, we assume the target time zone is set as
Europe/Berlin.
// formatting a UNIX timestamp as a time
echo Yii::$app->formatter->asTime(1412599260); // 14:41:00
If the time zone is not set explicitly on the formatter component, the time
zone configured in the application is used, which is the same time zone
as set in the PHP configuration.
) translated into the current application language. You can configure the
nullDisplay property to customize this string.
Yii::$app->formatter->locale = ’de-DE’;
echo Yii::$app->formatter->asDate(’2014-01-01’); // output: 1. Januar 2014
Yii::$app->formatter->locale = ’ru-RU’;
echo Yii::$app->formatter->asDate(’2014-01-01’); // output: 1 января 2014 г.
8.2 Pagination
When there are too much data to be displayed on a single page, a common
strategy is to display them in multiple pages and on each page only display
a small portion of the data. This strategy is known as pagination.
Yii uses a yii\data\Pagination object to represent the information
about a pagination scheme. In particular,
7
http://php.net/manual/en/book.intl.php
8.2. PAGINATION 377
• total count specifies the total number of data items. Note that this
is usually much more than the number of data items needed to display
on a single page.
• page size specifies how many data items each page contains. The
default value is 20.
• current page gives the current page number (zero-based). The default
value is 0, meaning the first page.
With a fully specified yii\data\Pagination object, you can retrieve and
display data partially. For example, if you are fetching data from a data-
base, you can specify the OFFSET and LIMIT clause of the DB query with the
corresponding values provided by the pagination. Below is an example,
use yii\data\Pagination;
// get the total number of articles (but do not fetch the article data yet)
$count = $query->count();
// limit the query using the pagination and retrieve the articles
$articles = $query->offset($pagination->offset)
->limit($pagination->limit)
->all();
echo LinkPager::widget([
’pagination’ => $pagination,
]);
$pagination->route = ’article/index’;
// displays: /index.php?r=article%2Findex&page=100
echo $pagination->createUrl(100);
// displays: /index.php?r=article%2Findex&page=101
echo $pagination->createUrl(101);
Tip: You can customize the name of the page query parameter by
configuring the pageParam property when creating the pagination
object.
8.3 Sorting
When displaying multiple rows of data, it is often needed that the data be
sorted according to some columns specified by end users. Yii uses a yii
\data\Sort object to represent the information about a sorting schema. In
particular,
• attributes specifies the attributes by which the data can be sorted.
An attribute can be as simple as a model attribute. It can also be a
composite one by combining multiple model attributes or DB columns.
More details will be given in the following.
• attributeOrders gives the currently requested ordering directions for
each attribute.
• orders gives the ordering directions in terms of the low-level columns.
To use yii\data\Sort, first declare which attributes can be sorted. Then
retrieve the currently requested ordering information from attributeOrders
or orders and use them to customize the data query. For example,
use yii\data\Sort;
$articles = Article::find()
->where([’status’ => 1])
->orderBy($sort->orders)
->all();
8.3. SORTING 379
In the above example, two attributes are declared for the Sort object: age
and name.
The age attribute is a simple attribute corresponding to the age attribute
of the Article Active Record class. It is equivalent to the following declara-
tion:
’age’ => [
’asc’ => [’age’ => SORT_ASC],
’desc’ => [’age’ => SORT_DESC],
’default’ => SORT_ASC,
’label’ => Inflector::camel2words(’age’),
]
Info: You can directly feed the value of orders to the database
query to build its ORDER BY clause. Do not use attributeOrders
because some of the attributes may be composite and cannot be
recognized by the database query.
// displays: /index.php?r=article%2Findex&sort=age
echo $sort->createUrl(’age’);
380 CHAPTER 8. DISPLAYING DATA
You specify the pagination and sorting behaviors of a data provider by con-
figuring its pagination and sort properties which correspond to the config-
urations for yii\data\Pagination and yii\data\Sort, respectively. You
may also configure them to be false to disable pagination and/or sorting
features.
8.4. DATA PROVIDERS 381
These data providers mainly vary in the way how the data source is specified.
In the following subsections, we will explain the detailed usage of each of
these data providers.
If $query in the above example is created using the following code, then the
data provider will return raw arrays.
use yii\db\Query;
$count = Yii::$app->db->createCommand(’
SELECT COUNT(*) FROM post WHERE status=:status
’, [’:status’ => 1])->queryScalar();
$data = [
[’id’ => 1, ’name’ => ’name 1’, ...],
[’id’ => 2, ’name’ => ’name 2’, ...],
...
[’id’ => 100, ’name’ => ’name 100’, ...],
];
/**
* @var string|callable name of the key column or a callable returning
it
*/
8.4. DATA PROVIDERS 385
public $key;
/**
* @var SplFileObject
*/
protected $fileObject; // SplFileObject is very convenient for seeking
to particular line in a file
/**
* {@inheritdoc}
*/
public function init()
{
parent::init();
// open file
$this->fileObject = new SplFileObject($this->filename);
}
/**
* {@inheritdoc}
*/
protected function prepareModels()
{
$models = [];
$pagination = $this->getPagination();
return $models;
}
/**
* {@inheritdoc}
*/
protected function prepareKeys($models)
{
386 CHAPTER 8. DISPLAYING DATA
return $keys;
}
return array_keys($models);
}
/**
* {@inheritdoc}
*/
protected function prepareTotalCount()
{
$count = 0;
while (!$this->fileObject->eof()) {
$this->fileObject->next();
++$count;
}
return $count;
}
}
$filterCondition = null;
return $filter;
}
}
$query = Post::find();
if ($filterCondition !== null) {
$query->andWhere($filterCondition);
}
PostSearch model serves the purpose of defining which properties and values
are allowed for filtering:
use yii\base\Model;
Data filters are quite flexible. You may customize how conditions are built
and which operators are allowed. For details check API docs on yii\data
\DataFilter.
8.5.1 DetailView
The DetailView widget displays the details of a single data model.
It is best used for displaying a model in a regular format (e.g. each
model attribute is displayed as a row in a table). The model can be either
an instance or subclass of yii\base\Model such as an active record or an
associative array.
388 CHAPTER 8. DISPLAYING DATA
8.5.2 ListView
The ListView widget is used to display data from a data provider. Each data
model is rendered using the specified view file. Since it provides features
8.5. DATA WIDGETS 389
such as pagination, sorting and filtering out of the box, it is handy both to
display information to end user and to create data managing UI.
A typical usage is as follows:
use yii\widgets\ListView;
use yii\data\ActiveDataProvider;
<?php
use yii\helpers\Html;
use yii\helpers\HtmlPurifier;
?>
<div class="post">
<h2><?= Html::encode($model->title) ?></h2>
In the view file above, the current data model is available as $model. Addi-
tionally the following variables are available:
• $key: mixed, the key value associated with the data item.
• $index: integer, the zero-based index of the data item in the items array
returned by the data provider.
• $widget: ListView, this widget instance.
If you need to pass additional data to each view, you can use the $viewParams
property to pass key value pairs like the following:
echo ListView::widget([
’dataProvider’ => $dataProvider,
’itemView’ => ’_post’,
’viewParams’ => [
’fullView’ => true,
’context’ => ’main-page’,
// ...
],
]);
8.5.3 GridView
Data grid or GridView is one of the most powerful Yii widgets. It is extremely
useful if you need to quickly build the admin section of the system. It takes
data from a data provider and renders each row using a set of columns
presenting data in the form of a table.
Each row of the table represents the data of a single data item, and
a column usually represents an attribute of the item (some columns may
correspond to complex expressions of attributes or static text).
The minimal code needed to use GridView is as follows:
use yii\grid\GridView;
use yii\data\ActiveDataProvider;
The above code first creates a data provider and then uses GridView to
display every attribute in every row taken from the data provider. The
displayed table is equipped with sorting and pagination functionality out of
the box.
Grid columns
The columns of the grid table are configured in terms of yii\grid\Column
classes, which are configured in the columns property of GridView configur-
ation. Depending on column type and settings these are able to present data
differently. The default class is yii\grid\DataColumn, which represents a
model attribute and can be sorted and filtered by.
echo GridView::widget([
’dataProvider’ => $dataProvider,
’columns’ => [
[’class’ => ’yii\grid\SerialColumn’],
// Simple columns defined by the data contained in $dataProvider.
// Data from the model’s column will be used.
’id’,
’username’,
// More complex one.
[
’class’ => ’yii\grid\DataColumn’, // can be omitted, as it is
the default
’value’ => function ($data) {
return $data->name; // $data[’name’] for array data, e.g.
using SqlDataProvider.
8.5. DATA WIDGETS 391
},
],
],
]);
Note that if the columns part of the configuration isn’t specified, Yii tries
to show all possible columns of the data provider’s model.
Column classes
Grid columns could be customized by using different column classes:
echo GridView::widget([
’dataProvider’ => $dataProvider,
’columns’ => [
[
’class’ => ’yii\grid\SerialColumn’, // <-- here
// you may configure additional properties here
],
In addition to column classes provided by Yii that we’ll review below, you
can create your own column classes.
Each column class extends from yii\grid\Column so that there are some
common options you can set while configuring grid columns.
• header allows to set content for header row.
• footer allows to set content for footer row.
• visible defines if the column should be visible.
• content allows you to pass a valid PHP callback that will return data
for a row. The format is the following:
function ($model, $key, $index, $column) {
return ’a string’;
}
You may specify various container HTML options by passing arrays to:
• headerOptions
• footerOptions
• filterOptions
• contentOptions
Data column Data column is used for displaying and sorting data. It is
the default column type so the specifying class could be omitted when using
it.
The main setting of the data column is its format property. Its val-
ues correspond to methods in the formatter application component that is
Formatter by default:
echo GridView::widget([
’columns’ => [
[
’attribute’ => ’name’,
392 CHAPTER 8. DISPLAYING DATA
controller action IDs (also called button names in the context of action
column). They will be replaced by the corresponding button rendering
callbacks specified in buttons. For example, the token {view} will be
replaced by the result of the callback buttons[’view’]. If a callback
cannot be found, the token will be replaced with an empty string. The
default tokens are {view} {update} {delete}.
• buttons is an array of button rendering callbacks. The array keys
are the button names (without curly brackets), and the values are the
corresponding button rendering callbacks. The callbacks should use
the following signature:
function ($url, $model, $key) {
// return the button HTML code
}
In the code above, $url is the URL that the column creates for the
button, $model is the model object being rendered for the current row,
and $key is the key of the model in the data provider array.
• urlCreator is a callback that creates a button URL using the specified
model information. The signature of the callback should be the same
as that of yii\grid\ActionColumn::createUrl(). If this property is
not set, button URLs will be created using yii\grid\ActionColumn
::createUrl().
• visibleButtons is an array of visibility conditions for each button.
The array keys are the button names (without curly brackets), and the
values are the boolean true/false or the anonymous function. When
the button name is not specified in this array it will be shown by
default. The callbacks must use the following signature:
function ($model, $key, $index) {
return $model->status === ’editable’;
}
],
],
Users may click on the checkboxes to select rows of the grid. The selected
rows may be obtained by calling the following JavaScript code:
var keys = $(’#grid’).yiiGridView(’getSelectedRows’);
// keys is an array consisting of the keys associated with the selected rows
Serial column Serial column renders row numbers starting with 1 and
going forward.
Usage is as simple as the following:
echo GridView::widget([
’dataProvider’ => $dataProvider,
’columns’ => [
[’class’ => ’yii\grid\SerialColumn’], // <-- here
// ...
Sorting data
Note: This section is under development.
• https://github.com/yiisoft/yii2/issues/1576
Filtering data
For filtering data, the GridView needs a model that represents the search
criteria which is usually taken from the filter fields in the GridView table.
A common practice when using active records is to create a search Model
class that provides needed functionality (it can be generated for you by Gii).
This class defines the validation rules to show filter controls on the GridView
table and to provide a search() method that will return the data provider
with an adjusted query that processes the search criteria.
To add the search capability for the Post model, we can create a PostSearch
model like the following example:
<?php
namespace app\models;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
[[’id’], ’integer’],
[[’title’, ’creation_date’], ’safe’],
];
}
return $dataProvider;
}
}
You can use this function in the controller to get the dataProvider for the
GridView:
$searchModel = new PostSearch();
$dataProvider = $searchModel->search(Yii::$app->request->get());
return $this->render(’myview’, [
’dataProvider’ => $dataProvider,
’searchModel’ => $searchModel,
]);
And in the view you then assign the $dataProvider and $searchModel to the
GridView:
echo GridView::widget([
’dataProvider’ => $dataProvider,
396 CHAPTER 8. DISPLAYING DATA
use yii\helpers\Html;
use yii\widgets\ActiveForm;
<div class="post-search">
<?php $form = ActiveForm::begin([
’action’ => [’index’],
’method’ => ’get’,
]); ?>
<div class="form-group">
<?= Html::submitButton(’Search’, [’class’ => ’btn btn-primary’]) ?>
<?= Html::submitButton(’Reset’, [’class’ => ’btn btn-default’]) ?>
</div>
Note: if you use Gii to generate CRUD code, the separate filter
form (_search.php) is generated by default, but is commented in
index.php view. Uncomment it and it’s ready to use!
Separate filter form is useful when you need to filter by fields, that are not
displayed in GridView or for special filtering conditions, like date range.
For filtering by date range we can add non DB attributes createdFrom and
createdTo to the search model:
8.5. DATA WIDGETS 397
/**
* @var string
*/
public $createdTo;
}
// ...
Filtering also needs the joinWith call as above. You also need to define the
searchable column in attributes and rules like this:
public function attributes()
{
// add related fields to searchable attributes
return array_merge(parent::attributes(), [’author.name’]);
}
Info: In the above we use the same string for the relation name
and the table alias; however, when your alias and relation name
differ, you have to pay attention to where you use the alias and
where you use the relation name. A simple rule for this is to use
the alias in every place that is used to build the database query
and the relation name in all other definitions such as attributes()
and rules() etc.
For example, if you use the alias au for the author relation table,
the joinWith statement looks like the following:
$query->joinWith([’author au’]);
Using SQL views for filtering, sorting and displaying data There
is also another approach that can be faster and more useful - SQL views.
For example, if we need to show the gridview with users and their profiles,
we can do so in this way:
CREATE OR REPLACE VIEW vw_user_info AS
SELECT user.*, user_profile.lastname, user_profile.firstname
FROM user, user_profile
WHERE user.id = user_profile.user_id
Then you need to create the ActiveRecord that will be representing this
view:
namespace app\models\views\grid;
use yii\db\ActiveRecord;
/**
* {@inheritdoc}
*/
public static function tableName()
{
return ’vw_user_info’;
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
// define here your rules
];
}
400 CHAPTER 8. DISPLAYING DATA
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
// define here your attribute labels
];
}
After that you can use this UserView active record with search models,
without additional specification of sorting and filtering attributes. All at-
tributes will be working out of the box. Note that this approach has several
pros and cons:
• you don’t need to specify different sorting and filtering conditions.
Everything works out of the box;
• it can be much faster because of the data size, count of sql queries
performed (for each relation you will not need any additional query);
• since this is just a simple mapping UI on the sql view it lacks some
domain logic that is in your entities, so if you have some methods like
isActive, isDeleted or others that will influence the UI, you will need
to duplicate them in this class too.
$userProvider->pagination->pageParam = ’user-page’;
$userProvider->sort->sortParam = ’user-sort’;
$postProvider->pagination->pageParam = ’post-page’;
$postProvider->sort->sortParam = ’post-sort’;
echo ’<h1>Users</h1>’;
echo GridView::widget([
’dataProvider’ => $userProvider,
]);
8.6. WORKING WITH CLIENT SCRIPTS 401
echo ’<h1>Posts</h1>’;
echo GridView::widget([
’dataProvider’ => $postProvider,
]);
Pjax::begin([
// PJax options
]);
Gridview::widget([
// GridView options
]);
Pjax::end();
Pjax also works for the links inside the Pjax widget and for the links specified
by Pjax::$linkSelector. But this might be a problem for the links of an
ActionColumn. To prevent this, add the HTML attribute data-pjax="0" to
the links when you edit the ActionColumn::$buttons property.
AJAX. This section describes methods provided by Yii for adding JavaScript
and CSS to a website as well as dynamically adjusting these.
The first argument is the actual JS code we want to insert into the page.
It will be wrapped into a <script> tag. The second argument determines at
which position the script should be inserted into the page. Possible values
are:
• View::POS_HEAD for head section.
• View::POS_BEGIN for right after opening <body>.
• View::POS_END for right before closing </body>.
• View::POS_READY for executing code on the document ready event9 .
This will automatically register jQuery and wrap the code into the
appropriate jQuery code. This is the default position.
• View::POS_LOAD for executing code on the document load event10 .
Same as the above, this will also register jQuery automatically.
The last argument is a unique script ID that is used to identify the script
code block and replace an existing one with the same ID instead of adding
a new one. If you don’t provide it, the JS code itself will be used as the ID.
It is used to avoid registration of the same code muliple times.
This will add a tag for the /js/main.js script located under the application
base URL.
It is highly recommended to use asset bundles to register external JS files
rather than registerJsFile() because these allow better flexibility and
more granular dependency configuration. Also using asset bundles allows
you to combine and compress multiple JS files, which is desirable for high
traffic websites.
The code above will result in adding the following to the <head> section of
the page:
<style>
body { background: #f00; }
</style>
If you want to specify additional properties of the style tag, pass an array
of name-values to the second argument. The last argument is a unique ID
that is used to identify the style block and make sure it is only added once
in case the same style is registered from different places in the code.
Registering CSS files
A CSS file can be registered using the following:
$this->registerCssFile("@web/css/themes/black-and-white.css", [
’depends’ => [\yii\bootstrap\BootstrapAsset::className()],
’media’ => ’print’,
], ’css-print-theme’);
• The first argument specifies the CSS file to be registered. The @web in
this example is an alias for the applications base URL.
• The second argument specifies the HTML attributes for the result-
ing <link> tag. The option depends is specially handled. It specifies
which asset bundles this CSS file depends on. In this case, the de-
pendent asset bundle is yii\bootstrap\BootstrapAsset. This means
the CSS file will be added after the CSS files from yii\bootstrap
\BootstrapAsset.
• The last argument specifies an ID identifying this CSS file. If it is not
provided, the URL of the CSS file will be used instead.
It is highly recommended to use asset bundles to register external CSS files
rather than registerCssFile(). Using asset bundles allows you to combine
and compress multiple CSS files, which is desirable for high traffic websites.
It also provides more flexibility as all asset dependencies of your application
are configured in one place.
In the above code, in the context of a view file, the AppAsset bundle is re-
gistered on the current view (represented by $this). When registering asset
bundles from within a widget, you would pass the $view of the widget instead
($this->view).
$options = [
’appName’ => Yii::$app->name,
’baseUrl’ => Yii::$app->request->baseUrl,
’language’ => Yii::$app->language,
// ...
];
$this->registerJs(
"var yiiOptions = ".\yii\helpers\Json::htmlEncode($options).";",
View::POS_HEAD,
’yiiOptions’
);
The above code will register a <script>-tag containing the JavaScript variable
definition, e.g.:
var yiiOptions = {"appName":"My Yii Application","baseUrl":"/basic/web","
language":"en"};
In your JavaScript code you can now access these like yiiOptions.baseUrl or
yiiOptions.language.
The above example code uses PHP Heredoc syntax11 for better readabil-
ity. This also enables better syntax highlighting in most IDEs so it is the
preferred way of writing inline JavaScript, especially useful for code that is
longer than a single line. The variable $message is created in PHP and thanks
to Json::htmlEncode it contains the string in valid JS syntax, which can be
inserted into the JavaScript code to place the dynamic string in the function
call to alert().
8.7 Theming
Theming is a way to replace a set of views with another without the need of
touching the original view rendering code. You can use theming to system-
atically change the look and feel of an application.
To use theming, you should configure the theme property of the view
application component. The property configures a yii\base\Theme object
which governs how view files are being replaced. You should mainly specify
the following properties of yii\base\Theme:
• yii\base\Theme::$basePath: specifies the base directory that con-
tains the themed resources (CSS, JS, images, etc.)
• yii\base\Theme::$baseUrl: specifies the base URL of the themed
resources.
• yii\base\Theme::$pathMap: specifies the replacement rules of view
files. More details will be given in the following subsections.
For example, if you call $this->render(’about’) in SiteController, you will
be rendering the view file @app/views/site/about.php. However, if you enable
theming in the following application configuration, the view file @app/themes
/basic/site/about.php will be rendered, instead.
return [
’components’ => [
’view’ => [
’theme’ => [
’basePath’ => ’@app/themes/basic’,
’baseUrl’ => ’@web/themes/basic’,
’pathMap’ => [
’@app/views’ => ’@app/themes/basic’,
],
],
],
8.7. THEMING 407
],
];
Security
9.1 Security
Good security is vital to the health and success of any application. Unfortu-
nately, many developers cut corners when it comes to security, either due to
a lack of understanding or because implementation is too much of a hurdle.
To make your Yii powered application as secure as possible, Yii has included
several excellent and easy to use security features.
• Authentication
• Authorization
• Working with Passwords
• Cryptography
• Views security
• Auth Clients1
• Best Practices
• Trusted proxies and headers
9.2 Authentication
Authentication is the process of verifying the identity of a user. It usually
uses an identifier (e.g. a username or an email address) and a secret token
(e.g. a password or an access token) to judge if the user is the one whom he
claims as. Authentication is the basis of the login feature.
Yii provides an authentication framework which wires up various com-
ponents to support login. To use this framework, you mainly need to do the
following work:
• Configure the user application component;
• Create a class that implements the yii\web\IdentityInterface in-
terface.
1
https://github.com/yiisoft/yii2-authclient/blob/master/docs/guide/
README.md
409
410 CHAPTER 9. SECURITY
use yii\db\ActiveRecord;
use yii\web\IdentityInterface;
/**
* Finds an identity by the given ID.
*
* @param string|int $id the ID to be looked for
* @return IdentityInterface|null the identity object that matches the
given ID.
*/
public static function findIdentity($id)
{
return static::findOne($id);
}
/**
* Finds an identity by the given token.
*
* @param string $token the token to be looked for
* @return IdentityInterface|null the identity object that matches the
given token.
*/
public static function findIdentityByAccessToken($token, $type = null)
{
return static::findOne([’access_token’ => $token]);
}
/**
* @return int|string current user ID
*/
public function getId()
{
return $this->id;
}
/**
* @return string current user auth key
*/
public function getAuthKey()
{
return $this->auth_key;
}
/**
* @param string $authKey
* @return bool if auth key is valid for current user
*/
public function validateAuthKey($authKey)
{
return $this->getAuthKey() === $authKey;
}
}
412 CHAPTER 9. SECURITY
Note that logging out a user is only meaningful when session is enabled.
The method will clean up the user authentication status from both memory
and session. And by default, it will also destroy all user session data. If you
want to keep the session data, you should call Yii::$app->user->logout(false),
instead.
9.3 Authorization
Authorization is the process of verifying that a user has enough permission
to do something. Yii provides two authorization methods: Access Control
Filter (ACF) and Role-Based Access Control (RBAC).
414 CHAPTER 9. SECURITY
In the code above ACF is attached to the site controller as a behavior. This
is the typical way of using an action filter. The only option specifies that
the ACF should only be applied to the login, logout and signup actions. All
other actions in the site controller are not subject to the access control. The
rules option lists the access rules, which reads as follows:
• Allow all guest (not yet authenticated) users to access the login and
signup actions. The roles option contains a question mark ? which is a
special token representing “guest users”.
• Allow authenticated users to access the logout action. The @ character
is another special token representing “authenticated users”.
ACF performs the authorization check by examining the access rules one
by one from top to bottom until it finds a rule that matches the current
9.3. AUTHORIZATION 415
execution context. The allow value of the matching rule will then be used to
judge if the user is authorized or not. If none of the rules matches, it means
the user is NOT authorized, and ACF will stop further action execution.
When ACF determines a user is not authorized to access the current
action, it takes the following measure by default:
• If the user is a guest, it will call yii\web\User::loginRequired() to
redirect the user browser to the login page.
• If the user is already authenticated, it will throw a yii\web\ForbiddenHttpException.
You may customize this behavior by configuring the yii\filters\AccessControl
::$denyCallback property like the following:
[
’class’ => AccessControl::className(),
...
’denyCallback’ => function ($rule, $action) {
throw new \Exception(’You are not allowed to access this page’);
}
]
// Match callback called! This page can be accessed only each October 31
st
public function actionSpecialCallback()
{
return $this->render(’happy-halloween’);
}
}
Basic Concepts
A role represents a collection of permissions (e.g. creating posts, updating
posts). A role may be assigned to one or multiple users. To check if a user
has a specified permission, we may check if the user is assigned with a role
that contains that permission.
Associated with each role or permission, there may be a rule. A rule
represents a piece of code that will be executed during access check to de-
termine if the corresponding role or permission applies to the current user.
For example, the “update post” permission may have a rule that checks if the
current user is the post creator. During access checking, if the user is NOT
the post creator, he/she will be considered not having the “update post”
permission.
Both roles and permissions can be organized in a hierarchy. In particular,
a role may consist of other roles or permissions; and a permission may consist
of other permissions. Yii implements a partial order hierarchy which includes
the more special tree hierarchy. While a role can contain a permission, it is
not true vice versa.
Configuring RBAC
Before we set off to define authorization data and perform access checking,
we need to configure the authManager application component. Yii provides
two types of authorization managers: yii\rbac\PhpManager and yii\rbac
\DbManager. The former uses a PHP script file to store authorization data,
while the latter stores authorization data in a database. You may consider
using the former if your application does not require very dynamic role and
permission management.
Using PhpManager The following code shows how to configure the authManager
in the application configuration using the yii\rbac\PhpManager class:
return [
// ...
’components’ => [
’authManager’ => [
3
http://csrc.nist.gov/rbac/sandhu-ferraiolo-kuhn-00.pdf
418 CHAPTER 9. SECURITY
Using DbManager The following code shows how to configure the authManager
in the application configuration using the yii\rbac\DbManager class:
return [
// ...
’components’ => [
’authManager’ => [
’class’ => ’yii\rbac\DbManager’,
// uncomment if you want to cache RBAC items hierarchy
// ’cache’ => ’cache’,
],
// ...
],
];
Either way in the end you’ll get the following RBAC hierarchy:
420 CHAPTER 9. SECURITY
won’t be different.
Using migrations You can use migrations to initialize and modify hier-
archy via APIs offered by authManager.
Create new migration using ./yii migrate/create init_rbac then impe-
ment creating a hierarchy:
<?php
use yii\db\Migration;
// add "author" role and give this role the "createPost" permission
$author = $auth->createRole(’author’);
$auth->add($author);
$auth->addChild($author, $createPost);
// add "admin" role and give this role the "updatePost" permission
// as well as the permissions of the "author" role
$admin = $auth->createRole(’admin’);
$auth->add($admin);
$auth->addChild($admin, $updatePost);
$auth->addChild($admin, $author);
$auth->removeAll();
}
}
422 CHAPTER 9. SECURITY
use Yii;
use yii\console\Controller;
// add "author" role and give this role the "createPost" permission
$author = $auth->createRole(’author’);
$auth->add($author);
$auth->addChild($author, $createPost);
// add "admin" role and give this role the "updatePost" permission
// as well as the permissions of the "author" role
$admin = $auth->createRole(’admin’);
$auth->add($admin);
$auth->addChild($admin, $updatePost);
$auth->addChild($admin, $author);
Note: If you are using advanced template, you need to put your
RbacController inside console/controllers directory and change
namespace to console\controllers.
The command above could be executed from console the following way:
yii rbac/init
If you don’t want to hardcode what users have certain roles, don’t
put ->assign() calls into the command. Instead, create either UI
or console command to manage assignments.
return $user;
}
return null;
}
For applications that require complex access control with dynamically up-
dated authorization data, special user interfaces (i.e. admin panel) may need
to be developed using APIs offered by authManager.
424 CHAPTER 9. SECURITY
Using Rules
As aforementioned, rules add additional constraint to roles and permissions.
A rule is a class extending from yii\rbac\Rule. It must implement the
execute() method. In the hierarchy we’ve created previously author cannot
edit his own post. Let’s fix it. First we need a rule to verify that the user is
the post author:
namespace app\rbac;
use yii\rbac\Rule;
use app\models\Post;
/**
* Checks if authorID matches user passed via params
*/
class AuthorRule extends Rule
{
public $name = ’isAuthor’;
/**
* @param string|int $user the user ID.
* @param Item $item the role or permission that this rule is associated
with
* @param array $params parameters passed to ManagerInterface::
checkAccess().
* @return bool a value indicating whether the rule permits the role or
permission it is associated with.
*/
public function execute($user, $item, $params)
{
return isset($params[’post’]) ? $params[’post’]->createdBy == $user
: false;
}
}
The rule above checks if the post is created by $user. We’ll create a special
permission updateOwnPost in the command we’ve used previously:
$auth = Yii::$app->authManager;
// add the "updateOwnPost" permission and associate the rule with it.
$updateOwnPost = $auth->createPermission(’updateOwnPost’);
$updateOwnPost->description = ’Update own post’;
$updateOwnPost->ruleName = $rule->name;
$auth->add($updateOwnPost);
Access Check
If the current user is Jane with ID=1 we are starting at createPost and trying
to get to Jane:
426 CHAPTER 9. SECURITY
],
[
’allow’ => true,
’actions’ => [’create’],
’roles’ => [’createPost’],
],
[
’allow’ => true,
’actions’ => [’update’],
’roles’ => [’updatePost’],
],
[
’allow’ => true,
’actions’ => [’delete’],
’roles’ => [’deletePost’],
],
],
],
];
}
If all the CRUD operations are managed together then it’s a good idea to
use a single permission, like managePost, and check it in yii\web\Controller
::beforeAction().
In the above example, no parameters are passed with the roles specified
for accessing an action, but in case of the updatePost permission, we need to
pass a post parameter for it to work properly. You can pass parameters to
yii\web\User::can() by specifying roleParams on the access rule:
[
’allow’ => true,
’actions’ => [’update’],
’roles’ => [’updatePost’],
’roleParams’ => function() {
return [’post’ => Post::findOne([’id’ => Yii::$app->request->get(’id
’)])];
},
],
A default role is a role that is implicitly assigned to all users. The call to yii
\rbac\ManagerInterface::assign() is not needed, and the authorization
data does not contain its assignment information.
A default role is usually associated with a rule which determines if the
role applies to the user being checked.
Default roles are often used in applications which already have some sort
of role assignment. For example, an application may have a “group” column
in its user table to represent which privilege group each user belongs to.
If each privilege group can be mapped to an RBAC role, you can use the
default role feature to automatically assign each user to an RBAC role. Let’s
use an example to show how this can be done.
Assume in the user table, you have a group column which uses 1 to rep-
resent the administrator group and 2 the author group. You plan to have
two RBAC roles admin and author to represent the permissions for these two
groups, respectively. You can set up the RBAC data as follows,
namespace app\rbac;
use Yii;
use yii\rbac\Rule;
/**
* Checks if user group matches
*/
class UserGroupRule extends Rule
{
public $name = ’userGroup’;
$auth = Yii::$app->authManager;
$author = $auth->createRole(’author’);
$author->ruleName = $rule->name;
9.4. WORKING WITH PASSWORDS 431
$auth->add($author);
// ... add permissions as children of $author ...
$admin = $auth->createRole(’admin’);
$admin->ruleName = $rule->name;
$auth->add($admin);
$auth->addChild($admin, $author);
// ... add permissions as children of $admin ...
Note that in the above, because “author” is added as a child of “admin”, when
you implement the execute() method of the rule class, you need to respect
this hierarchy as well. That is why when the role name is “author”, the
execute() method will return true if the user group is either 1 or 2 (meaning
the user is in either “admin” group or “author” group).
Next, configure authManager by listing the two roles in yii\rbac\BaseManager
::$defaultRoles:
return [
// ...
’components’ => [
’authManager’ => [
’class’ => ’yii\rbac\PhpManager’,
’defaultRoles’ => [’admin’, ’author’],
],
// ...
],
];
Now if you perform an access check, both of the admin and author roles will
be checked by evaluating the rules associated with them. If the rule returns
true, it means the role applies to the current user. Based on the above rule
implementation, this means if the group value of a user is 1, the admin role
would apply to the user; and if the group value is 2, the author role would
apply.
provides two helper functions which make using crypt to securely generate
and verify hashes easier.
When a user provides a password for the first time (e.g., upon registra-
tion), the password needs to be hashed:
$hash = Yii::$app->getSecurity()->generatePasswordHash($password);
The hash can then be associated with the corresponding model attribute, so
it can be stored in the database for later use.
When a user attempts to log in, the submitted password must be verified
against the previously hashed and stored password:
if (Yii::$app->getSecurity()->validatePassword($password, $hash)) {
// all good, logging user in
} else {
// wrong password
}
9.5 Cryptography
In this section we’ll review the following security aspects:
• Generating random data
• Encryption and Decryption
• Confirming Data Integrity
1. Filter input.
2. Escape output.
Filter input
Filter input means that input should never be considered safe and you should
always check if the value you’ve got is actually among allowed ones. For ex-
ample, if we know that sorting could be done by three fields title, created_at
and status and the field could be supplied via user input, it’s better to check
the value we’ve got right where we’re receiving it. In terms of basic PHP
that would look like the following:
$sortBy = $_GET[’sort’];
if (!in_array($sortBy, [’title’, ’created_at’, ’status’])) {
throw new Exception(’Invalid sort value.’);
}
Escape output
Escape output means that depending on context where we’re using data it
should be escaped i.e. in context of HTML you should escape <, > and alike
special characters. In context of JavaScript or SQL it will be different set of
characters. Since it’s error-prone to escape everything manually Yii provides
various tools to perform escaping for different contexts.
Further reading on the topic:
• https://www.owasp.org/index.php/Command_Injection
• https://www.owasp.org/index.php/Code_Injection
• https://www.owasp.org/index.php/Cross-site_Scripting_%28XSS%
29
436 CHAPTER 9. SECURITY
This is valid query that will search for users with empty username and then
will drop user table most probably resulting in broken website and data loss
(you’ve set up regular backups, right?).
In Yii most of database querying happens via Active Record which prop-
erly uses PDO prepared statements internally. In case of prepared statements
it’s not possible to manipulate query as was demonstrated above.
Still, sometimes you need raw queries or query builder. In this case you
should use safe ways of passing data. If data is used for column values it’s
preferred to use prepared statements:
// query builder
$userIDs = (new Query())
->select(’id’)
->from(’user’)
->where(’status=:status’, [’:status’ => $status])
->all();
// DAO
$userIDs = $connection
->createCommand(’SELECT id FROM user where status=:status’)
->bindValues([’:status’ => $status])
->queryColumn();
If data is used to specify column names or table names the best thing to do
is to allow only predefined set of values:
function actionList($orderBy = null)
{
if (!in_array($orderBy, [’name’, ’status’])) {
throw new BadRequestHttpException(’Only name and status are allowed
to order by.’)
}
// ...
}
In case it’s not possible, table and column names should be escaped. Yii has
special syntax for such escaping which allows doing it the same way for all
databases it supports:
$sql = "SELECT COUNT([[$column]]) FROM {{table}}";
$rowCount = $connection->createCommand($sql)->queryScalar();
9.6. SECURITY BEST PRACTICES 437
You can get details about the syntax in Quoting Table and Column Names.
Further reading on the topic:
• https://www.owasp.org/index.php/SQL_Injection
send GET request to load an image, so we can modify code to accept only
POST requests on that URL. Unfortunately, this will not save us, because
an attacker can put some JavaScript code instead of <img> tag, which allows
to send POST requests on that URL.
In order to avoid CSRF you should always:
Sometimes you need to disable CSRF validation per controller and/or action.
It could be achieved by setting its property:
namespace app\controllers;
use yii\web\Controller;
use yii\web\Controller;
namespace app\components;
use yii\base\Action;
Warning: Disabling CSRF will allow any site to send POST re-
quests to your site. It is important to implement extra validation
such as checking an IP address or a secret token in this case.
You should never enable Gii or the Debug toolbar in production. It could
be used to get information about database structure, code and to simply
rewrite code with what’s generated by Gii.
Debug toolbar should be avoided at production unless really necessary.
It exposes all the application and config details possible. If you absolutely
need it check twice that access is properly restricted to your IP only.
Further reading on the topic:
• https://www.owasp.org/index.php/Exception_Handling
• https://www.owasp.org/index.php/Top_10_2007-Information_Leakage
filter the value by setting the hostInfo property of the request application
component.
For more information about the server configuration, please refer to the
documentation of your webserver:
• Apache 2: http://httpd.apache.org/docs/trunk/vhosts/examples.
html#defaultallports
• Nginx: https://www.nginx.com/resources/wiki/start/topics/examples/
server_blocks/
If you don’t have access to the server configuration, you can setup yii
\filters\HostControl filter at application level in order to protect against
such kind of attack:
// Web Application configuration file
return [
’as hostControl’ => [
’class’ => ’yii\filters\HostControl’,
’allowedHosts’ => [
’example.com’,
’*.example.com’,
],
’fallbackHostInfo’ => ’https://example.com’,
],
// ...
];
Caching
10.1 Caching
Caching is a cheap and effective way to improve the performance of a Web
application. By storing relatively static data in cache and serving it from
cache when requested, the application saves the time that would be required
to generate the data from scratch every time.
Caching can occur at different levels and places in a Web application.
On the server-side, at the lower level, cache may be used to store basic data,
such as a list of most recent article information fetched from database; and
at the higher level, cache may be used to store fragments or whole of Web
pages, such as the rendering result of the most recent articles. On the client-
side, HTTP caching may be used to keep most recently visited page content
in the browser cache.
Yii supports all these caching mechanisms:
• Data caching
• Fragment caching
• Page caching
• HTTP caching
443
444 CHAPTER 10. CACHING
$data = $this->calculateSomething();
When cache has data associated with the $key, the cached value will be
returned. Otherwise, the passed anonymous function will be executed to
calculate the value that will be cached and returned.
If the anonymous function requires some data from the outer scope, you
can pass it with the use statement. For example:
$user_id = 42;
$data = $cache->getOrSet($key, function () use ($user_id) {
return $this->calculateSomething($user_id);
});
You can then access the above cache component using the expression Yii::
$app->cache.
Because all cache components support the same set of APIs, you can
swap the underlying cache component with a different one by reconfiguring
it in the application configuration without modifying the code that uses the
cache. For example, you can modify the above configuration to use APC
cache:
’components’ => [
’cache’ => [
’class’ => ’yii\caching\ApcCache’,
],
],
cache component. In both cases, you may use the same code Yii::$app
->cache->get($key) to attempt retrieving data from the cache without
worrying that Yii::$app->cache might be null.
• yii\caching\FileCache: uses standard files to store cached data.
This is particularly suitable to cache large chunk of data, such as page
content.
• yii\caching\MemCache: uses PHP memcache3 and memcached4 ex-
tensions. This option can be considered as the fastest one when dealing
with cache in a distributed applications (e.g. with several servers, load
balancers, etc.)
• yii\redis\Cache: implements a cache component based on Redis5
key-value store (redis version 2.6.12 or higher is required).
• yii\caching\WinCache: uses PHP WinCache6 (see also7 ) extension.
• yii\caching\XCache (deprecated): uses PHP XCache8 extension.
• yii\caching\ZendDataCache (deprecated): uses Zend Data Cache9 as
the underlying caching medium.
Tip: You may use different cache storage in the same applica-
tion. A common strategy is to use memory-based cache storage
to store data that is small but constantly used (e.g. statistical
data), and use file-based or database-based cache storage to store
data that is big and less frequently used (e.g. page content).
• multiGet(): retrieves multiple data items from cache with the spe-
cified keys.
• multiSet(): stores multiple data items in cache. Each item is identi-
fied by a key.
• multiAdd(): stores multiple data items in cache. Each item is identi-
fied by a key. If a key already exists in the cache, the data item will
be skipped.
• exists(): returns a value indicating whether the specified key is found
in the cache.
• delete(): removes a data item identified by a key from the cache.
• flush(): removes all data items from the cache.
Cache Keys
Each data item stored in cache is uniquely identified by a key. When you
store a data item in cache, you have to specify a key for it. Later when you
retrieve the data item from cache, you should provide the corresponding key.
You may use a string or an arbitrary value as a cache key. When a key
is not a string, it will be automatically serialized into a string.
A common strategy of defining a cache key is to include all determining
factors in terms of an array. For example, yii\db\Schema uses the following
key to cache schema information about a database table:
[
__CLASS__, // schema class name
$this->db->dsn, // DB connection data source name
$this->db->username, // DB connection login user
$name, // table name
];
As you can see, the key includes all necessary information needed to uniquely
specify a database table.
448 CHAPTER 10. CACHING
When the same cache storage is used by different applications, you should
specify a unique cache key prefix for each application to avoid conflicts of
cache keys. This can be done by configuring the yii\caching\Cache::
$keyPrefix property. For example, in the application configuration you can
write the following code:
’components’ => [
’cache’ => [
’class’ => ’yii\caching\ApcCache’,
’keyPrefix’ => ’myapp’, // a unique cache key prefix
],
],
Cache Expiration
A data item stored in a cache will remain there forever unless it is removed
because of some caching policy enforcement (e.g. caching space is full and
the oldest data are removed). To change this behavior, you can provide an
expiration parameter when calling set() to store a data item. The parameter
indicates for how many seconds the data item can remain valid in the cache.
When you call get() to retrieve the data item, if it has passed the expiration
time, the method will return false, indicating the data item is not found in
the cache. For example,
// keep the data in cache for at most 45 seconds
$cache->set($key, $data, 45);
sleep(50);
$data = $cache->get($key);
if ($data === false) {
// $data is expired or is not found in the cache
}
Since 2.0.11 you may set defaultDuration value in your cache component
configuration if you prefer a custom cache duration over the default unlimited
duration. This will allow you not to pass custom duration parameter to set()
each time.
Cache Dependencies
Besides expiration setting, cached data item may also be invalidated by
changes of the so-called cache dependencies. For example, yii\caching
\FileDependency represents the dependency of a file’s modification time.
10.2. DATA CACHING 449
// the result of the SQL query will be served from the cache
450 CHAPTER 10. CACHING
});
Configurations
Usages
return $result;
}, $duration, $dependency);
Any SQL queries in the anonymous function will be cached for the specified
duration with the specified dependency. If the result of a query is found
valid in the cache, the query will be skipped and the result will be served
from the cache instead. If you do not specify the $duration parameter, the
value of queryCacheDuration will be used instead.
Sometimes within cache(), you may want to disable query caching for
some particular queries. You can use yii\db\Connection::noCache() in
this case.
$result = $db->cache(function ($db) {
$db->noCache(function ($db) {
});
// ...
return $result;
});
If you just want to use query caching for a single query, you can call yii\db
\Command::cache() when building the command. For example,
// use query caching and set query cache duration to be 60 seconds
$customer = $db->createCommand(’SELECT * FROM customer WHERE id=1’)->cache
(60)->queryOne();
// ...
452 CHAPTER 10. CACHING
return $result;
});
Limitations
Query caching does not work with query results that contain resource hand-
lers. For example, when using the BLOB column type in some DBMS, the
query result will return a resource handler for the column data.
Some caching storage has size limitation. For example, memcache limits
the maximum size of each entry to be 1MB. Therefore, if the size of a query
result exceeds this limit, the caching will fail.
$this->endCache();
}
10.3. FRAGMENT CACHING 453
Duration
Perhaps the most commonly used option of fragment caching is duration.
It specifies for how many seconds the content can remain valid in a cache.
The following code caches the content fragment for at most one hour:
if ($this->beginCache($id, [’duration’ => 3600])) {
$this->endCache();
}
If the option is not set, it will take the default value 60, which means the
cached content will expire in 60 seconds.
Dependencies
Like data caching, content fragment being cached can also have dependen-
cies. For example, the content of a post being displayed depends on whether
or not the post is modified.
To specify a dependency, set the dependency option, which can be either
an yii\caching\Dependency object or a configuration array for creating a
dependency object. The following code specifies that the fragment content
depends on the change of the updated_at column value:
$dependency = [
’class’ => ’yii\caching\DbDependency’,
’sql’ => ’SELECT MAX(updated_at) FROM post’,
];
$this->endCache();
}
Variations
Content being cached may be variated according to some parameters. For
example, for a Web application supporting multiple languages, the same
piece of view code may generate the content in different languages. There-
fore, you may want to make the cached content variated according to the
current application language.
To specify cache variations, set the variations option, which should be
an array of scalar values, each representing a particular variation factor. For
example, to make the cached content variated by the language, you may use
the following code:
if ($this->beginCache($id, [’variations’ => [Yii::$app->language]])) {
$this->endCache();
}
Toggling Caching
Sometimes you may want to enable fragment caching only when certain
conditions are met. For example, for a page displaying a form, you only
want to cache the form when it is initially requested (via GET request).
Any subsequent display (via POST request) of the form should not be cached
because the form may contain user input. To do so, you may set the enabled
option, like the following:
if ($this->beginCache($id, [’enabled’ => Yii::$app->request->isGet])) {
$this->endCache();
}
if ($this->beginCache($id1)) {
if ($this->beginCache($id2, $options2)) {
$this->endCache();
}
$this->endCache();
}
Different caching options can be set for the nested caches. For example, the
inner caches and the outer caches can use different cache duration values.
Even when the data cached in the outer cache is invalidated, the inner cache
may still provide the valid inner fragment. However, it is not true vice versa.
If the outer cache is evaluated to be valid, it will continue to provide the same
cached copy even after the content in the inner cache has been invalidated.
Therefore, you must be careful in setting the durations or the dependencies
of the nested caches, otherwise the outdated inner fragments may be kept in
the outer fragment.
$this->endCache();
}
The above code states that page caching should be used only for the index
action. The page content should be cached for at most 60 seconds and should
be variated by the current application language and the cached page should
be invalidated if the total number of posts is changed.
As you can see, page caching is very similar to fragment caching. They
both support options such as duration, dependencies, variations, and enabled.
Their main difference is that page caching is implemented as an action filter
while fragment caching a widget.
10.5. HTTP CACHING 457
You can use fragment caching as well as dynamic content together with
page caching.
The above code states that HTTP caching should be enabled for the index
action only. It should generate a Last-Modified HTTP header based on the
458 CHAPTER 10. CACHING
last update time of posts. When a browser visits the index page for the first
time, the page will be generated on the server and sent to the browser; If the
browser visits the same page again and there is no post being modified during
the period, the server will not re-generate the page, and the browser will use
the cached version on the client-side. As a result, server-side rendering and
page content transmission are both skipped.
The above code states that HTTP caching should be enabled for the view
action only. It should generate an ETag HTTP header based on the title and
content of the requested post. When a browser visits the view page for the
first time, the page will be generated on the server and sent to the browser;
If the browser visits the same page again and there is no change to the title
and content of the post, the server will not re-generate the page, and the
browser will use the cached version on the client-side. As a result, server-side
rendering and page content transmission are both skipped.
10.5. HTTP CACHING 459
ETags allow more complex and/or more precise caching strategies than
Last-Modified headers. For instance, an ETag can be invalidated if the site
has switched to another theme.
Expensive ETag generation may defeat the purpose of using HttpCache
and introduce unnecessary overhead, since they need to be re-evaluated on
every request. Try to find a simple expression that invalidates the cache if
the page content has been modified.
11
http://tools.ietf.org/html/rfc7232#section-2.4
12
http://www.php.net/manual/en/function.session-cache-limiter.php
460 CHAPTER 10. CACHING
Chapter 11
use yii\rest\ActiveController;
1
http://en.wikipedia.org/wiki/HATEOAS
461
462 CHAPTER 11. RESTFUL WEB SERVICES
The above configuration mainly adds a URL rule for the user controller so
that the user data can be accessed and manipulated with pretty URLs and
meaningful HTTP verbs.
HTTP/1.1 200 OK
...
X-Pagination-Total-Count: 1000
X-Pagination-Page-Count: 50
X-Pagination-Current-Page: 1
X-Pagination-Per-Page: 20
Link: <http://localhost/users?page=1>; rel=self,
<http://localhost/users?page=2>; rel=next,
<http://localhost/users?page=50>; rel=last
Transfer-Encoding: chunked
Content-Type: application/json; charset=UTF-8
[
{
"id": 1,
...
},
{
"id": 2,
...
},
...
]
Try changing the acceptable content type to be application/xml, and you will
see the result is returned in XML format:
$ curl -i -H "Accept:application/xml" "http://localhost/users"
HTTP/1.1 200 OK
...
X-Pagination-Total-Count: 1000
X-Pagination-Page-Count: 50
464 CHAPTER 11. RESTFUL WEB SERVICES
X-Pagination-Current-Page: 1
X-Pagination-Per-Page: 20
Link: <http://localhost/users?page=1>; rel=self,
<http://localhost/users?page=2>; rel=next,
<http://localhost/users?page=50>; rel=last
Transfer-Encoding: chunked
Content-Type: application/xml
The following command will create a new user by sending a POST request
with the user data in JSON format:
$ curl -i -H "Accept:application/json" -H "Content-Type:application/json" -
XPOST "http://localhost/users" -d ’{"username": "example", "email": "
[email protected]"}’
{"id":1,"username":"example","email":"[email protected]","created_at
":1414674789,"updated_at":1414674789}
Tip: You may also access your APIs via Web browser by entering
the URL http://localhost/users. However, you may need some
browser plugins to send specific request headers.
As you can see, in the response headers, there is information about the total
count, page count, etc. There are also links that allow you to navigate to
other pages of data. For example, http://localhost/users?page=2 would give
you the next page of the user data.
Using the fields and expand parameters, you may also specify which fields
should be included in the result. For example, the URL http://localhost/
users?fields=id,email will only return the id and email fields.
You can and should remove these fields from result as described
in the Resources section.
11.1.5 Summary
Using the Yii RESTful API framework, you implement an API endpoint in
terms of a controller action, and you use a controller to organize the actions
that implement the endpoints for a single type of resource.
Resources are represented as data models which extend from the yii
\base\Model class. If you are working with databases (relational or NoSQL),
it is recommended you use ActiveRecord to represent resources.
You may use yii\rest\UrlRule to simplify the routing to your API
endpoints.
While not required, it is recommended that you develop your RESTful
APIs as a separate application, different from your Web front end and back
end for easier maintenance.
11.2 Resources
RESTful APIs are all about accessing and manipulating resources. You may
view resources as models in the MVC paradigm.
While there is no restriction in how to represent a resource, in Yii you
usually would represent resources in terms of objects of yii\base\Model or
its child classes (e.g. yii\db\ActiveRecord), for the following reasons:
• yii\base\Model implements the yii\base\Arrayable interface, which
allows you to customize how you want to expose resource data through
RESTful APIs.
• yii\base\Model supports input validation, which is useful if your
RESTful APIs need to support data input.
• yii\db\ActiveRecord provides powerful DB data access and manip-
ulation support, which makes it a perfect fit if your resource data is
stored in databases.
In this section, we will mainly describe how a resource class extending from
yii\base\Model (or its child classes) can specify what data may be returned
via RESTful APIs. If the resource class does not extend from yii\base
\Model, then all its public member variables will be returned.
466 CHAPTER 11. RESTFUL WEB SERVICES
11.2.1 Fields
When including a resource in a RESTful API response, the resource needs
to be serialized into a string. Yii breaks this process into two steps. First,
the resource is converted into an array by yii\rest\Serializer. Second,
the array is serialized into a string in a requested format (e.g. JSON, XML)
by response formatters. The first step is what you should mainly focus
when developing a resource class.
By overriding fields() and/or extraFields(), you may specify what
data, called fields, in the resource can be put into its array representation.
The difference between these two methods is that the former specifies the
default set of fields which should be included in the array representation,
while the latter specifies additional fields which may be included in the array
if an end user requests for them via the expand query parameter. For example,
// returns all fields as declared in fields()
http://localhost/users
// only returns "id" and "email" fields, provided they are declared in
fields()
http://localhost/users?fields=id,email
// only returns "id" and "email" provided they are in fields() and "profile"
if it is in extraFields()
http://localhost/users?fields=id,email&expand=profile
Overriding fields()
By default, yii\base\Model::fields() returns all model attributes as fields,
while yii\db\ActiveRecord::fields() only returns the attributes which
have been populated from DB.
You can override fields() to add, remove, rename or redefine fields. The
return value of fields() should be an array. The array keys are the field
names, and the array values are the corresponding field definitions which
can be either property/attribute names or anonymous functions returning
the corresponding field values. In the special case when a field name is the
same as its defining attribute name, you can omit the array key. For example,
// explicitly list every field, best used when you want to make sure the
changes
// in your DB table or model attributes do not cause your field changes (to
keep API backward compatibility).
11.2. RESOURCES 467
// filter out some fields, best used when you want to inherit the parent
implementation
// and blacklist some sensitive fields.
public function fields()
{
$fields = parent::fields();
return $fields;
}
Overriding extraFields()
By default, yii\base\Model::extraFields() returns an empty array, while
yii\db\ActiveRecord::extraFields() returns the names of the relations
that have been populated from DB.
The return data format of extraFields() is the same as that of fields
(). Usually, extraFields() is mainly used to specify fields whose values are
objects. For example, given the following field declaration,
public function fields()
{
return [’id’, ’email’];
}
{
return [’profile’];
}
11.2.2 Links
HATEOAS2 , an abbreviation for Hypermedia as the Engine of Application
State, promotes that RESTful APIs should return information that allows
clients to discover actions supported for the returned resources. The key of
HATEOAS is to return a set of hyperlinks with relation information when
resource data are served by the APIs.
Your resource classes may support HATEOAS by implementing the yii
\web\Linkable interface. The interface contains a single method getLinks()
which should return a list of links. Typically, you should return at least the
self link representing the URL to the resource object itself. For example,
use yii\base\Model;
use yii\web\Link; // represents a link object as defined in JSON Hypermedia
API Language.
use yii\web\Linkable;
use yii\helpers\Url;
//...
{
return [’profile’];
}
11.2.3 Collections
Resource objects can be grouped into collections. Each collection contains a
list of resource objects of the same type.
While collections can be represented as arrays, it is usually more desirable
to represent them as data providers. This is because data providers support
sorting and pagination of resources, which is a commonly needed feature
for RESTful APIs returning collections. For example, the following action
returns a data provider about the post resources:
namespace app\controllers;
470 CHAPTER 11. RESTFUL WEB SERVICES
use yii\rest\Controller;
use yii\data\ActiveDataProvider;
use app\models\Post;
Filtering collections
Since version 2.0.13 Yii provides a facility to filter collections. An example
can be found in the Quick Start guide. In case you’re implementing an
endpoint yourself, filtering could be done as described in Filtering Data
Providers using Data Filters section of Data Providers guide.
11.3 Controllers
After creating the resource classes and specifying how resource data should
be formatted, the next thing to do is to create controller actions to expose
the resources to end users through RESTful APIs.
Yii provides two base controller classes to simplify your work of creating
RESTful actions: yii\rest\Controller and yii\rest\ActiveController.
The difference between these two controllers is that the latter provides a
default set of actions that are specifically designed to deal with resources
represented as Active Record. So if you are using Active Record and are
comfortable with the provided built-in actions, you may consider extending
11.3. CONTROLLERS 471
11.3.2 Filters
Most RESTful API features provided by yii\rest\Controller are imple-
mented in terms of filters. In particular, the following filters will be executed
in the order they are listed:
• contentNegotiator: supports content negotiation, to be explained in
the Response Formatting section;
• verbFilter: supports HTTP method validation;
• authenticator: supports user authentication, to be explained in the
Authentication section;
• rateLimiter: supports rate limiting, to be explained in the Rate Lim-
iting section.
These named filters are declared in the behaviors() method. You may
override this method to configure individual filters, disable some of them,
472 CHAPTER 11. RESTFUL WEB SERVICES
or add your own filters. For example, if you only want to use HTTP basic
authentication, you may write the following code:
use yii\filters\auth\HttpBasicAuth;
CORS
Adding the Cross-Origin Resource Sharing filter to a controller is a bit more
complicated than adding other filters described above, because the CORS
filter has to be applied before authentication methods and thus needs a
slightly different approach compared to other filters. Also authentication
has to be disabled for the CORS Preflight requests3 so that a browser can
safely determine whether a request can be made beforehand without the
need for sending authentication credentials. The following shows the code
that is needed to add the yii\filters\Cors filter to an existing controller
that extends from yii\rest\ActiveController:
use yii\filters\auth\HttpBasicAuth;
return $behaviors;
}
3
https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS#
Preflighted_requests
11.3. CONTROLLERS 473
Customizing Actions
By default, yii\rest\ActiveController provides the following actions:
• index: list resources page by page;
• view: return the details of a specified resource;
• create: create a new resource;
• update: update an existing resource;
• delete: delete the specified resource;
• options: return the supported HTTP methods.
All these actions are declared through the actions() method. You may
configure these actions or disable some of them by overriding the actions()
method, like shown the following,
public function actions()
{
$actions = parent::actions();
return $actions;
}
Please refer to the class references for individual action classes to learn what
configuration options are available.
/**
* Checks the privilege of the current user.
*
* This method should be overridden to check whether the current user has
the privilege
* to run the specified action against the specified data model.
* If the user does not have access, a [[ForbiddenHttpException]] should be
thrown.
*
* @param string $action the ID of the action to be executed
* @param \yii\base\Model $model the model to be accessed. If ‘null‘, it
means no specific model is being accessed.
* @param array $params additional parameters
* @throws ForbiddenHttpException if the user does not have access
*/
public function checkAccess($action, $model = null, $params = [])
{
// check if the user can access $action and $model
// throw ForbiddenHttpException if access should be denied
if ($action === ’update’ || $action === ’delete’) {
if ($model->author_id !== \Yii::$app->user->id)
throw new \yii\web\ForbiddenHttpException(sprintf(’You can only
%s articles that you\’ve created.’, $action));
}
}
11.4 Routing
With resource and controller classes ready, you can access the resources using
the URL like http://localhost/index.php?r=user/create, similar to what you
can do with normal Web applications.
In practice, you usually want to enable pretty URLs and take advantage
of HTTP verbs. For example, a request POST /users would mean accessing
the user/create action. This can be done easily by configuring the urlManager
application component in the application configuration like the following:
’urlManager’ => [
’enablePrettyUrl’ => true,
’enableStrictParsing’ => true,
’showScriptName’ => false,
’rules’ => [
[’class’ => ’yii\rest\UrlRule’, ’controller’ => ’user’],
],
]
11.4. ROUTING 475
Compared to the URL management for Web applications, the main new
thing above is the use of yii\rest\UrlRule for routing RESTful API re-
quests. This special URL rule class will create a whole set of child URL
rules to support routing and URL creation for the specified controller(s).
For example, the above code is roughly equivalent to the following rules:
[
’PUT,PATCH users/<id>’ => ’user/update’,
’DELETE users/<id>’ => ’user/delete’,
’GET,HEAD users/<id>’ => ’user/view’,
’POST users’ => ’user/create’,
’GET,HEAD users’ => ’user/index’,
’users/<id>’ => ’user/options’,
’users’ => ’user/options’,
]
You may have noticed that the controller ID user appears in plural form as
users in the endpoint URLs. This is because yii\rest\UrlRule automatic-
476 CHAPTER 11. RESTFUL WEB SERVICES
ally pluralizes controller IDs when creating child URL rules. You may disable
this behavior by setting yii\rest\UrlRule::$pluralize to be false.
In case when the automatic pluralization does not meet your requirement,
you may also configure the yii\rest\UrlRule::$controller property to
explicitly specify how to map a name used in endpoint URLs to a controller
ID. For example, the following code maps the name u to the controller ID
user.
[
’class’ => ’yii\rest\UrlRule’,
’controller’ => [’u’ => ’user’],
]
1. Determine various factors that may affect the response format, such
as media type, language, version, etc. This process is also known as
content negotiation4 .
2. Convert resource objects into arrays, as described in the Resources
section. This is done by yii\rest\Serializer.
4
http://en.wikipedia.org/wiki/Content_negotiation
11.5. RESPONSE FORMATTING 477
HTTP/1.1 200 OK
Date: Sun, 02 Mar 2014 05:31:43 GMT
Server: Apache/2.2.26 (Unix) DAV/2 PHP/5.4.20 mod_ssl/2.2.26 OpenSSL/0.9.8y
X-Powered-By: PHP/5.4.20
X-Pagination-Total-Count: 1000
X-Pagination-Page-Count: 50
X-Pagination-Current-Page: 1
X-Pagination-Per-Page: 20
Link: <http://localhost/users?page=1>; rel=self,
<http://localhost/users?page=2>; rel=next,
<http://localhost/users?page=50>; rel=last
Transfer-Encoding: chunked
Content-Type: application/json; charset=UTF-8
[
{
"id": 1,
...
},
{
"id": 2,
...
},
...
]
Behind the scene, before a RESTful API controller action is executed, the
yii\filters\ContentNegotiator filter will check the Accept HTTP header
in the request and set the response format to be ’json’. After the action
is executed and returns the resulting resource object or collection, yii\rest
\Serializer will convert the result into an array. And finally, yii\web
\JsonResponseFormatter will serialize the array into a JSON string and
include it in the response body.
478 CHAPTER 11. RESTFUL WEB SERVICES
The keys of the formats property are the supported MIME types, while the
values are the corresponding response format names which must be suppor-
ted in yii\web\Response::$formatters.
You may then get the following response for request http://localhost/users:
HTTP/1.1 200 OK
Date: Sun, 02 Mar 2014 05:31:43 GMT
Server: Apache/2.2.26 (Unix) DAV/2 PHP/5.4.20 mod_ssl/2.2.26 OpenSSL/0.9.8y
X-Powered-By: PHP/5.4.20
X-Pagination-Total-Count: 1000
X-Pagination-Page-Count: 50
X-Pagination-Current-Page: 1
X-Pagination-Per-Page: 20
11.5. RESPONSE FORMATTING 479
{
"items": [
{
"id": 1,
...
},
{
"id": 2,
...
},
...
],
"_links": {
"self": {
"href": "http://localhost/users?page=1"
},
"next": {
"href": "http://localhost/users?page=2"
},
"last": {
"href": "http://localhost/users?page=50"
}
},
"_meta": {
"totalCount": 1000,
"pageCount": 50,
"currentPage": 1,
"perPage": 20
}
}
When returning data from a database using the DAO database layer all
data will be represented as strings, which is not always the expected result
especially numeric values should be represented as numbers in JSON. When
using the ActiveRecord layer for retrieving data from the database, the values
for numeric columns will be converted to integers when data is fetched from
the database in yii\db\ActiveRecord::populateRecord().
11.6 Authentication
Unlike Web applications, RESTful APIs are usually stateless, which means
sessions or cookies should not be used. Therefore, each request should come
with some sort of authentication credentials because the user authentication
status may not be maintained by sessions or cookies. A common practice
is to send a secret access token with each request to authenticate the user.
Since an access token can be used to uniquely identify and authenticate a
user, API requests should always be sent via HTTPS to prevent
man-in-the-middle (MitM) attacks.
There are different ways to send an access token:
• HTTP Basic Auth5 : the access token is sent as the username. This
should only be used when an access token can be safely stored on the
API consumer side. For example, the API consumer is a program
running on a server.
• Query parameter: the access token is sent as a query parameter in the
API URL, e.g., https://example.com/users?access-token=xxxxxxxx. Be-
cause most Web servers will keep query parameters in server logs, this
approach should be mainly used to serve JSONP requests which cannot
use HTTP headers to send access tokens.
• OAuth 26 : the access token is obtained by the consumer from an
authorization server and sent to the API server via HTTP Bearer
Tokens7 , according to the OAuth2 protocol.
Yii supports all of the above authentication methods. You can also easily
create new authentication methods.
To enable authentication for your APIs, do the following steps:
5
http://en.wikipedia.org/wiki/Basic_access_authentication
6
http://oauth.net/2/
7
http://tools.ietf.org/html/rfc6750
11.6. AUTHENTICATION 481
3. Implement yii\web\IdentityInterface::findIdentityByAccessToken()
in your user identity class.
Step 1 is not required but is recommended for RESTful APIs which should
be stateless. When enableSession is false, the user authentication status
will NOT be persisted across requests using sessions. Instead, authentication
will be performed for every request, which is accomplished by Step 2 and 3.
For example, to use HTTP Basic Auth, you may configure the authenticator
behavior as follows,
use yii\filters\auth\HttpBasicAuth;
11.6.1 Authorization
After a user is authenticated, you probably want to check if he or she has the
permission to perform the requested action for the requested resource. This
process is called authorization which is covered in detail in the Authorization
section.
If your controllers extend from yii\rest\ActiveController, you may
override the checkAccess() method to perform authorization check. The
11.7. RATE LIMITING 483
Once the identity class implements the required interface, Yii will automat-
ically use yii\filters\RateLimiter configured as an action filter for yii
\rest\Controller to perform rate limiting check. The rate limiter will
484 CHAPTER 11. RESTFUL WEB SERVICES
When rate limiting is enabled, by default every response will be sent with the
following HTTP headers containing the current rate limiting information:
• X-Rate-Limit-Limit, the maximum number of requests allowed with a
time period
• X-Rate-Limit-Remaining, the number of remaining requests in the current
time period
• X-Rate-Limit-Reset, the number of seconds to wait in order to get the
maximum number of allowed requests
You may disable these headers by configuring yii\filters\RateLimiter::
$enableRateLimitHeaders to be false, as shown in the above code example.
11.8 Versioning
A good API is versioned : changes and new features are implemented in new
versions of the API instead of continually altering just one version. Unlike
Web applications, with which you have full control of both the client-side
and server-side code, APIs are meant to be used by clients beyond your
control. For this reason, backward compatibility (BC) of the APIs should be
maintained whenever possible. If a change that may break BC is necessary,
you should introduce it in new version of the API, and bump up the version
number. Existing clients can continue to use the old, working version of the
API; and new or upgraded clients can get the new functionality in the new
API version.
// via a parameter
Accept: application/json; version=v1
// via a vendor content type
Accept: application/vnd.company.myapp-v1+json
Both methods have their pros and cons, and there are a lot of debates about
each approach. Below you’ll see a practical strategy for API versioning that
is a mix of these two methods:
• Put each major version of API implementation in a separate module
whose ID is the major version number (e.g. v1, v2). Naturally, the API
URLs will contain major version numbers.
• Within each major version (and thus within the corresponding mod-
ule), use the Accept HTTP request header to determine the minor
version number and write conditional code to respond to the minor
versions accordingly.
For each module serving a major version, the module should include the re-
source and controller classes serving that specific version. To better separate
code responsibility, you may keep a common set of base resource and con-
troller classes, and subclass them in each individual version module. Within
the subclasses, implement the concrete code such as Model::fields().
Your code may be organized like the following:
api/
common/
controllers/
UserController.php
PostController.php
models/
User.php
Post.php
modules/
v1/
controllers/
UserController.php
PostController.php
models/
User.php
Post.php
Module.php
v2/
controllers/
UserController.php
PostController.php
models/
User.php
Post.php
Module.php
’v1’ => [
’class’ => ’app\modules\v1\Module’,
],
’v2’ => [
’class’ => ’app\modules\v2\Module’,
],
],
’components’ => [
’urlManager’ => [
’enablePrettyUrl’ => true,
’enableStrictParsing’ => true,
’showScriptName’ => false,
’rules’ => [
[’class’ => ’yii\rest\UrlRule’, ’controller’ => [’v1/user’,
’v1/post’]],
[’class’ => ’yii\rest\UrlRule’, ’controller’ => [’v2/user’,
’v2/post’]],
],
],
],
];
you should consider throwing an exception along with a proper HTTP status
code (e.g., yii\web\NotFoundHttpException represents a 404 status code).
Yii will send the response along with the corresponding HTTP status code
and text. Yii will also include the serialized representation of the exception
in the response body. For example:
{
"name": "Not Found Exception",
"message": "The requested resource was not found.",
"code": 0,
"status": 404
}
The following list summarizes the HTTP status codes that are used by the
Yii REST framework:
• 200: OK. Everything worked as expected.
• 201: A resource was successfully created in response to a POST request.
The Location header contains the URL pointing to the newly created
resource.
• 204: The request was handled successfully and the response contains
no body content (like a DELETE request).
• 304: The resource was not modified. You can use the cached version.
• 400: Bad request. This could be caused by various actions by the user,
such as providing invalid JSON data in the request body, providing
invalid action parameters, etc.
• 401: Authentication failed.
• 403: The authenticated user is not allowed to access the specified API
endpoint.
• 404: The requested resource does not exist.
• 405: Method not allowed. Please check the Allow header for the allowed
HTTP methods.
• 415: Unsupported media type. The requested content type or version
number is invalid.
• 422: Data validation failed (in response to a POST request, for example).
Please check the response body for detailed error messages.
• 429: Too many requests. The request was rejected due to rate limiting.
• 500: Internal server error. This could be caused by internal program
errors.
488 CHAPTER 11. RESTFUL WEB SERVICES
{
"success": false,
"data": {
"name": "Not Found Exception",
"message": "The requested resource was not found.",
"code": 0,
"status": 404
}
}
To achieve this goal, you can respond to the beforeSend event of the response
component in the application configuration:
return [
// ...
’components’ => [
’response’ => [
’class’ => ’yii\web\Response’,
’on beforeSend’ => function ($event) {
$response = $event->sender;
if ($response->data !== null && Yii::$app->request->get(’
suppress_response_code’)) {
$response->data = [
’success’ => $response->isSuccessful,
’data’ => $response->data,
];
$response->statusCode = 200;
}
},
],
],
];
The above code will reformat the response (for both successful and failed
responses) as explained when suppress_response_code is passed as a GET para-
meter.
Chapter 12
Development Tools
489
490 CHAPTER 12. DEVELOPMENT TOOLS
Testing
13.1 Testing
Testing is an important part of software development. Whether we are aware
of it or not, we conduct testing continuously. For example, when we write
a class in PHP, we may debug it step by step or simply use echo or die
statements to verify the implementation works according to our initial plan.
In the case of a web application, we’re entering some test data in forms to
ensure the page interacts with us as expected.
The testing process could be automated so that each time when we need
to verify something, we just need to call up the code that does it for us.
The code that verifies the result matches what we’ve planned is called test
and the process of its creation and further execution is known as automated
testing, which is the main topic of these testing chapters.
493
494 CHAPTER 13. TESTING
Tip: If you feel that you are losing time doing a lot of small and
simple iterations, try covering more by your test scenario so you
do more before executing tests again. If you’re debugging too
much, try doing the opposite.
The reason to create tests before doing any implementation is that it allows
us to focus on what we want to achieve and fully dive into “how to do it” af-
terwards. Usually it leads to better abstractions and easier test maintenance
when it comes to feature adjustments or less coupled components.
So to sum up the advantages of such approach are the following:
• Keeps you focused on one thing at a time which results in improved
planning and implementation.
• Results in test-covering more features in greater detail i.e. if tests are
OK most likely nothing’s broken.
In the long term it usually gives you a good time-saving effect.
A unit test verifies that a single unit of code is working as expected. That is,
given different input parameters, the test verifies the class method returns
expected results. Unit tests are usually developed by people who write the
classes being tested.
Unit testing in Yii is built on top of PHPUnit and, optionally, Codecep-
tion so it’s recommended to go through their docs:
• Codeception for Yii framework4
• Codeception Unit Tests5
• PHPUnit docs starting from chapter 26
If you’ve started with advanced template, please refer to “testing” guide7 for
more details about running tests.
If you’ve started with basic template, check its README “testing” sec-
tion8 .
2
https://github.com/yiisoft/yii2-app-basic
3
https://github.com/yiisoft/yii2-app-advanced
4
http://codeception.com/for/yii
5
http://codeception.com/docs/05-UnitTests
6
http://phpunit.de/manual/current/en/writing-tests-for-phpunit.html
7
https://github.com/yiisoft/yii2-app-advanced/blob/master/docs/guide/
start-testing.md
8
https://github.com/yiisoft/yii2-app-basic/blob/master/README.md#testing
496 CHAPTER 13. TESTING
13.6 Fixtures
Fixtures are an important part of testing. Their main purpose is to set up
the environment in a fixed/known state so that your tests are repeatable and
run in an expected way. Yii provides a fixture framework that allows you to
define your fixtures precisely and use them easily both when running your
tests with Codeception and independently.
A key concept in the Yii fixture framework is the so-called fixture object.
A fixture object represents a particular aspect of a test environment and is
an instance of yii\test\Fixture or its child class. For example, you may
use UserFixture to make sure the user DB table contains a fixed set of data.
You load one or multiple fixture objects before running a test and unload
them when finishing.
A fixture may depend on other fixtures, specified via its yii\test\Fixture
::$depends property. When a fixture is being loaded, the fixtures it depends
on will be automatically loaded BEFORE the fixture; and when the fixture is
being unloaded, the dependent fixtures will be unloaded AFTER the fixture.
use yii\test\ActiveFixture;
{
public $modelClass = ’app\models\User’;
}
You may give an alias to a row so that later in your test, you may refer to
the row via the alias. In the above example, the two rows are aliased as user1
and user2, respectively.
Also, you do not need to specify the data for auto-incremental columns.
Yii will automatically fill the actual values into the rows when the fixture is
being loaded.
13.6. FIXTURES 499
Tip: You may customize the location of the data file by setting
the yii\test\ActiveFixture::$dataFile property. You may
also override yii\test\ActiveFixture::getData() to provide
the data.
use yii\test\ActiveFixture;
The dependency also ensures, that the fixtures are loaded and unloaded in
a well defined order. In the above example UserFixture will always be loaded
before UserProfileFixture to ensure all foreign key references exist and will
be unloaded after UserProfileFixture has been unloaded for the same reason.
In the above, we have shown how to define a fixture about a DB table.
To define a fixture not related with DB (e.g. a fixture about certain files
and directories), you may extend from the more general base class yii\test
\Fixture and override the load() and unload() methods.
use app\tests\fixtures\UserProfileFixture;
18
http://codeception.com/
500 CHAPTER 13. TESTING
// ...test methods...
}
you organize the data files in a hierarchical way that is similar to your class
namespaces. For example,
# under folder tests\unit\fixtures
data\
components\
fixture_data_file1.php
fixture_data_file2.php
...
fixture_data_fileN.php
models\
fixture_data_file1.php
fixture_data_file2.php
...
fixture_data_fileN.php
# and so on
In this way you will avoid collision of fixture data files between tests and use
them as you need.
Note: In the example above fixture files are named only for ex-
ample purpose. In real life you should name them according to
which fixture class your fixture classes are extending from. For
example, if you are extending from yii\test\ActiveFixture
for DB fixtures, you should use DB table names as the fix-
ture data file names; If you are extending from yii\mongodb
\ActiveFixture for MongoDB fixtures, you should use collec-
tion names as the file names.
The similar hierarchy can be used to organize fixture class files. Instead of
using data as the root directory, you may want to use fixtures as the root
directory to avoid conflict with the data files.
Yii supports fixtures via the yii fixture command line tool. This tool sup-
ports:
• Loading fixtures to different storage such as: RDBMS, NoSQL, etc;
• Unloading fixtures in different ways (usually it is clearing storage);
• Auto-generating fixtures and populating it with random data.
return [
502 CHAPTER 13. TESTING
[
’name’ => ’Chase’,
’login’ => ’lmayert’,
’email’ => ’[email protected]’,
’auth_key’ => ’K3nF70it7tzNsHddEiq0BZ0i-OU8S3xV’,
’password’ => ’$2y$13$WSyE5hHsG1rWN2jV8LRHzubilrCLI5Ev/
iK0r3jRuwQEs2ldRu.a2’,
],
[
’name’ => ’Celestine’,
’login’ => ’napoleon69’,
’email’ => ’[email protected]’,
’auth_key’ => ’dZlXsVnIDgIzFgX4EduAqkEPuphhOh9q’,
’password’ => ’$2y$13$kkgpvJ8lnjKo8RuoR30ay.RjDf15bMcHIF7Vz1zz/6
viYG5xJExU6’,
],
];
If we are using fixture that loads data into database then these rows will be
applied to users table. If we are using nosql fixtures, for example mongodb
fixture, then this data will be applied to users mongodb collection. In order
to learn about implementing various loading strategies and more, refer to
official documentation19 . Above fixture example was auto-generated by yii2
-faker extension, read more about it in these section. Fixture classes name
should not be plural.
Loading fixtures
Fixture classes should be suffixed by Fixture class. By default fixtures will be
searched under tests\unit\fixtures namespace, you can change this behavior
with config or command options. You can exclude some fixtures due load or
unload by specifying - before its name like -User.
To load fixture, run the following command:
The required fixture_name parameter specifies a fixture name which data will
be loaded. You can load several fixtures at once. Below are correct formats
of this command:
// load ‘User‘ fixture
yii fixture/load User
// same as above
yii fixture "*"
Unloading fixtures
While command line options allow us to configure the fixture command on-
the-fly, sometimes we may want to configure the command once for all. For
example you can configure different fixture path as follows:
504 CHAPTER 13. TESTING
’controllerMap’ => [
’fixture’ => [
’class’ => ’yii\console\controllers\FixtureController’,
’namespace’ => ’myalias\some\custom\namespace’,
’globalFixtures’ => [
’some\name\space\Foo’,
’other\name\space\Bar’
],
],
]
Auto-generating fixtures
Yii also can auto-generate fixtures for you based on some template. You
can generate your fixtures with different data on different languages and
formats. This feature is done by Faker20 library and yii2-faker extension.
See extension guide21 for more docs.
13.6.5 Summary
In the above, we have described how to define and use fixtures. Below we
summarize the typical workflow of running unit tests related with DB:
1. Use yii migrate tool to upgrade your test database to the latest version;
20
https://github.com/fzaninotto/Faker
21
https://github.com/yiisoft/yii2-faker
Chapter 14
Special Topics
505
506 CHAPTER 14. SPECIAL TOPICS
While the basic1 and advanced2 project templates are great for most of your
needs, you may want to create your own project template with which to start
your projects.
Project templates in Yii are simply repositories containing a composer.json
file, and registered as a Composer package. Any repository can be identified
as a Composer package, making it installable via create-project Composer
command.
Since it’s a bit too much to start building your entire template from
scratch, it is better to use one of the built-in templates as a base. Let’s use
the basic template here.
Then wait for the repository to be downloaded to your computer. Since the
changes made to the template won’t be pushed back, you can delete the .git
directory and all of its contents from the download.
Next, actually modify the structure and contents of the application as you
would like the default to be. Finally, update the README file to be applic-
able to your template.
place to host it. If you intend to keep your template non-collaborative, any
Git repository site will do.
Next, you need to register your package for Composer’s sake. For public
templates, the package should be registered at Packagist4 . For private tem-
plates, it is a bit more tricky to register the package. For instructions, see
the Composer documentation5 .
That’s all that’s required to create a new Yii project template. Now you can
create projects using your template:
Besides the rich features for building web applications, Yii also has full-
featured support for console applications which are mainly used to create
background and maintenance tasks that need to be performed for a website.
The structure of console applications is very similar to a Yii web applica-
tion. It consists of one or more yii\console\Controller classes, which are
often referred to as commands in the console environment. Each controller
can also have one or more actions, just like web controllers.
Both project templates already have a console application with them.
You can run it by calling the yii script, which is located in the base directory
of the repository. This will give you a list of available commands when you
run it without any further parameters:
4
https://packagist.org/
5
https://getcomposer.org/doc/05-repositories.md#hosting-your-own
14.2. CONSOLE APPLICATIONS 509
As you can see in the screenshot, Yii has already defined a set of com-
mands that are available by default:
• AssetController - Allows you to combine and compress your JavaS-
cript and CSS files. You can learn more about this command in the
Assets Section.
• CacheController - Allows you to flush application caches.
• FixtureController - Manages fixture data loading and unloading for
testing purposes. This command is described in more detail in the
Testing Section about Fixtures.
• HelpController - Provides help information about console commands,
this is the default command and prints what you have seen in the above
output.
• MessageController - Extracts messages to be translated from source
files. To learn more about this command, please refer to the I18N
Section.
• MigrateController - Manages application migrations. Database mi-
grations are described in more detail in the Database Migration Sec-
tion.
• ServeController - Allows you run PHP built-in web server.
14.2.1 Usage
You execute a console controller action using the following syntax:
yii <route> [--option1=value1 --option2=value2 ... argument1 argument2 ...]
510 CHAPTER 14. SPECIAL TOPICS
In the above, <route> refers to the route to the controller action. The options
will populate the class properties and arguments are the parameters of the
action method.
For example, the MigrateController::actionUp() with MigrateController
::$migrationTable set to migrations and a limit of 5 migrations can be
called like so:
yii migrate/up 5 --migrationTable=migrations
This script will be created as part of your application; you’re free to edit it
to suit your needs. The YII_DEBUG constant can be set to false if you do not
want to see a stack trace on error, and/or if you want to improve the overall
performance. In both basic and advanced application templates, the console
application entry script has debugging enabled by default to provide a more
developer-friendly environment.
14.2.3 Configuration
As can be seen in the code above, the console application uses its own con-
figuration file, named console.php. In this file you should configure various
14.2. CONSOLE APPLICATIONS 511
Bash completion
Make sure bash completion is installed. For most of installations it is avail-
able by default.
Place the completion script in /etc/bash_completion.d/:
curl -L https://raw.githubusercontent.com/yiisoft/yii2/master/contrib/
completion/bash/yii -o /etc/bash_completion.d/yii
For temporary usage you can put the file into the current directory and
include it in the current session via source yii. If globally installed you may
need to restart the terminal or source ~/.bashrc to activate it.
Check the Bash Manual6 for other ways of including completion script
to your environment.
ZSH completion
Put the completion script in directory for completions, using e.g. ~/.zsh/
completion/
6
https://www.gnu.org/software/bash/manual/html_node/
Programmable-Completion.html
512 CHAPTER 14. SPECIAL TOPICS
mkdir -p ~/.zsh/completion
curl -L https://raw.githubusercontent.com/yiisoft/yii2/master/contrib/
completion/zsh/_yii -o ~/.zsh/completion/_yii
Options
By overriding the yii\console\Controller::options() method, you can
specify options that are available to a console command (controller/actionID).
The method should return a list of the controller class’s public properties.
When running a command, you may specify the value of an option using the
syntax --optionName=optionValue. This will assign optionValue to the optionName
property of the controller class.
If the default value of an option is of an array type and you set this
option while running the command, the option value will be converted into
an array by splitting the input string on any commas.
Options Aliases
Since version 2.0.8 console command provides yii\console\Controller::
optionAliases() method to add aliases for options.
To define an alias, override yii\console\Controller::optionAliases()
in your controller, for example:
14.2. CONSOLE APPLICATIONS 513
namespace app\commands;
use yii\console\Controller;
Now, you can use the following syntax to run the command:
yii hello -m=hello
Arguments
Besides options, a command can also receive arguments. The arguments
will be passed as the parameters to the action method corresponding to
the requested sub-command. The first argument corresponds to the first
parameter, the second corresponds to the second, and so on. If not enough
arguments are provided when the command is called, the corresponding para-
meters will take the declared default values, if defined. If no default value
is set, and no value is provided at runtime, the command will exit with an
error.
You may use the array type hint to indicate that an argument should be
treated as an array. The array will be generated by splitting the input string
on commas.
The following example shows how to declare arguments:
class ExampleController extends \yii\console\Controller
{
// The command "yii example/create test" will call "actionCreate(’test’)
"
public function actionCreate($name) { ... }
Exit Code
Using exit codes is a best practice for console application development. Con-
ventionally, a command returns 0 to indicate that everything is OK. If the
command returns a number greater than zero, that’s considered to be indic-
ative of an error. The number returned will be the error code, potentially
usable to find out details about the error. For example 1 could stand gener-
ally for an unknown error and all codes above would be reserved for specific
cases: input errors, missing files, and so forth.
To have your console command return an exit code, simply return an
integer in the controller action method:
public function actionIndex()
{
if (/* some problem */) {
echo "A problem occurred!\n";
return 1;
}
// do something
return 0;
}
There are some predefined constants you can use. These are defined in the
yii\console\ExitCode class:
public function actionIndex()
{
if (/* some problem */) {
echo "A problem occurred!\n";
return ExitCode::UNSPECIFIED_ERROR;
}
// do something
return ExitCode::OK;
}
It’s a good practice to define meaningful constants for your controller in case
you have more specific error code types.
If you need to build string dynamically combining multiple styles it’s better
to use ansiFormat():
$name = $this->ansiFormat(’Alex’, Console::FG_YELLOW);
echo "Hello, my name is $name.";
Tables
Since version 2.0.13 there is a widget that allows you to format table data
in console. It could be used as the following:
echo Table::widget([
’headers’ => [’Project’, ’Status’, ’Participant’],
’rows’ => [
[’Yii’, ’OK’, ’@samdark’],
[’Yii’, ’OK’, ’@cebe’],
],
]);
14.3.1 boolean
[
// checks if "selected" is either 0 or 1, regardless of data type
[’selected’, ’boolean’],
516 CHAPTER 14. SPECIAL TOPICS
Note: Because data input submitted via HTML forms are all
strings, you normally should leave the strict property as false.
14.3.2 captcha
[
[’verificationCode’, ’captcha’],
]
14.3.3 compare
[
// validates if the value of "password" attribute equals to that of "
password_repeat"
[’password’, ’compare’],
This validator compares the specified input value with another one and make
sure if their relationship is as specified by the operator property.
14.3. CORE VALIDATORS 517
The compare validator can only be used to compare strings and numbers. If
you need to compare values like dates you have two options. For comparing
a date against a fixed value, you can simply use the date validator and
specify its $min or $max property. If you need to compare two dates entered
in the form, e.g. a fromDate and a toDate field, you can use a combination of
compare and date validator like the following:
[’fromDate’, ’date’, ’timestampAttribute’ => ’fromDate’],
[’toDate’, ’date’, ’timestampAttribute’ => ’toDate’],
[’fromDate’, ’compare’, ’compareAttribute’ => ’toDate’, ’operator’ => ’<’, ’
enableClientValidation’ => false],
518 CHAPTER 14. SPECIAL TOPICS
As validators are executed in the order they are specified this will first valid-
ate that the values entered in fromDate and toDate are valid date values and if
so, they will be converted into a machine readable format. Afterwards these
two values are compared with the compare validator. Client validation is not
enabled as this will only work on the server-side because the date validator
currently does not provide client validation, so $enableClientValidation
is set to false on the compare validator too.
14.3.4 date
The date validator comes with three different shortcuts:
[
[[’from_date’, ’to_date’], ’date’],
[[’from_datetime’, ’to_datetime’], ’datetime’],
[[’some_time’], ’time’],
]
This validator checks if the input value is a date, time or datetime in a proper
format. Optionally, it can convert the input value into a UNIX timestamp
or other machine readable format and store it in an attribute specified via
timestampAttribute.
• format: the date/time format that the value being validated should be
in. This can be a date time pattern as described in the ICU manual7 .
Alternatively this can be a string prefixed with php: representing a
format that can be recognized by the PHP Datetime class. Please refer
to http://php.net/manual/en/datetime.createfromformat.php on
supported formats. If this is not set, it will take the value of Yii::$app
->formatter->dateFormat. See the API documentation for more details.
• timestampAttribute: the name of the attribute to which this validator
may assign the UNIX timestamp converted from the input date/time.
This can be the same attribute as the one being validated. If this is the
case, the original value will be overwritten with the timestamp value
after validation. See “Handling date input with the DatePicker” 8 for a
usage example.
Since version 2.0.4, a format and timezone can be specified for this at-
tribute using $timestampAttributeFormat and $timestampAttributeTimeZone.
Note, that when using timestampAttribute, the input value will be con-
verted to a unix timestamp, which by definition is in UTC, so a con-
version from the input time zone to UTC will be performed.
• Since version 2.0.4 it is also possible to specify a minimum or maximum
timestamp.
7
http://userguide.icu-project.org/formatparse/datetime#
TOC-Date-Time-Format-Syntax
8
https://github.com/yiisoft/yii2-jui/blob/master/docs/guide/
topics-date-picker.md
14.3. CORE VALIDATORS 519
In case the input is optional you may also want to add a default value filter
in addition to the date validator to ensure empty input is stored as null.
Otherwise you may end up with dates like 0000-00-00 in your database or
1970-01-01 in the input field of a date picker.
[
[[’from_date’, ’to_date’], ’default’, ’value’ => null],
[[’from_date’, ’to_date’], ’date’],
],
14.3.5 default
[
// set "age" to be null if it is empty
[’age’, ’default’, ’value’ => null],
// assign "from" and "to" with a date 3 days and 6 days from today, if
they are empty
[[’from’, ’to’], ’default’, ’value’ => function ($model, $attribute) {
return date(’Y-m-d’, strtotime($attribute === ’to’ ? ’+3 days’ : ’+6
days’));
}],
]
This validator does not validate data. Instead, it assigns a default value to
the attributes being validated if the attributes are empty.
• value: the default value or a PHP callable that returns the default
value which will be assigned to the attributes being validated if they
are empty. The signature of the PHP callable should be as follows,
function foo($model, $attribute) {
// ... compute $value ...
return $value;
}
14.3.6 double
[
// checks if "salary" is a double number
[’salary’, ’double’],
]
520 CHAPTER 14. SPECIAL TOPICS
14.3.7 each
Info: This validator has been available since version 2.0.4.
[
// checks if every category ID is an integer
[’categoryIDs’, ’each’, ’rule’ => [’integer’]],
]
This validator only works with an array attribute. It validates if every ele-
ment of the array can be successfully validated by a specified validation rule.
In the above example, the categoryIDs attribute must take an array value
and each array element will be validated by the integer validation rule.
• rule: an array specifying a validation rule. The first element in the
array specifies the class name or the alias of the validator. The rest of
the name-value pairs in the array are used to configure the validator
object.
• allowMessageFromRule: whether to use the error message returned by the
embedded validation rule. Defaults to true. If false, it will use message
as the error message.
14.3.8 email
[
// checks if "email" is a valid email address
[’email’, ’email’],
]
• enableIDN, whether the validation process should take into account IDN
(internationalized domain names). Defaults to false. Note that in
order to use IDN validation you have to install and enable the intl
PHP extension, or an exception would be thrown.
14.3.9 exist
[
// a1 needs to exist in the column represented by the "a1" attribute
[’a1’, ’exist’],
// a1 needs to exist, but its value will use a2 to check for the
existence
[’a1’, ’exist’, ’targetAttribute’ => ’a2’],
// a1 and a2 need to exist together, and they both will receive error
message
[[’a1’, ’a2’], ’exist’, ’targetAttribute’ => [’a1’, ’a2’]],
This validator checks if the input value can be found in a table column
represented by an Active Record attribute. You can use targetAttribute to
specify the Active Record attribute and targetClass the corresponding Active
Record class. If you do not specify them, they will take the values of the
attribute and the model class being validated.
You can use this validator to validate against a single column or multiple
columns (i.e., the combination of multiple attribute values should exist).
• targetClass: the name of the Active Record class that should be used
to look for the input value being validated. If not set, the class of the
model currently being validated will be used.
• targetAttribute: the name of the attribute in targetClass that should
be used to validate the existence of the input value. If not set, it will
use the name of the attribute currently being validated. You may use
an array to validate the existence of multiple columns at the same
time. The array values are the attributes that will be used to validate
the existence, while the array keys are the attributes whose values are
to be validated. If the key and the value are the same, you can just
specify the value.
522 CHAPTER 14. SPECIAL TOPICS
14.3.10 file
[
// checks if "primaryImage" is an uploaded image file in PNG, JPG or GIF
format.
// the file size must be less than 1MB
[’primaryImage’, ’file’, ’extensions’ => [’png’, ’jpg’, ’gif’], ’maxSize
’ => 1024*1024],
]
14.3.11 filter
[
// trim "username" and "email" inputs
[[’username’, ’email’], ’filter’, ’filter’ => ’trim’, ’skipOnArray’ =>
true],
This validator does not validate data. Instead, it applies a filter on the input
value and assigns it back to the attribute being validated.
• filter: a PHP callback that defines a filter. This can be a global
function name, an anonymous function, etc. The function signature
must be function ($value) { return $newValue; }. This property must
be set.
• skipOnArray: whether to skip the filter if the input value is an array.
Defaults to false. Note that if the filter cannot handle array input,
you should set this property to be true. Otherwise some PHP error
might occur.
Tip: If you want to trim input values, you may directly use the
trim validator.
Tip: There are many PHP functions that have the signature ex-
pected for the filter callback. For example to apply type casting
(using e.g. intval10 , boolval11 , . . . ) to ensure a specific type for
an attribute, you can simply specify the function names of the
filter without the need to wrap them in a closure:
[’property’, ’filter’, ’filter’ => ’boolval’],
[’property’, ’filter’, ’filter’ => ’intval’],
10
http://php.net/manual/en/function.intval.php
11
http://php.net/manual/en/function.boolval.php
524 CHAPTER 14. SPECIAL TOPICS
14.3.12 image
[
// checks if "primaryImage" is a valid image with proper size
[’primaryImage’, ’image’, ’extensions’ => ’png, jpg’,
’minWidth’ => 100, ’maxWidth’ => 1000,
’minHeight’ => 100, ’maxHeight’ => 1000,
],
]
This validator checks if the input value represents a valid image file. It
extends from the file validator and thus inherits all its properties. Besides,
it supports the following additional properties specific for image validation
purpose:
• minWidth: the minimum width of the image. Defaults to null, meaning
no lower limit.
• maxWidth: the maximum width of the image. Defaults to null, meaning
no upper limit.
• minHeight: the minimum height of the image. Defaults to null, meaning
no lower limit.
• maxHeight: the maximum height of the image. Defaults to null, meaning
no upper limit.
14.3.13 ip
[
// checks if "ip_address" is a valid IPv4 or IPv6 address
[’ip_address’, ’ip’],
] ‘ In this example, access is allowed for all the IPv4 and IPv6 addresses
excluding 192.168.10.0/24 subnet. IPv4 address 192.168.10.128 is also
allowed, because it is listed before the restriction.
• networks: array of network aliases, that can be used in ranges. Format
of array:
– key - alias name
– value - array of strings. String can be a range, IP address or an-
other alias. String can be negated with ! (independent of negation
option).
The following aliases are defined by default:
– *: any
– any: 0.0.0.0/0, ::/0
– private: 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16, fd00::/8
– multicast: 224.0.0.0/4, ff00::/8
– linklocal: 169.254.0.0/16, fe80::/10
– localhost: 127.0.0.0/8’, ::1
– documentation: 192.0.2.0/24, 198.51.100.0/24, 203.0.113.0/24, 2001:
526 CHAPTER 14. SPECIAL TOPICS
db8::/32
– system: multicast, linklocal, localhost, documentation
14.3.14 in
[
// checks if "level" is 1, 2 or 3
[’level’, ’in’, ’range’ => [1, 2, 3]],
]
This validator checks if the input value can be found among the given list of
values.
• range: a list of given values within which the input value should be
looked for.
• strict: whether the comparison between the input value and the given
values should be strict (both the type and value must be the same).
Defaults to false.
• not: whether the validation result should be inverted. Defaults to false
. When this property is set true, the validator checks if the input value
is NOT among the given list of values.
• allowArray: whether to allow the input value to be an array. When this
is true and the input value is an array, every element in the array must
be found in the given list of values, or the validation would fail.
14.3.15 integer
[
// checks if "age" is an integer
[’age’, ’integer’],
]
14.3.16 match
[
// checks if "username" starts with a letter and contains only word
characters
[’username’, ’match’, ’pattern’ => ’/^[a-z]\w*$/i’]
]
14.3. CORE VALIDATORS 527
This validator checks if the input value matches the specified regular expres-
sion.
• pattern: the regular expression that the input value should match. This
property must be set, or an exception will be thrown.
• not: whether to invert the validation result. Defaults to false, meaning
the validation succeeds only if the input value matches the pattern. If
this is set true, the validation is considered successful only if the input
value does NOT match the pattern.
14.3.17 number
[
// checks if "salary" is a number
[’salary’, ’number’],
]
14.3.18 required
[
// checks if both "username" and "password" are not empty
[[’username’, ’password’], ’required’],
]
This validator checks if the input value is provided and not empty.
• requiredValue: the desired value that the input should be. If not set, it
means the input should not be empty.
• strict: whether to check data types when validating a value. Defaults
to false. When requiredValue is not set, if this property is true, the
validator will check if the input value is not strictly null; If this property
is false, the validator will use a loose rule to determine a value is empty
or not. When requiredValue is set, the comparison between the input
and requiredValue will also check data types if this property is true.
14.3.19 safe
528 CHAPTER 14. SPECIAL TOPICS
[
// marks "description" to be a safe attribute
[’description’, ’safe’],
]
This validator does not perform data validation. Instead, it is used to mark
an attribute to be a safe attribute.
14.3.20 string
[
// checks if "username" is a string whose length is between 4 and 24
[’username’, ’string’, ’length’ => [4, 24]],
]
This validator checks if the input value is a valid string with certain length.
• length: specifies the length limit of the input string being validated.
This can be specified in one of the following forms:
– an integer: the exact length that the string should be of;
– an array of one element: the minimum length of the input string
(e.g. [8]). This will overwrite min.
– an array of two elements: the minimum and maximum lengths of
the input string (e.g. [8, 128]). This will overwrite both min and
max.
• min: the minimum length of the input string. If not set, it means no
minimum length limit.
• max: the maximum length of the input string. If not set, it means no
maximum length limit.
• encoding: the encoding of the input string to be validated. If not set,
it will use the application’s charset value which defaults to UTF-8.
14.3.21 trim
[
// trims the white spaces surrounding "username" and "email"
[[’username’, ’email’], ’trim’],
]
This validator does not perform data validation. Instead, it will trim the
surrounding white spaces around the input value. Note that if the input
value is an array, it will be ignored by this validator.
14.3.22 unique
[
// a1 needs to be unique in the column represented by the "a1" attribute
[’a1’, ’unique’],
This validator checks if the input value is unique in a table column. It only
works with Active Record model attributes. It supports validation against
either a single column or multiple columns.
• targetClass: the name of the Active Record class that should be used
to look for the input value being validated. If not set, the class of the
model currently being validated will be used.
• targetAttribute: the name of the attribute in targetClass that should
be used to validate the uniqueness of the input value. If not set, it will
use the name of the attribute currently being validated. You may use
an array to validate the uniqueness of multiple columns at the same
time. The array values are the attributes that will be used to validate
the uniqueness, while the array keys are the attributes whose values
are to be validated. If the key and the value are the same, you can just
specify the value.
• filter: additional filter to be applied to the DB query used to check
the uniqueness of the input value. This can be a string or an array
representing the additional query condition (refer to yii\db\Query::
where() on the format of query condition), or an anonymous function
with the signature function ($query), where $query is the Query object
that you can modify in the function.
14.3.23 url
[
// checks if "website" is a valid URL. Prepend "http://" to the "website
" attribute
// if it does not have a URI scheme
[’website’, ’url’, ’defaultScheme’ => ’http’],
]
14.4.1 Requirements
• docker
• docker-compose
Visit the download page13 to get the Docker tooling.
14.4.2 Installation
After installation, you should be able to run docker ps and see an output
similar to
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
With Compose you can configure manage all services required for your ap-
plication, such as databases and caching.
14.4.3 Resources
• PHP-base images for Yii can be found at yii2-docker14
• Docker support for yii2-app-basic15
• Docker support for yii2-app-advanced16 is in development
14.4.4 Usage
Basic commands for Docker are
docker-compose up -d
to stop and remove all services, be aware of data loss when not using
host-volumes
To run commands in a container
docker-compose run --rm php composer install
14.5 Internationalization
Internationalization (I18N) refers to the process of designing a software ap-
plication so that it can be adapted to various languages and regions without
engineering changes. For Web applications, this is of particular importance
because the potential users may be worldwide. Yii offers a full spectrum of
I18N features that support message translation, view translation, date and
number formatting.
Language
In Yii, we often use the term “language” to refer to a locale.
A Yii application uses two kinds of languages:
18
http://www.loc.gov/standards/iso639-2/
19
https://en.wikipedia.org/wiki/ISO_3166-1#Current_codes
20
http://userguide.icu-project.org/locale#TOC-The-Locale-Concept
14.5. INTERNATIONALIZATION 533
• source language: This refers to the language in which the text mes-
sages in the source code are written.
• target language: This is the language that should be used to display
content to end users.
The so-called message translation service mainly translates a text message
from source language to target language.
Configuration
You can configure application languages in the “application configuration”
like the following:
return [
// set target language to be Russian
’language’ => ’ru-RU’,
......
];
The default value for the source language is en-US, meaning US English.
It is recommended that you keep this default value unchanged. Usually
it is much easier to find people who can translate from “English to other
languages” than from “non-English to non-English“.
You often need to set the target language dynamically based on differ-
ent factors, such as the language preference of end users. Instead of config-
uring it in the application configuration, you can use the following statement
to change the target language:
// change target language to Chinese
\Yii::$app->language = ’zh-CN’;
How to implement
To use the message translation service, you mainly need to do the following
work:
1. Wrap a text message The method Yii::t() can be used like the
following,
echo \Yii::t(’app’, ’This is a string to translate!’);
Category wildcards with * symbol The pattern app* indicates that all
message categories whose names start with app should be translated using
this message source.
14.5. INTERNATIONALIZATION 535
/**
* Translation map for nl-NL
*/
return [
’welcome’ => ’welkom’
];
Other storage types Besides storing the messages in PHP files, you may
also use the following message sources to store translated messages in differ-
ent storage:
• yii\i18n\GettextMessageSource uses GNU Gettext MO or PO files
to maintain translated messages.
• yii\i18n\DbMessageSource uses a database table to store translated
messages.
Message Parameters
In a message to be translated, you can embed one or multiple parameters
(also called placeholders) so that they can be replaced by the given values.
By giving different sets of values, you can variate the translated message
dynamically. In the following example, the placeholder {username} in the
message ’Hello, {username}!’ will be replaced by ’Alexander’ and ’Qiang’,
respectively.
$username = ’Alexander’;
// display a translated message with username being "Alexander"
echo \Yii::t(’app’, ’Hello, {username}!’, [
’username’ => $username,
]);
$username = ’Qiang’;
// display a translated message with username being "Qiang"
echo \Yii::t(’app’, ’Hello, {username}!’, [
’username’ => $username,
]);
Parameter Formatting
You can specify additional formatting rules in the placeholders of a message
so that the parameter values can be formatted properly before they replace
the placeholders. In the following example, the price parameter value will
be treated as a number and formatted as a currency value:
$price = 100;
echo \Yii::t(’app’, ’Price: {0,number,currency}’, $price);
You can specify an optional parameter style as short, medium, long, or full:
echo \Yii::t(’app’, ’Today is {0,date,short}’, time());
You can also specify a custom pattern to format the date value:
echo \Yii::t(’app’, ’Today is {0,date,yyyy-MM-dd}’, time());
Formatting reference24 .
You can specify an optional parameter style as short, medium, long, or full:
echo \Yii::t(’app’, ’It is {0,time,short}’, time());
You can also specify a custom pattern to format the time value:
echo \Yii::t(’app’, ’It is {0,date,HH:mm}’, time());
Formatting reference25 .
These other, few, many and other special argument names vary depending
on language. To learn which ones you should specify for a particular loc-
ale, please refer to “Plural Rules, Cardinal” at http://intl.rmcreative.ru/29 .
Alternatively you can refer to rules reference at unicode.org30 .
27
http://intl.rmcreative.ru/
28
http://intl.rmcreative.ru/
29
http://intl.rmcreative.ru/
30
http://cldr.unicode.org/index/cldr-spec/plural-rules
540 CHAPTER 14. SPECIAL TOPICS
There’s an offset parameter for the cases when the string is like the following:
$likeCount = 2;
echo Yii::t(’app’, ’You {likeCount,plural,
offset: 1
=0{did not like this}
=1{liked this}
one{and one other person liked this}
other{and # others liked this}
}’, [
’likeCount’ => $likeCount
]);
// Translation
’You are the {n,selectordinal,one{#st} two{#nd} few{#rd} other{#th}} visitor
’ => ’Вы {n,selectordinal,other{#-й}} посетитель’,
The format is very close to what’s used for plurals. To learn which argu-
ments you should specify for a particular locale, please refer to “Plural Rules,
Ordinal” at http://intl.rmcreative.ru/31 . Alternatively you can refer to rules
reference at unicode.org32 .
31
http://intl.rmcreative.ru/
32
http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/language_
plural_rules.html
14.5. INTERNATIONALIZATION 541
Selection You can use the select parameter type to choose a phrase based
on the parameter value. For example,
// It may produce "Snoopy is a dog and it loves Yii!"
echo \Yii::t(’app’, ’{name} is a {gender} and {gender,select,female{she}
male{he} other{it}} loves Yii!’, [
’name’ => ’Snoopy’,
’gender’ => ’dog’,
]);
In the expression above, both female and male are possible parameter values,
while other handles values that do not match either one of them. Following
each possible parameter value, you should specify a phrase and enclose it in
a pair of curly brackets.
’i18n’ => [
’translations’ => [
’*’ => [
’class’ => ’yii\i18n\PhpMessageSource’
],
],
],
Now you can use categories without configuring each one, which is similar to
Yii 1.1 behavior. Messages for the category will be loaded from a file under
the default translation basePath that is @app/messages:
echo Yii::t(’not_specified_category’, ’message from unspecified category’);
namespace app\modules\users;
use Yii;
{
public $controllerNamespace = ’app\modules\users\controllers’;
In the example above we are using wildcard for matching and then filtering
each category per needed file. Instead of using fileMap, you can simply use
the convention of the category mapping to the same named file. Now you
can use Module::t(’validation’, ’your custom validation message’) or Module::
t(’form’, ’some form label’) directly.
namespace app\widgets\menu;
use yii\base\Widget;
use Yii;
Instead of using fileMap you can simply use the convention of the category
mapping to the same named file. Now you can use Menu::t(’messages’, ’new
messages {messages}’, [’{messages}’ => 10]) directly.
Note: For widgets you also can use i18n views, with the same
rules as for controllers being applied to them too.
namespace app\components;
use yii\i18n\MissingTranslationEvent;
class TranslationEventHandler
{
public static function handleMissingTranslation(MissingTranslationEvent
$event)
{
14.5. INTERNATIONALIZATION 545
Open the created file and adjust the parameters to fit your needs. Pay special
attention to:
• languages: an array representing what languages your app should be
translated to;
• messagePath: path where to store message files, which should match the
i18n‘s basePath parameter stated in config.
You may also use ‘./yii message/config’ command to dynamically generate
configuration file with specified options via cli. For example, you can set
languages and messagePath parameters like the following:
Once you’re done with the configuration file you can finally extract your
messages with the command:
./yii message path/to/config.php
Also, you may use options to dynamically change parameters for extraction.
You will then find your files (if you’ve chosen file based translations) in
your messagePath directory.
546 CHAPTER 14. SPECIAL TOPICS
33
http://php.net/manual/en/book.intl.php
34
http://php.net/manual/en/book.intl.php
35
http://site.icu-project.org/
14.6. MAILING 547
It is also recommended that you use an ICU version equal or greater than
version 49. This will ensure you can use all the features described in this
document. For example, an ICU version below 49 does not support using
# placeholders in plural rules. Please refer to http://site.icu-project.
org/download for a complete list of available ICU versions. Note that the
version numbering has changed after the 4.8 release (e.g., ICU 4.8, ICU 49,
ICU 50, etc.)
Additionally the information in the time zone database shipped with the
ICU library may be outdated. Please refer to the ICU manual36 for details
on updating the time zone database. While for output formatting the ICU
timezone database is used, the time zone database used by PHP may be
relevant too. You can update it by installing the latest version of the pecl
package timezonedb37 .
14.6 Mailing
Note: This section is under development.
Yii supports composition and sending of the email messages. However, the
framework core provides only the content composition functionality and basic
interface. Actual mail sending mechanism should be provided by the exten-
sion, because different projects may require its different implementation and
it usually depends on the external services and libraries.
For the most common cases you can use yii2-swiftmailer38 official exten-
sion.
14.6.1 Configuration
Mail component configuration depends on the extension you have chosen. In
general your application configuration should look like:
return [
//....
’components’ => [
’mailer’ => [
’class’ => ’yii\swiftmailer\Mailer’,
],
],
];
36
http://userguide.icu-project.org/datetime/timezone#
TOC-Updating-the-Time-Zone-Data
37
http://pecl.php.net/package/timezonedb
38
https://github.com/yiisoft/yii2-swiftmailer
548 CHAPTER 14. SPECIAL TOPICS
In the above example the method compose() creates an instance of the mail
message, which then is populated and sent. You may put more complex logic
in this process if needed:
$message = Yii::$app->mailer->compose();
if (Yii::$app->user->isGuest) {
$message->setFrom(’[email protected]’);
} else {
$message->setFrom(Yii::$app->user->identity->email);
}
$message->setTo(Yii::$app->params[’adminEmail’])
->setSubject(’Message subject’)
->setTextBody(’Plain text content’)
->send();
Some particular mail extensions may benefit from this approach, using single
network message etc.
<?php
use yii\helpers\Html;
use yii\helpers\Url;
?>
<h2>This message allows you to visit our site home page by one click</h2>
<?= Html::a(’Go to home page’, Url::home(’http’)) ?>
In order to compose message content via view file simply pass view name to
the compose() method:
Yii::$app->mailer->compose(’home-link’) // a view rendering result becomes
the message body here
->setFrom(’[email protected]’)
->setTo(’[email protected]’)
->setSubject(’Message subject’)
->send();
You may pass additional view parameters to compose() method, which will
be available inside the view files:
Yii::$app->mailer->compose(’greetings’, [
’user’ => Yii::$app->user->identity,
’advertisement’ => $adContent,
]);
You can specify different view files for HTML and plain text message con-
tents:
Yii::$app->mailer->compose([
’html’ => ’contact-html’,
’text’ => ’contact-text’,
]);
If you specify view name as a scalar string, its rendering result will be used
as HTML body, while plain text body will be composed by removing all
HTML entities from HTML one.
View rendering result can be wrapped into the layout, which can be setup
using yii\mail\BaseMailer::$htmlLayout and yii\mail\BaseMailer::
$textLayout. It will work the same way like layouts in regular web ap-
plication. Layout can be used to setup mail CSS styles or other shared
content:
<?php
use yii\helpers\Html;
Then inside the view file you can use the following code:
<img src="<?= $message->embed($imageFileName); ?>">
14.7. PERFORMANCE TUNING 551
Note: you can either save the messages to the files or send them
to the actual recipients, but can not do both simultaneously.
A mail message file can be opened by a regular text file editor, so you can
browse the actual message headers, content and so on. This mechanism may
prove itself, while debugging application or running unit test.
return [
// ...
’components’ => [
// ...
’cache’ => [
’class’ => ’yii\caching\FileCache’,
],
’db’ => [
’class’ => ’yii\db\Connection’,
’dsn’ => ’mysql:host=localhost;dbname=mydatabase’,
’username’ => ’root’,
’password’ => ’’,
’enableSchemaCache’ => true,
In the above code, $posts will be populated as an array of table rows. Each
row is a plain array. To access the title column of the i-th row, you may
use the expression $posts[$i][’title’].
You may also use DAO to build queries and retrieve data in plain arrays.
Additionally you may consider using authoritative class maps43 and APCu
cache44 . Note that both opmizations may or may not be suitable for your
particular case.
If the pull frequency is too low, the tasks may be processed with great delay,
but if the frequency is too high, it will introduce high overhead.
In the push method, you would use a message queue (e.g. RabbitMQ,
ActiveMQ, Amazon SQS, etc.) to manage the tasks. Whenever a new task
is put on the queue, it will initiate or notify the task handling process to
trigger the task processing.
Renaming webroot
Connect to your shared host using FTP or by other means. You will probably
see something like the following.
config
logs
www
If you can write to the root level directory i.e. where config, logs and www
are, then upload assets, commands etc. as is to the root level directory.
If your webserver is Apache you’ll need to add an .htaccess file with the
following content to web (or public_html or whatever) (where the index.php
file is located):
Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on
In case of nginx you should not need any extra config files.
Check requirements
In order to run Yii, your webserver must meet its requirements. The very
minimum requirement is PHP 5.4. In order to check the requirements copy
requirements.php from your root directory into the webroot directory and run
it via browser using http://example.com/requirements.php URL. Don’t forget
to delete the file afterwards.
558 CHAPTER 14. SPECIAL TOPICS
In the code above, both Smarty and Twig are configured to be useable by
the view files. But in order to get these extensions into your project, you
need to also modify your composer.json file to include them, too:
"yiisoft/yii2-smarty": "~2.0.0",
"yiisoft/yii2-twig": "~2.0.0",
51
https://github.com/yiisoft/yii2-app-advanced/blob/master/docs/guide/
topic-shared-hosting.md
52
http://twig.sensiolabs.org/
53
http://www.smarty.net/
14.10. WORKING WITH THIRD-PARTY CODE 559
54
https://github.com/yiisoft/yii2-twig/tree/master/docs/guide
55
https://github.com/yiisoft/yii2-smarty/tree/master/docs/guide
56
https://getcomposer.org/
560 CHAPTER 14. SPECIAL TOPICS
If neither of the above is the case, it is likely that the library relies on PHP
include path configuration to correctly locate and include class files. Simply
follow its instruction on how to configure the PHP include path.
In the worst case when the library requires explicitly including every class
file, you can use the following method to include the classes on demand:
• Identify which classes the library contains.
• List the classes and the corresponding file paths in Yii::$classMap in
the entry script of the application. For example,
Yii::$classMap[’Class1’] = ’path/to/Class1.php’;
Yii::$classMap[’Class2’] = ’path/to/Class2.php’;
In case you would like to use only the database abstraction layer or other non-
asset related features of Yii, you should require a special composer package
that prevent Bower and NPM packages installation. See cebe/assetfree-yii258
for details.
See also the general section about installing Yii for more information on
Composer and solution to possible issues popping up during the installation.
Otherwise, you can download59 the Yii release file and unpack it in the
BasePath/vendor directory.
Next, you should modify the entry script of the 3rd-party system by
including the following code at the beginning:
require __DIR__ . ’/../vendor/yiisoft/yii2/Yii.php’;
As you can see, the code above is very similar to that in the entry script of
a typical Yii application. The only difference is that after the application
instance is created, the run() method is not called. This is because by calling
run(), Yii will take over the control of the request handling workflow which
is not needed in this case and already handled by the existing application.
Like in a Yii application, you should configure the application instance
based on the environment running the third-party system. For example,
to use the Active Record feature, you need to configure the db application
component with the DB connection setting used by the third-party system.
Now you can use most features provided by Yii. For example, you can
create Active Record classes and use them to work with databases.
Note: Yii 2 requires PHP 5.4 or above. You should make sure
that both your server and the existing application support this.
Because both Yii 1 and Yii 2 have the Yii class, you should create a custom-
ized version to combine them. The above code includes the customized Yii
class file, which can be created as follows.
$yii2path = ’/path/to/yii2’;
require $yii2path . ’/BaseYii.php’; // Yii 2.x
$yii1path = ’/path/to/yii1’;
require $yii1path . ’/YiiBase.php’; // Yii 1.x
That’s all! Now in any part of your code, you can use Yii::$app to access the
Yii 2 application instance, while Yii::app() will give you the Yii 1 application
instance:
echo get_class(Yii::app()); // outputs ’CWebApplication’
echo get_class(Yii::$app); // outputs ’yii\web\Application’
Create file composer.json under the micro-app directory using your favorite
editor and add the following:
{
"require": {
"yiisoft/yii2": "~2.0.0"
},
"repositories": [
{
"type": "composer",
"url": "https://asset-packagist.org"
}
]
}
Save the file and run the composer install command. This will install the
framework with all its dependencies.
require(__DIR__ . ’/../vendor/autoload.php’);
require(__DIR__ . ’/../vendor/yiisoft/yii2/Yii.php’);
Also create a file named config.php which will contain all application config-
uration:
<?php
return [
’id’ => ’micro-app’,
// the basePath of the application will be the ‘micro-app‘ directory
’basePath’ => __DIR__,
// this is where the application will find all controllers
’controllerNamespace’ => ’micro\controllers’,
// set an alias to enable autoloading of classes from the ’micro’
namespace
’aliases’ => [
’@micro’ => __DIR__,
],
];
Your project is now ready for coding. Although it’s up to you to decide the
project directory structure, as long as you observe namespaces.
namespace micro\controllers;
use yii\web\Controller;
If you want to use a different name for this controller you can change it
and configure yii\base\Application::$defaultRoute accordingly. For ex-
ample, for a DefaultController that would be ’defaultRoute’ => ’default/index
’.
At this point the project structure should look like this:
micro-app/|–
composer.json|–
config.php|–
14.11. USING YII AS A MICRO-FRAMEWORK 565
web/
|– index.php|–
controllers/
|– SiteController.php
If you have not set up the web server yet, you may want to take a look at web
server configuration file examples. Another options is to use the yii serve
command which will use the PHP build-in web server. You can run it from
the micro-app/ directory via:
vendor/bin/yii serve --docroot=./web
Opening the application URL in a browser should now print “Hello World!”
which has been returned in the SiteController::actionIndex().
Next we create a database migration to create a post table. Make sure you
have a separate configuration file as explained above, we need it to run the
console commands below. Running the following commands will create a
database migration file and apply the migration to the database:
vendor/bin/yii migrate/create --appconfig=config.php create_post_table --
fields="title:string,body:text"
vendor/bin/yii migrate/up --appconfig=config.php
Create directory models and a Post.php file in that directory. This is the code
for the model:
566 CHAPTER 14. SPECIAL TOPICS
<?php
namespace micro\models;
use yii\db\ActiveRecord;
namespace micro\controllers;
use yii\rest\ActiveController;
• Please also refer to the Looking Ahead section for further references.
568 CHAPTER 14. SPECIAL TOPICS
Chapter 15
Widgets
569
570 CHAPTER 15. WIDGETS
Helpers
16.1 Helpers
Note: This section is under development.
Yii provides many classes that help simplify common coding tasks, such as
string or array manipulations, HTML code generation, and so on. These
helper classes are organized under the yii\helpers namespace and are all
static classes (meaning they contain only static properties and methods and
should not be instantiated).
You use a helper class by directly calling one of its static methods, like
the following:
use yii\helpers\Html;
573
574 CHAPTER 16. HELPERS
• Inflector
• Json
• Markdown
• StringHelper
• Url
• VarDumper
namespace yii\helpers;
16.2 ArrayHelper
Additionally to the rich set of PHP array functions1 , the Yii array helper
provides extra static methods allowing you to deal with arrays more effi-
1
http://php.net/manual/en/book.array.php
16.2. ARRAYHELPER 575
ciently.
$array = [
’foo’ => [
’bar’ => new User(),
]
];
First method argument is where we’re getting value from. Second argument
specifies how to get the data. It could be one of the following:
• Name of array key or object property to retrieve value from.
• Set of dot separated array keys or object property names. The one
we’ve used in the example above.
• A callback returning a value.
The callback should be the following:
$fullName = ArrayHelper::getValue($user, function ($user, $defaultValue) {
return $user->firstName . ’ ’ . $user->lastName;
});
Third optional argument is default value which is null if not specified. Could
be used as follows:
$username = ArrayHelper::getValue($comment, ’user.username’, ’Unknown’);
$array = [
’key’ => [
’in’ => [’k’ => ’value’]
]
];
576 CHAPTER 16. HELPERS
After executing the code $array will contain [’options’ => [1, 2]] and $type
will be A. Note that unlike getValue method, remove supports simple key
names only.
$data2 = [
’username’ => ’Carsten’,
];
The result will be an associative array, where the key is the value of id
attribute:
[
’123’ => [’id’ => ’123’, ’data’ => ’abc’, ’device’ => ’laptop’],
’345’ => [’id’ => ’345’, ’data’ => ’hgi’, ’device’ => ’smartphone’]
// The second element of an original array is overwritten by the last
element because of the same id
]
]
]
Second argument that specifies keys to sort by can be a string if it’s a single
key, an array in case of multiple keys or an anonymous function like the
following one:
ArrayHelper::multisort($data, function($item) {
return isset($item[’age’]) ? [’age’, ’name’] : ’name’;
});
$array2 = [
’version’ => ’2.0’,
’ids’ => [
2,
],
’validDomains’ => new \yii\helpers\ReplaceArrayValue([
’yiiframework.com’,
’www.yiiframework.com’,
]),
’emails’ => [
’dev’ => new \yii\helpers\UnsetArrayValue(),
],
];
The first argument contains the data we want to convert. In our case we’re
converting a Post AR model.
The second argument is conversion mapping per class. We’re setting a
mapping for Post model. Each mapping array contains a set of mappings.
Each mapping could be:
• A field name to include as is.
• A key-value pair of desired array key name and model column name
to take value from.
• A key-value pair of desired array key name and a callback which returns
value.
The result of conversion above for single model will be:
[
’id’ => 123,
’title’ => ’test’,
’createTime’ => ’2013-01-01 12:00AM’,
’length’ => 301,
]
// true
ArrayHelper::isSubset(new ArrayObject([’a’, ’c’]), new ArrayObject([’a’, ’b’
, ’c’]));
16.3.1 Basics
Since building dynamic HTML by string concatenation can get messy very
fast, Yii provides a set of methods to manipulate tag options and build tags
based on these options.
Generating Tags
The code for generating a tag looks like the following:
<?= Html::tag(’p’, Html::encode($user->name), [’class’ => ’username’]) ?>
The first argument is the tag name. The second one is the content to be
enclosed between the start and end tags. Note that we are using Html::encode
— that’s because the content isn’t encoded automatically to allow
using HTML when needed. The third one is an array of HTML options,
or in other words, tag attributes. In this array the key is the name of the
attribute (such as class, href or target), and the value is its value.
The code above will generate the following HTML:
5
http://php.net/manual/en/function.in-array.php
6
http://php.net/manual/en/language.basic-syntax.phpmode.php
584 CHAPTER 16. HELPERS
<p class="username">samdark</p>
In case you need just an opening or closing tag, you can use the Html::
beginTag() and Html::endTag() methods.
Options are used in many methods of the Html helper and various wid-
gets. In all these cases there is some extra handling to know about:
• If a value is null, the corresponding attribute will not be rendered.
• Attributes whose values are of boolean type will be treated as boolean
attributes7 .
• The values of attributes will be HTML-encoded using Html::encode().
• If the value of an attribute is an array, it will be handled as follows:
– If the attribute is a data attribute as listed in yii\helpers\Html
::$dataAttributes, such as data or ng, a list of attributes will be
rendered, one for each element in the value array. For example, ’
data’ => [’id’ => 1, ’name’ => ’yii’] generates data-id="1" data-
name="yii"; and ’data’ => [’params’ => [’id’ => 1, ’name’ => ’yii
’], ’status’ => ’ok’] generates data-params=’{"id":1,"name":"yii"
}’ data-status="ok". Note that in the latter example JSON format
is used to render a sub-array.
– If the attribute is NOT a data attribute, the value will be JSON-
encoded. For example, [’params’ => [’id’ => 1, ’name’ => ’yii’]
generates params=’{"id":1,"name":"yii"}’.
You may specify multiple CSS classes using the array style as well:
$options = [’class’ => [’btn’, ’btn-default’]];
7
http://www.w3.org/TR/html5/infrastructure.html#boolean-attributes
16.3. HTML HELPER 585
You may also use the array style when adding or removing classes:
$options = [’class’ => ’btn’];
If the CSS class option is specified using the array style, you may use a
named key to mark the logical purpose of the class. In this case, a class with
the same key in the array style will be ignored in Html::addCssClass():
$options = [
’class’ => [
’btn’,
’theme’ => ’btn-default’,
]
];
CSS styles can be set up in similar way using the style attribute:
$options = [’style’ => [’width’ => ’100px’, ’height’ => ’100px’]];
$decodedUserName = Html::decode($userName);
16.3.2 Forms
Dealing with form markup is quite repetitive and error prone. Because of
that, there is a group of methods to help dealing with them.
Creating Forms
Forms can be opened with beginForm() method like the following:
<?= Html::beginForm([’order/update’, ’id’ => $id], ’post’, [’enctype’ => ’
multipart/form-data’]) ?>
The first argument is the URL the form will be submitted to. It can be
specified in the form of a Yii route and parameters accepted by Url::to().
The second one is the method to use. post is the default. The third one is an
array of options for the form tag. In this case we’re changing the encoding of
the form data in the POST request to multipart/form-data, which is required
in order to upload files.
Closing the form tag is simple:
<?= Html::endForm() ?>
Buttons
In order to generate buttons, you can use the following code:
<?= Html::button(’Press me!’, [’class’ => ’teaser’]) ?>
<?= Html::submitButton(’Submit’, [’class’ => ’submit’]) ?>
<?= Html::resetButton(’Reset’, [’class’ => ’reset’]) ?>
8
http://www.php.net/manual/en/function.htmlspecialchars.php
9
http://www.php.net/manual/en/function.htmlspecialchars-decode.php
16.3. HTML HELPER 587
The first argument for all three methods is the button title, and the second
one is an array of options. The title isn’t encoded, so if you’re displaying
data from the end user, encode it with Html::encode().
Input Fields
There are two groups of input methods. The ones starting with active, which
are called active inputs, and the ones not starting with it. Active inputs take
data from the model and attribute specified, while in the case of a regular
input, data is specified directly.
The most generic methods are:
type, input name, input value, options
<?= Html::input(’text’, ’username’, $user->name, [’class’ => $username]) ?>
If you know the input type in advance, it’s more convenient to use the short-
cut methods:
• yii\helpers\Html::buttonInput()
• yii\helpers\Html::submitInput()
• yii\helpers\Html::resetInput()
• yii\helpers\Html::textInput(), yii\helpers\Html::activeTextInput()
• yii\helpers\Html::hiddenInput(), yii\helpers\Html::activeHiddenInput()
• yii\helpers\Html::passwordInput() / yii\helpers\Html::activePasswordInput()
• yii\helpers\Html::fileInput(), yii\helpers\Html::activeFileInput()
• yii\helpers\Html::textarea(), yii\helpers\Html::activeTextarea()
Radios and checkboxes are a bit different in terms of method signature:
<?= Html::radio(’agree’, true, [’label’ => ’I agree’]) ?>
<?= Html::activeRadio($model, ’agree’, [’class’ => ’agreement’]) ?>
Dropdown lists and list boxes can be rendered like the following:
<?= Html::dropDownList(’list’, $currentUserId, ArrayHelper::map($userModels,
’id’, ’name’)) ?>
<?= Html::activeDropDownList($users, ’id’, ArrayHelper::map($userModels, ’id
’, ’name’)) ?>
The first argument is the name of the input, the second one is the value
that’s currently selected, and the third one is an array of key-value pairs,
where the array key is the list value and the array value is the list label.
If you want multiple choices to be selectable, you can use a checkbox list:
588 CHAPTER 16. HELPERS
// post-title
echo Html::getInputId($post, ’title’);
// my first post
echo Html::getAttributeValue($post, ’title’);
// $post->authors[0]
echo Html::getAttributeValue($post, ’[0]authors[0]’);
In the above, the first argument is the model, while the second one is the
attribute expression. In its simplest form the expression is just an attribute
name, but it can be an attribute name prefixed and/or suffixed with array
indexes, which is mainly used for tabular input:
• [0]content is used in tabular data input to represent the content attrib-
ute for the first model in tabular input;
16.3. HTML HELPER 589
Gives you
Gives you
<script defer>alert("Hello!");</script>
generates
<!--[if IE 5]>
<link href="http://example.com/css/ie5.css" />
<![endif]-->
The first argument is the URL. The second one is an array of options. In
addition to the regular options, you can specify:
• condition to wrap <link in conditional comments with the specified
condition. Hope you won’t need conditional comments ever ;)
• noscript can be set to true to wrap <link in a <noscript> tag so it will be
included only when there’s either no JavaScript support in the browser
or it was disabled by the user.
To link a JavaScript file:
<?= Html::jsFile(’@web/js/main.js’) ?>
Same as with CSS, the first argument specifies the URL of the file to be
included. Options can be passed as the second argument. In the options you
can specify condition in the same way as in the options for cssFile.
590 CHAPTER 16. HELPERS
16.3.4 Hyperlinks
There’s a method to generate hyperlinks conveniently:
<?= Html::a(’Profile’, [’user/view’, ’id’ => $id], [’class’ => ’profile-link
’]) ?>
The first argument is the title. It’s not encoded, so if you’re using data
entered by the user, you need to encode it with Html::encode(). The second
argument is what will be in the href attribute of the <a tag. See Url::to()
for details on what values it accepts. The third argument is an array of tag
attributes.
If you need to generate mailto links, you can use the following code:
<?= Html::mailto(’Contact us’, ’[email protected]’) ?>
16.3.5 Images
In order to generate an image tag, use the following:
<?= Html::img(’@web/images/logo.png’, [’alt’ => ’My logo’]) ?>
generates
Besides aliases, the first argument can accept routes, parameters and URLs,
in the same way Url::to() does.
16.3.6 Lists
Unordered list can be generated like the following:
<?= Html::ul($posts, [’item’ => function($item, $index) {
return Html::tag(
’li’,
$this->render(’post’, [’item’ => $item]),
[’class’ => ’post’]
);
}]) ?>
$relativeHomeUrl = Url::home();
$absoluteHomeUrl = Url::home(true);
$httpsAbsoluteHomeUrl = Url::home(’https’);
The only parameter of the method works exactly the same as for Url::home().
You may specify the route as a string, e.g., site/index. You may also use an
array if you want to specify additional query parameters for the URL being
created. The array format must be:
// generates: /index.php?r=site%2Findex¶m1=value1¶m2=value2
[’site/index’, ’param1’ => ’value1’, ’param2’ => ’value2’]
If you want to create a URL with an anchor, you can use the array format
with a # parameter. For example,
// generates: /index.php?r=site%2Findex¶m1=value1#name
[’site/index’, ’param1’ => ’value1’, ’#’ => ’name’]
// /index.php?r=site%2Findex
echo Url::toRoute(’site/index’);
// /index.php?r=site%2Findex&src=ref1#name
echo Url::toRoute([’site/index’, ’src’ => ’ref1’, ’#’ => ’name’]);
// http://www.example.com/index.php?r=site%2Findex
echo Url::toRoute(’site/index’, true);
// https://www.example.com/index.php?r=site%2Findex
echo Url::toRoute(’site/index’, ’https’);
// /index.php?r=site%2Findex&src=ref1#name
echo Url::to([’site/index’, ’src’ => ’ref1’, ’#’ => ’name’]);
// /images/logo.gif
echo Url::to(’@web/images/logo.gif’);
// images/logo.gif
echo Url::to(’images/logo.gif’);
16.4. URL HELPER 593
// http://www.example.com/images/logo.gif
echo Url::to(’@web/images/logo.gif’, true);
// https://www.example.com/images/logo.gif
echo Url::to(’@web/images/logo.gif’, ’https’);
// /index.php?r=post%2Fview&id=123&src=google
echo Url::current();
// /index.php?r=post%2Fview&id=123
echo Url::current([’src’ => null]);
// /index.php?r=post%2Fview&id=100&src=google
echo Url::current([’id’ => 100]);
In the next request we can get URL remembered in the following way:
$url = Url::previous();
$productUrl = Url::previous(’product’);