国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

Yii全局函數用法示例

2024-05-04 23:54:34
字體:
來源:轉載
供稿:網友

本文實例講述了Yii全局函數用法。分享給大家供大家參考,具體如下:

由于YII致力于完美的整合第三方庫,它并沒有定義任何全局函數。yii中的每一個應用都需要全類別和對象范圍。

例如,Yii::app()->user;Yii::app()->params['name'];等等。我們可以自行設定全局函數,使得代碼看起來更加簡潔易用。

我們可以保存在globals.php在protected/config目錄下。然后,在入口腳本index.php中,定義如下內容:

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

現在我們可以在應用的任何地方使用我們的全局函數,例如可以使用user()代替Yii::app()->user。注:如果你打算發布一個可重用的組件,請不要組件中使用全局函數,在不同的應用配置中,可能導致無法使用。同時,也應注意與第三方庫的沖突,可考慮對每個函數前加上自己的前綴,已做區分,例如框架核心均已C為前綴。

下面是代碼包含最常用的一些快捷功能。如還需其他,請自行添加。

/*** 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);}

希望本文所述對大家基于Yii框架的PHP程序設計有所幫助。


注:相關教程知識閱讀請移步到PHP教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 安义县| 榕江县| 东乌珠穆沁旗| 酒泉市| 隆化县| 墨江| 分宜县| 北票市| 密云县| 九寨沟县| 淳化县| 航空| 长白| 聂拉木县| 安阳市| 福鼎市| 威信县| 三门峡市| 建阳市| 乐陵市| 平湖市| 光山县| 长子县| 海阳市| 宁陕县| 卢氏县| 新源县| 南丹县| 射洪县| 衡阳县| 日照市| 新泰市| 佛学| 平武县| 类乌齐县| 乐昌市| 乐陵市| 麻栗坡县| 鄢陵县| 汾西县| 日土县|