Because Yii intends to be integrated nicely with third-party libraries, it does not define any global functions. Everything in Yii needs to be addressed with full class name or object scopes. For example, to access the current user, we need to use Yii::app()->user; to access application parameters, we need Yii::app()->params['name']; and so on. While editors like textmate can help alleviate the problem of these lengthy typings, it is worthwhile to define global shortcut functions to some commonly used method calls. They will make the application code look cleaner.
We may save the definition to these shortcut functions in a file named globals.php under the protected directory. Then, in the entry script index.php, we include the file at the beginning:
require('path/to/globals.php'); require('path/to/yii.php'); ......
Now, we can use these global functions anywhere in our application code. For example, to access the current user, we can use user(), instead of the lengthy Yii::app()->user.
Note: Do not use these shortcut functions in components that you intend to release as reusable extensions. Doing so would make your components unusable if the shortcut functions used are not defined in a different application. Also pay attention if the shortcut functions cause conflict with third-party libraries used in the application.
Below is the code containing some of the most commonly used shortcut functions. Feel free to adjust it according to your taste.
/** * This is the shortcut to DIRECTORY_SEPARATOR */ defined('DS') or define('DS',DIRECTORY_SEPARATOR); /** * This is the shortcut to Yii::app() */ function app() { return Yii::app(); } /** * This is the shortcut to Yii::app()->clientScript */ function cs() { // You could also call the client script instance via Yii::app()->clientScript // But this is faster return Yii::app()->getClientScript(); } /** * This is the shortcut to Yii::app()->user. */ function user() { return Yii::app()->getUser(); } /** * This is the shortcut to Yii::app()->createUrl() */ function url($route,$params=array(),$ampersand='&') { return Yii::app()->createUrl($route,$params,$ampersand); } /** * This is the shortcut to CHtml::encode */ function h($text) { return htmlspecialchars($text,ENT_QUOTES,Yii::app()->charset); } /** * This is the shortcut to CHtml::link() */ function l($text, $url = '#', $htmlOptions = array()) { return CHtml::link($text, $url, $htmlOptions); } /** * This is the shortcut to Yii::t() with default category = 'stay' */ function t($message, $category = 'stay', $params = array(), $source = null, $language = null) { return Yii::t($category, $message, $params, $source, $language); } /** * This is the shortcut to Yii::app()->request->baseUrl * If the parameter is given, it will be returned and prefixed with the app baseUrl. */ function bu($url=null) { static $baseUrl; if ($baseUrl===null) $baseUrl=Yii::app()->getRequest()->getBaseUrl(); return $url===null ? $baseUrl : $baseUrl.'/'.ltrim($url,'/'); } /** * Returns the named application parameter. * This is the shortcut to Yii::app()->params[$name]. */ function param($name) { return Yii::app()->params[$name]; }
Total 6 comments
You can also include it in your config file, instead of modifying all your bootstrap files.
If you add PHPDoc tags, especially @return, to these shortcuts then your IDE (PhpStorm, Netbeans, Eclipse, etc.) knows how to validate and code complete your code that uses them.
My version of this file does that and it's really handy.
I use the following additional session shortcut functions.
A useful one that I use in development is the following which dumps the target with syntax highlighting on by default:
If you use t(), then forget about using yiic message to extract messages because it assumes that message category is the first parameters (like with yii::t) ..with t(), category is the second parameter.
Leave a comment
Please login to leave your comment.