📜 ⬆️ ⬇️

Yii 1.1.8

The release of the PHP framework Yii 1.1.8, which includes more than 80 bugfixes, new features and improvements. Many thanks to those who reportable bugs, offered new features and used Orphus on yiiframework.ru as intended .

You can pick up a fresh distribution from the official site .

Upgrade instructions can be read in UPGRADE .
')
The full list of changes, as usual, can be read in CHANGELOG , we will briefly review the most interesting.



Custom Classes for URL Rules



For complex work with URLs that the standard CUrlRule class cannot handle (for example, when part of the URL depends on values ​​in the database), you can write your own classes for handling URLs and use them in the CUrlManager configuration:

 array( //   '/login' → 'site/login'  .. '<action:(login|logout|about)>' => 'site/<action>', //    '//' array( 'class' => 'application.components.CarUrlRule', 'connectionID' => 'db', ), //    'post/update' '<controller:\w+>/<action:\w+>' => '<controller>/<action>', ), 


The class itself is inherited from CBaseUrlRule and looks like this:

 class CarUrlRule extends CBaseUrlRule { public $connectionID = 'db'; public function createUrl($manager,$route,$params,$ampersand) { if ($route==='car/index') { if (isset($params['manufacturer'], $params['model'])) return $params['manufacturer'] . '/' . $params['model']; else if (isset($params['manufacturer'])) return $params['manufacturer']; } return false; //    } public function parseUrl($manager,$request,$pathInfo,$rawPathInfo) { if (preg_match('%^(\w+)(/(\w+))?$%', $pathInfo, $matches)) { //   $matches[1]  $matches[3]  //     ? //  ,  $_GET['manufacturer'] / $_GET['model'] //   'car/index' } return false; //    } } 


Improved class autoloader



Yii already has a class loader that connects them as soon as they are needed. Now it does not conflict with third-party loaders and can be performed both before and after. In previous versions, it was always executed after third-party autoloaders, which led to unnecessary calls when using the Yii classes:

 //        Yii Yii::registerAutoloader($autoloader); 


Now you can do this:

 //   true        Yii Yii::registerAutoloader($autoloader, true); 


By default, the Yii autoloader uses the PHP include path. For some hosting companies, this is a problem that can be solved by adjusting index.php bit:

 require('path/to/yii.php'); //  PHP include path Yii::$enableIncludePath = false; Yii::createWebApplication($config)->run(); 


Real-time logging



Some console applications can run for very long. It is useful for them to write messages to the log immediately and not to wait for the application to finish:

 //       Yii::getLogger()->autoFlush = 1; //       Yii::getLogger()->autoDump = true; 


We work with counters in the database via AR



The CActiveRecord class has a new saveCounters() method, similar to the existing CActiveRecord::updateCounters() . The main difference is that saveCounters() works only with the current object while updateCounters() works with the entire table:

 $post = Post::model()->findByPk(1); //     $post->saveCounters(array('views'=>1)); //      Post::model()->updateCounters(array('views'=>1), 'id=1'); 


Generate Translation Files



When using a yiic message it was often necessary to delete the old file and replace it with a new one that was generated next. Now you can write directly to the old file, if you set the overwrite option to true in the command configuration.

Creating URLs in console applications



In web applications, CUrlManager is usually used to generate a URL, which was previously not available to console applications. Now both in web applications and in console you can use Yii::app()->createUrl() , which is very convenient, for example, to build a sitemap that lists the URL of a web application.

In clips, you can now use parameters



This is useful when a piece of code in view is repeated many times, but it is not sufficiently complex to carry it into a separate file:

 <?php //  clip ?> <?php $this->beginClip('hello')?> <p>, {username}!</p> <?php $this->endClip() ?> <?php //  clip ?> <?php $this->renderClip('hello',array( '{username}'=>'Qiang', ))?> <?php $this->renderClip('hello',array( '{username}'=>'Alex', ))?> <?php $this->renderClip('hello',array( '{username}'=>'Michael', ))?> 

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


All Articles