法1:php
在使用Yii開發中咱們常常會遇到一個問題,每次使用Yii的組件、擴展等,咱們會像下面同樣去寫:html
Yii::app()->user; Yii::app()->getClientScript(); Yii::app()->params['name']; ...
這使咱們用起來很不方便,我在 yiiFramework 上看到了一篇解決這個問題的 Wiki Use shortcut functions to reduce typing, 由於上面的英文很簡單就不去翻譯了,這裏我只說明一下用法,建立全局文件 globals.php
把快捷函數放到這個全局文件裏面。全局文件放到 protected
文件夾下面,而後在入口文件 `index.php' 引入app
required('path/to/globals.php');
我推薦的是在配置文件 main.php' 裏面引入,爲了使
console應用也能使用,配置文件
console.php` 可修改成yii
return CMap::mergeArray(require('main.php'), array( // console 的配置 ... ));
下面是官網上的一些快捷函數和我本身經常使用到的一些:ide
<?php /** * This is the shortcut to DIRECTORY_SEPARATOR */ defined('DS') or define('DS', DIRECTORY_SEPARATOR); /** * This is the shortcut to Yii::app() * * @return CWebApplication */ function app() { return Yii::app(); } /** * This is the shortcut to Yii::app()->clientScript * * @return CClientScript */ function cs() { return Yii::app()->getClientScript(); } /** * This is the shortcut to Yii::app()->user. * * @return CWebUser */ function user() { return Yii::app()->getUser(); } /** * this is the shortcut 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 shortcut to CHtml::encode * * @param string $text * @return string */ function h($text) { return htmlspecialchars($text, ENT_QUOTES, Yii::app()->charset); } /** * This is the shortcut to CHtml::link(). * * @param string $text * @param string|array $url * @param array $htmlOptions * @return string */ function l($text, $url = '#', $htmlOptions = array()) { return CHtml::link($text, $url, $htmlOptions); } /** * This is the shortcut 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 shortcut 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(); return $url === null ? $baseUrl : $baseUrl . '/' . ltrim($url, '/'); } /** * Returns the named application parameter. * This is the shortcut to Yii::app()->params[$name]. * * @param string $name * @return mixed */ function param($name) { return Yii::app()->params[$name]; } // 下面是我用到的 /** * 格式化函數,這是 Yii::app()->format 的快捷函數,使用方法 * * <pre> * format($timestamp,'Datetime'); * </pre> * * @param mixed $value * @param string $type 格式化的類型,如 'datetime', 'time'.., 詳見 CFormatter * @return mixed */ function format($value, $type) { return Yii::app()->format->format($value, $type); } /** * 這是加載模型的函數,若是沒有找到模型則會拋出 404 錯誤 * * @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); } }
源碼下載: 點擊下載函數
咱們還能夠根據項目的具體需求添加ui
轉自:http://www.yiiwiki.com/45/this
法2:url
是使用YII的類庫spa
在protected\components下創建一個tool.php的靜態類
代碼以下
<?php class Tool { public static function mkpath($path) { $dirs = array(); $path = preg_replace('/(\/){2,}|(\\\){1,}/', '/', $path); $dirs = explode("/", $path); $path = ""; foreach ($dirs as $element) { $path.=$element . "/"; if (!is_dir($path)) { if (!mkdir($path, 0777)) { return false; } else { chmod($path, 0777); } } } return true; } public static function test() { echo('tetts'); } }
而後控制器裏面直接使用Tool::mkpath(xx/xx);
第一種方法最大的很差就是可能會出現衝突.