This article describes the usage of Yii global function. Share it for your reference, as follows:
Since YII is committed to perfectly integrating third-party libraries, it does not define any global functions. Every application in yii requires a full category and object range.
For example,Yii::app()->user;Yii::app()->params['name'];etc. We can set global functions by ourselves to make the code look more concise and easy to use.
We can save it in the protected/config directory. Then, in the entry script, define the following:
$globals=dirname(__FILE__).'/protected/config/'; ... require_once($yii); require_once($globals);
Now we can use our global function anywhere in the application, for example, you can use user() instead of Yii::app()->user. Note:If you plan to publish a reusable component, please do not use global functions in the component. In different application configurations, it may not be used. At the same time, you should also pay attention to conflicts with third-party libraries. You can consider adding your own prefix to each function and making a distinction. For example, the framework core is prefixed by C.
Here are some of the most commonly used shortcut features. If you need anything else, please add it yourself.
/** * 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]; } /** * A useful one that I use in development is the following * which dumps the target with syntax highlighting on by default */ function dump($target) { return CVarDumper: :dump($target, 10, true); }
For more information about Yii, readers who are interested in this site can view the topic:Yii framework introduction and common techniques summary》、《Summary of excellent development framework for php》、《Basic tutorial on getting started with smarty templates》、《PHP object-oriented programming tutorial》、《Summary of usage of php strings》、《PHP+mysql database operation tutorial"and"Summary of common database operation techniques for php》
I hope that this article will be helpful to everyone's PHP programming based on the Yii framework.