Yii全局函數(shù)用法示例
本文實(shí)例講述了Yii全局函數(shù)用法。分享給大家供大家參考,具體如下:
由于YII致力于完美的整合第三方庫(kù),它并沒(méi)有定義任何全局函數(shù)。yii中的每一個(gè)應(yīng)用都需要全類(lèi)別和對(duì)象范圍。
例如,Yii::app()->user;Yii::app()->params['name'];等等。我們可以自行設(shè)定全局函數(shù),使得代碼看起來(lái)更加簡(jiǎn)潔易用。
我們可以保存在globals.php在protected/config目錄下。然后,在入口腳本index.php中,定義如下內(nèi)容:
$globals=dirname(__FILE__).'/protected/config/globals.php'; ... require_once($yii); require_once($globals);
現(xiàn)在我們可以在應(yīng)用的任何地方使用我們的全局函數(shù),例如可以使用user()代替Yii::app()->user。注:如果你打算發(fā)布一個(gè)可重用的組件,請(qǐng)不要組件中使用全局函數(shù),在不同的應(yīng)用配置中,可能導(dǎo)致無(wú)法使用。同時(shí),也應(yīng)注意與第三方庫(kù)的沖突,可考慮對(duì)每個(gè)函數(shù)前加上自己的前綴,已做區(qū)分,例如框架核心均已C為前綴。
下面是代碼包含最常用的一些快捷功能。如還需其他,請(qǐng)自行添加。
/** * 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); }
更多關(guān)于Yii相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Yii框架入門(mén)及常用技巧總結(jié)》、《php優(yōu)秀開(kāi)發(fā)框架總結(jié)》、《smarty模板入門(mén)基礎(chǔ)教程》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門(mén)教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家基于Yii框架的PHP程序設(shè)計(jì)有所幫助。
- Yii2中添加全局函數(shù)的方法分析
- 深入淺析yii2-gii自定義模板的方法
- yii2高級(jí)應(yīng)用之自定義組件實(shí)現(xiàn)全局使用圖片上傳功能的方法
- Yii2實(shí)現(xiàn)自定義獨(dú)立驗(yàn)證器的方法
- Yii2框架自定義驗(yàn)證規(guī)則操作示例
- Yii框架自定義數(shù)據(jù)庫(kù)操作組件示例
- YII框架實(shí)現(xiàn)自定義第三方擴(kuò)展操作示例
- yii2.0之GridView自定義按鈕和鏈接用法
- yii2中的rules 自定義驗(yàn)證規(guī)則詳解
- Yii框架函數(shù)簡(jiǎn)單用法分析
- YII2框架自定義全局函數(shù)的實(shí)現(xiàn)方法小結(jié)
相關(guān)文章
基于PHP的加載類(lèi)操作以及其他兩種魔術(shù)方法的應(yīng)用實(shí)例
下面小編就為大家?guī)?lái)一篇基于PHP的加載類(lèi)操作以及其他兩種魔術(shù)方法的應(yīng)用實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08基于AppServ,XAMPP,WAMP配置php.ini去掉警告信息(NOTICE)的方法詳解
本篇文章是對(duì)AppServ,XAMPP,WAMP配置php.ini去掉警告信息(NOTICE)的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05PHP表單數(shù)據(jù)寫(xiě)入MySQL數(shù)據(jù)庫(kù)的代碼
這篇文章主要介紹了PHP表單數(shù)據(jù)寫(xiě)入MySQL數(shù)據(jù)庫(kù)的相關(guān)資料非常不錯(cuò)具有參考借鑒價(jià)值,需要的朋友可以參考下2016-05-05laravel-admin 添加、編輯按鈕支持?jǐn)y帶參數(shù)的解決方法
通過(guò)修改源碼實(shí)現(xiàn)laravel-admin添加、編輯按鈕支持?jǐn)y帶參數(shù),解決一些特殊功能需求,并且不影響之前添加和編輯程序運(yùn)行,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2023-11-11詳解PHP字符串替換str_replace()函數(shù)四種用法
str_replace() 函數(shù)以其他字符替換字符串中的一些字符(區(qū)分大小寫(xiě))。本文重點(diǎn)給大家介紹PHP字符串替換str_replace()函數(shù)四種用法,感興趣的朋友一起看看吧2017-10-10