Difference between #1 and #8 of
Use shortcut functions to reduce typing

Changes

Title unchanged

Use shortcut functions to reduce typing

Category unchanged

Tutorials

Yii version unchanged

Tags unchanged

Content changed

[...]
......
```

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
thatso 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.
[...]
function cs()
{
// You could also call the client script instance via Yii::app()->clientScript
 
// But this is faster
 
return Yii::app()->cgetClientScript();
 
}
 
 
/**
 
 * This is the shortcut to Yii::app()->user.
 
 */
 
function user() 
 
{
 
return Yii::app()->getUser()
;
}
[...]
* This is the shortcut to CHtml::link()
*/
function l
ink($text, $url = '#', $htmlOptions = array())
{
return CHtml::link($text, $url, $htmlOptions);
[...]
static $baseUrl;
if ($baseUrl===null)
$baseUrl=Yii::app()->
rgetRequest->b()->getBaseUrl();
return $url===null ? $baseUrl : $baseUrl.'/'.ltrim($url,'/');
}
[...]
return Yii::app()->params[$name];
}

 
/**
 
 * This is the shortcut to Yii::app()->user.
 
 */
 
function user() 
 
{
 
return Yii::app()->user;
 
}
 
```
 
```
 
 
### Links
 
 
- [Russian Version](http://dbhelp.ru/global-yii-functions/page/)
46 0
45 followers
Viewed: 66 730 times
Version: 1.1
Category: Tutorials
Tags:
Written by: qiang
Last updated by: Yang He
Created on: May 8, 2009
Last updated: 11 years ago
Update Article

Revisions

View all history