- Crypto Library Change
- RBAC Caching
- Page Caching
- Cache Busting for Assets
- Modifying Current URL
- Disabling Log Rotation
- Data Attributes
- Trimming Input
- Max Length of Input
- Configurable Objects
We are very pleased to announce the release of Yii Framework version 2.0.3. Please refer to the instructions at http://www.yiiframework.com/download/ to install or upgrade to this version.
Version 2.0.3 is a patch release of Yii 2.0 which contains about 50 minor new features and bug fixes. Complete list of changes may be found in the change log. We hereby thank to all contributors who have spent their precious time helping improve Yii and made this release possible.
Additional thanks goes to contributors improving documentation and translating into many languages.
You may follow the development progress of Yii 2 by starring or watching Yii 2.0 GitHub Project. You may also follow Yii Twitter feeds or join Yii Facebook group to connect with other Yii developers.
Below we summarize some of the most important features included in this release.
Crypto Library Change ¶
This is a big change mainly internally. We have replaced Mcrypt (which has not been touched by its authors for 8 years) with OpenSSL in yii\base\Security
. Thanks to Tom Worster for his excellent job in keeping the high security standard for Yii. Because OpenSSL is built into PHP by default, you should not experience any backward-compatibility issue. But if you do, please let us know.
RBAC Caching ¶
If you are using database to store RBAC data, you may find its performance is not ideal as each access check would involve the execution of many SQL statements. To improve the performance, a caching mechanism is now implemented for yii\rbac\DbManager
. It stores the whole RBAC hierarchy in cache and thus greatly improves the performance of checkAccess()
. By default, RBAC caching is not enabled. You may enable it by configuring yii\rbac\DbManager
in the application configuration, like the following:
return [
'components' => [
'authManager' => [
'class' => 'yii\rbac\DbManager',
'cache' => 'cache', // this enables RBAC caching
],
'cache' => [
'class' => 'yii\caching\ApcCache',
]
// ...
],
]
Page Caching ¶
Previously, page caching is limited to caching HTML content only. If you attempt to use it to cache RESTful responses, you may find it won't work due to incorrect response format. With the enhancement introduced in this release, you can now use yii\filters\PageCache
to cache different kinds of response data as well as response headers. For example, in a RESTful controller class, you can cache an index
action as follows,
public function behaviors()
{
return [
[
'class' => 'yii\filters\PageCache',
'only' => ['index'],
'duration' => 60,
],
];
}
Cache Busting for Assets ¶
Yet another enhancement related with caching is the support for cache busting of published assets. It often happens in production servers that you enable HTTP caching for JS or CSS files. A drawback is that if you make changes to these files, the client side may still use the old version due to HTTP caching. You can now configure yii\web\AssetManager::appendTimestamp
to enable cache busting of published JS and CSS files. By setting this property to be true, the file modification time of the corresponding JS or CSS file will be appended to each JS/CSS URL. Therefore, whenever the file is modified, the client side will always receive the latest version:
return [
'components' => [
'assetManager' => [
'class' => 'yii\web\AssetManager',
'appendTimestamp' => true,
],
// ...
],
]
Modifying Current URL ¶
A new helper method yii\helpers\Url::current()
is added to help you more easily modify the currently requested URL by adding or removing some of the GET parameters. For example,
// assume $_GET = ['id' => 123, 'src' => 'google'], current route is "post/view"
// /index.php?r=post/view&id=123&src=google
echo Url::current();
// /index.php?r=post/view&id=123
echo Url::current(['src' => null]);
// /index.php?r=post/view&id=100&src=google
echo Url::current(['id' => 100]);
Disabling Log Rotation ¶
If you keep logs in files by using yii\log\FileTarget
, you can now disable the automatic log rotation with a single property configuration like the following. This is mainly useful when you are using external tools to rotate log files.
return [
'components' => [
'log' => [
'targets' => [
[
'class' => 'yii\log\FileTarget',
'enableRotation' => false,
],
],
],
],
];
Data Attributes ¶
When you are using yii\helpers\Html
to generate HTML tags, the data
attribute is specially handled. For example,
// displays: <div data-name="xyz" data-age="20"></div>
echo Html::tag('div', '', ['data' => ['name' => 'xyz', 'age' => 20]]);
Now the ng
attribute and data-ng
attributes are also specially handled in this way. This is mainly useful when you are working with AngularJS. For all other attributes, an array will be turned into JSON format when being embedded into an HTML tag.
You can customize which attributes should be specially handled as shown above by modifying the yii\helpers\Html::$dataAttributes
variable.
Trimming Input ¶
If you are using the trim
validation rule, you may find trimming is now done on the client side too. That is if a text input is associated with a trim
validation rule, the surrounding blanks in the input data will be trimmed automatically upon the input losing focus. If you do not like this new behavior, you can set the enableClientValidation
option of the trim
rule to be false.
Max Length of Input ¶
When using yii\helpers\Html::activeTextInput()
or yii\widgets\ActiveField::textInput()
to create a text input, you may want to specify the maxlength
attribute of the generated text input. If you happen to have a string
validation rule associated with the corresponding model attribute, you may want to avoid explicitly specifying the value of maxlength
because this may already be specified in the string
rule. You can achieve this goal by setting maxlength
to be true and it will be automatically populated with the value of the max
option of the string
rule. For example,
// assume "name" has a validation rule: ['name', 'string', 'max' => 128]
// generates: <input type="text" ... maxlength="128">
echo Html::activeTextInput($model, 'name', ['maxlength' => true]);
Configurable Objects ¶
A new interface named yii\base\Configurable
can now be used to declared a class as configurable. If a class implements this interface, the yii\di\Container
will assume the class constructor's last parameter accepts a configuration array. For example,
class Foo implements \yii\base\Configurable
{
public function __construct($a, $b, $config = [])
{
}
}
$container = new \yii\di\Container;
$object = $container->get('Foo', [1, 2], ['prop1' => 3]);
// equivalent to: $object = new Foo(1, 2, ['prop1' => 3]);
Previously before this release, you would have to extend from yii\base\Object
in order for a class to be treated like this.