📜 ⬆️ ⬇️

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 development process of Yii 2, 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 and URL aliases. Now you can 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 there are changes in the structure of routes. When using a pseudonym, you don’t have to change the URL creation code.

Configuring dependent components



The properties of many components accept the ID of other components, such as yii\caching\DbCache::db or yii\web\CacheSession::cache . Sometimes in order not to create a new component for unit testing, you may need to set such a 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 that is dependent on external components, you can use the following code to provide a similar feature:

 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 db property with one of the following values:



Fixed slug



If you use yii\behaviors\SluggableBehavior , you can now set the new immutable property to 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.

Automatic selection of an alternative language by the DatePicker widget



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

Passing Validation Errors



The yii\base\Model class now contains the addErrors() method, which allows you to pass the validation errors of 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 it 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()); // .... } } } 

Source: https://habr.com/ru/post/247741/


All Articles