Use shortcut functions to reduce typing

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];
}
Links
46 0
45 followers
Viewed: 68 095 times
Version: 1.1
Category: Tutorials
Tags:
Written by: qiang
Last updated by: Yang He
Created on: May 8, 2009
Last updated: 12 years ago
Update Article

Revisions

View all history