During Yii development, we often encounter a problem. every time we use Yii components and extensions, we will write them as follows:
During Yii development, we often encounter a problem. every time we use Yii components and extensions, we will write them as follows:
user; Yii::app()->getClientScript(); Yii::app()->params['name'];...
This makes it inconvenient to Use. on yiiFramework, I saw a Wiki Use cut functions to reduce typing to solve this problem, because the above English is very simple and I will not translate it, here I will only describe how to create a global file globals. php puts the shortcut function in this global file. Put the global file under the protected folder, and then introduce it in the 'index. php' file.
required('path/to/globals.php');
I recommend that you introduce it in the configuration file main. php'. to make the console application usable, the configuration file console. php' can be changed
Return CMap: mergeArray (require ('main. php'), array (// console configuration ...));
The following are some shortcut functions on the official website and some of my frequently used functions:
ClientScript ** @ return CClientScript */function cs () {return Yii: app ()-> getClientScript ();}/*** This is the actual cut to Yii :: app ()-> user. ** @ return CWebUser */function user () {return Yii: app ()-> getUser ();}/*** this is the actual cut to Yii :: app ()-> createUrl (). ** @ param string $ route * @ param array $ params * @ param string $ ampersand * @ return string */function url ($ route, $ params = Array (), $ ampersand = '&') {return Yii: app ()-> createUrl ($ route, $ params, $ ampersand );} /*** This is the actual cut to CHtml: encode ** @ param string $ text * @ return string */function h ($ text) {return htmlspecialchars ($ text, ENT_QUOTES, Yii: app ()-> charset);}/*** This is the actual cut to CHtml: link (). ** @ param string $ text * @ param string | array $ url * @ param array $ htmlOptions * @ return s Tring */function l ($ text, $ url = '#', $ htmlOptions = array () {return CHtml: link ($ text, $ url, $ htmlOptions);}/*** This is the actual cut to Yii: t () with default category = 'Stay' ** @ param string $ message * @ param string $ category * @ param array $ params * @ param string $ source * @ param string $ language *@ return string */function t ($ message, $ category = 'stay', $ params = array (), $ source = Null, $ language = null) {return Yii: t ($ category, $ message, $ params, $ source, $ language );} /*** This is the actual cut to Yii: app ()-> request-> baseUrl * If the parameter is given, it will be returned and prefixed with the app baseUrl. ** @ param string $ url * @ return string */function bu ($ url = null) {static $ baseUrl; if ($ baseUrl = null) $ baseUrl = Yii:: app ()-> getRequest ()-> getBaseUrl (); ret Urn $ url === null? $ BaseUrl: $ baseUrl. '/'. ltrim ($ url, '/');}/*** Returns the named application parameter. * This is the actual cut to Yii: app ()-> params [$ name]. ** @ param string $ name * @ return mixed */function param ($ name) {return Yii: app ()-> params [$ name];} // The following is the/*** formatting function I used. this is a shortcut function of Yii: app ()-> format. how to use it ** * format($timestamp,'Datetime'); *
** @ Param mixed $ value * @ param string $ type the formatting type, such as 'datetime', 'Time '.., for details, see CFormatter * @ return mixed */function format ($ value, $ type) {return Yii: app ()-> format ($ value, $ type );} /*** This is a function for loading models, if no model is found, the system will throw a 404 error ** @ param string $ modelName * @ param integer $ pk * @ param string $ errorMsg * @ return CActiveRecord */function load_model ($ modelName, $ pk, $ errorMsg = null) {$ model = $ ModelName: model ()-> findByPk (int) $ pk); if ($ model! = Null) {return $ model;} else {throw new CHttpException (404, $ errorMsg );}}
Download source code: Click to download
You can also add