Yii 2.0.3

    Just in time, the release of the PHP Yii framework version 2.0.3 was released. The procedure for updating and installing is described at http://www.yiiframework.com/download/ .

    This version includes about 50 improvements and fixes. The full list can be viewed on GitHub . The team takes this opportunity to thank everyone who helps us .

    Special thanks to those who improve the documentation and translate it into many languages.

    It is convenient to follow the development process of the framework on GitHub by putting an asterisk on the project or using the watch button. Subscribe to our Twitter and Facebook .

    Below we will consider the most important improvements of this version.



    Cryptography



    This is a pretty serious change, albeit an internal one. For yii\base\Securitywe replaced Mcrypt (which has been abandoned for eight years) with OpenSSL, for which it is worth saying thanks to Tom Worster, who looks after Yii's security. Since OpenSSL is present by default in most PHP distributions, compatibility issues should not arise. But if you have any, be sure to let me know.

    RBAC Caching



    If you store RBAC data in a database, you might notice that the performance is not ideal: each check results in a fairly significant number of SQL queries. In order to fix this, we added caching to yii\rbac\DbManager. The entire hierarchy is stored in the cache, which greatly increases productivity checkAccess(). By default, RBAC caching is disabled. It is included as follows:

    return [
        'components' => [
            'authManager' => [
                'class' => 'yii\rbac\DbManager',
                'cache' => 'cache',   // <---- вот так
            ],
            'cache' => [
                'class' => 'yii\caching\ApcCache',
            ]
            // ...
        ],
    ]
    


    Page Caching



    Previously, page caching was limited only to HTML. It did not work for the REST API due to invalid HTTP headers. Now you can use yii\filters\PageCacheto cache various types of data and headers. This is how you can cache the action of a indexREST controller:

    public function behaviors()
    {
        return [
            [
                'class' => 'yii\filters\PageCache',
                'only' => ['index'],
                'duration' => 60,
            ],
        ];
    }
    


    Forced loading of fresh resources



    Another caching improvement is support for forced loading of fresh resources, which is often useful on production servers when HTTP caching of JS or CSS files is enabled. In this case, even if you make changes to the resource files, the client may retrieve the old version from the cache. Now you can set the property yii\web\AssetManager::appendTimestampto true. File modification time will be added to the JS and CSS resource URLs, so the client will always get the latest version:

    return [
        'components' => [
            'assetManager' => [
                'class' => 'yii\web\AssetManager',
                'appendTimestamp' => true,
            ],
            // ...
        ],
    ]
    


    Changes to the current URL



    A new method has been added yii\helpers\Url::current()that facilitates the task of changing the current URL by adding or removing GET parameters:

    // допустим, $_GET = ['id' => 123, 'src' => 'google'], текущий маршрут "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]);
    


    Turn off log rotation



    If you write logs to files through yii\log\FileTarget, now you can turn off automatic file rotation, which is useful if additional utilities do this:

    return [
        'components' => [
            'log' => [
                'targets' => [
                    [
                        'class' => 'yii\log\FileTarget',
                        'enableRotation' => false,
                    ],
                ],
            ],
        ],
    ];
    


    Data attributes



    When used, the yii\helpers\Htmlattribute is dataprocessed in a special way:

    // выдаёт: 
    echo Html::tag('div', '', ['data' => ['name' => 'xyz', 'age' => 20]]);


    Now, as processed ngand data-ng. This is useful primarily for those who work with AngularJS. For other attributes, the array will be converted to JSON.

    You can specify which attributes to process in a special way by adding them to yii\helpers\Html::$dataAttributes.

    Input Trimming



    If you use the validation rule trim, you will find that pruning is now done on the client as well. You can disable this behavior by setting enableClientValidationthe validation rule property trimto false.

    Maximum field length



    When using yii\helpers\Html::activeTextInput()or yii\widgets\ActiveField::textInput()to create input fields, you may need to set a property maxlength. If the corresponding model field has a validation rule string, maxlengthyou can take it from it. To do this maxlength, set the value to true:

    // у "name" есть правило валидации: ['name', 'string', 'max' => 128]
    // generates: 
    echo Html::activeTextInput($model, 'name', ['maxlength' => true]);
    


    Configurable objects



    Now you can use the new interface yii\base\Configurableto mark the class as “configurable”. In this case yii\di\Container, it waits for the constructor to accept the configuration array:

    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]);
    // эквивалентно: $object = new Foo(1, 2, ['prop1' => 3]);
    


    Previously, it was necessary to inherit from yii\base\Objectto achieve a similar effect.

    Also popular now: