Yii global function usage example, yii global example

Source: Internet
Author: User

Yii global function usage example, yii global example

This example describes how to use Yii global functions. We will share this with you for your reference. The details are as follows:

YII is committed to perfectly integrating third-party libraries and does not define any global functions. Every application in yii requires a full range of categories and objects.

For example, Yii: app ()-> user; Yii: app ()-> params ['name. We can set global functions on our own to make the Code look more concise and easy to use.

We can save it in the globals. php directory under the protected/config directory. Then, define the following content in index. php:

$globals=dirname(__FILE__).'/protected/config/globals.php';...require_once($yii);require_once($globals);

Now we can use our global functions anywhere in the application. For example, we can use user () instead of Yii: app ()-> user. Note: If you want to release a reusable component, do not use global functions in the component. Different Application configurations may cause unavailability. 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. For example, the framework core has been prefixed with C.

The following code contains some of the most common shortcuts. If you want to add others, add them by 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);}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.