Yii 2.0.2

    The PHP framework team is pleased to announce the release of version 2.0.2. Installation and upgrade instructions can be found at http://www.yiiframework.com/download/ .

    Version 2.0.2 is a Yii 2.0 patch release and contains about 40 minor improvements and fixes. A complete list of changes can be found on GitHub . Thanks to everyone who helped us prepare this release.

    If you want to follow the Yii 2 development process, you can put an asterisk or use the watch button on the project page on GitHub . We also have Twitter and Facebook .

    Consider the most significant additions to this release.



    Route Aliases



    Prior to 2.0.2, Yii supported path aliases and URLs. You can now set aliases for routes. After setting the route alias, you can use it when creating URLs using the methods Url::to()and Url::toRoute():

    use yii\helpers\Url;
    Yii::setAlias('@posts', 'post/index');
    // /index.php?r=post/index
    echo Url::to(['@posts']);
    echo Url::toRoute('@posts');
    


    This feature is useful if changes to the route structure are expected. Using an alias does not have to change the URL creation code.

    Configuring Dependent Components



    The properties of many components take the ID of other components, such as yii\caching\DbCache::dbor yii\web\CacheSession::cache. Sometimes, in order not to create a new component for unit testing, you may need to set this property using the configuration array:

    $cache = Yii::createObject([
        'class' => 'yii\caching\DbCache',
        'db' => [
            'class' => 'yii\db\Connection',
            'dsn' => '...',
        ],
    ]);
    


    If you are developing a new class dependent on external components, you can use the following code to provide a similar opportunity:

    use yii\base\Object;
    use yii\db\Connection;
    use yii\di\Instance;
    class MyClass extends Object 
    {
        public $db = 'db';
        public function init() 
        {
            $this->db = Instance::ensure($this->db, Connection::className());
        }
    }
    


    The code above allows you to configure the property with dbone of the following values:

    • a line with the application component ID;
    • class object yii\db\Connection;
    • an array of configurations that can be used to instantiate yii\db\Connection.


    Immutable slug



    If you use yii\behaviors\SluggableBehavior, now you can assign a immutablevalue to the new property true. In this case, the once created slug will not change when the model is saved again. This is useful for SEO: once indexed content will remain at the same URL.

    Automatically select an alternate language with a DatePicker widget



    The widget yii\jui\DatePickernow automatically selects an alternative language if the specified language is not found. This is useful when you set the property languageas the locale ID that contains the region and / or variant. For example, if you set languageto de-DEand the widget does not find a language file /ui/i18n/datepicker-de-DE.js, then the language deand file will be automatically used /ui/i18n/datepicker-de.js.

    Validation Error Transfer



    The class yii\base\Modelnow contains a method addErrors()that allows you to pass validation errors from one model to another. For example, if you have a form class for the ActiveRecord model and you need to pass form validation errors to the ActiveRecord model, you can do this like this:

    use yii\base\Model;
    use yii\db\ActiveRecord;
    class MyForm extends Model 
    {
        public $model;
        public function process()
        {
            // ...
            if (!$this->validate()) {
                $this->model->addErrors($this->getErrors());
                // ....
            }
        }
    }
    

    Also popular now: