對於ThinkPHP5.0以前的版本,助手函數全部是單字母函數,但到ThinkPHP5之后,使用如下函數來代替單字母函數:
最常用:
/**
* 實例化Model
* @param string $name Model名稱
* @param string $layer 業務層名稱
* @param bool $appendSuffix 是否添加類名后綴
* @return \think\Model
*/
if (!function_exists('model')) {
function model($name = '', $layer = 'model', $appendSuffix = false)
{
return Loader::model($name, $layer, $appendSuffix);
}
}
—————————————————————————————————————————————————
/**
* 實例化驗證器
* @param string $name 驗證器名稱
* @param string $layer 業務層名稱
* @param bool $appendSuffix 是否添加類名后綴
* @return \think\Validate
*/
if (!function_exists('validate')) {
function validate($name = '', $layer = 'validate', $appendSuffix = false)
{
return Loader::validate($name, $layer, $appendSuffix);
}
}
—————————————————————————————————————————————————
/**
* 緩存管理
* @param mixed $name 緩存名稱,如果為數組表示進行緩存設置
* @param mixed $value 緩存值
* @param mixed $options 緩存參數
* @return mixed
*/
if (!function_exists('cache')) {
function cache($name, $value = '', $options = null)
{
if (is_array($options)) {
// 緩存操作的同時初始化
Cache::connect($options);
} elseif (is_array($name)) {
// 緩存初始化
return Cache::connect($name);
}
if ('' === $value) {
// 獲取緩存
return Cache::get($name);
} elseif (is_null($value)) {
// 刪除緩存
return Cache::rm($name);
} else {
// 緩存數據
if (is_array($options)) {
$expire = isset($options['expire']) ? $options['expire'] : null; //修復查詢緩存無法設置過期時間
} else {
$expire = is_numeric($options) ? $options : null; //默認快捷緩存設置過期時間
}
return Cache::set($name, $value, $expire);
}
}
}
—————————————————————————————————————————————————
/**
* 獲取和設置配置參數
* @param string|array $name 參數名
* @param mixed $value 參數值
* @param string $range 作用域
* @return mixed
*/
if (!function_exists('config')) {
function config($name = '', $value = null, $range = '')
{
if (is_null($value) && is_string($name)) {
return Config::get($name, $range);
} else {
return Config::set($name, $value, $range);
}
}
}
—————————————————————————————————————————————————
/**
* Url生成
* @param string $url 路由地址
* @param string|array $value 變量
* @param bool|string $suffix 前綴
* @param bool|string $domain 域名
* @return string
*/
if (!function_exists('url')) {
function url($url = '', $vars = '', $suffix = true, $domain = false)
{
return Url::build($url, $vars, $suffix, $domain);
}
}
—————————————————————————————————————————————————
/**
* 快速導入第三方框架類庫 所有第三方框架的類庫文件統一放到 系統的Vendor目錄下面
* @param string $class 類庫
* @param string $ext 類庫后綴
* @return boolean
*/
if (!function_exists('vendor')) {
function vendor($class, $ext = EXT)
{
return Loader::import($class, VENDOR_PATH, $ext);
}
}
—————————————————————————————————————————————————
/**
* 實例化控制器 格式:[模塊/]控制器
* @param string $name 資源地址
* @param string $layer 控制層名稱
* @param bool $appendSuffix 是否添加類名后綴
* @return \think\Controller
*/
if (!function_exists('controller')) {
function controller($name, $layer = 'controller', $appendSuffix = false)
{
return Loader::controller($name, $layer, $appendSuffix);
}
}
—————————————————————————————————————————————————
/**
* 調用模塊的操作方法 參數格式 [模塊/控制器/]操作
* @param string $url 調用地址
* @param string|array $vars 調用參數 支持字符串和數組
* @param string $layer 要調用的控制層名稱
* @param bool $appendSuffix 是否添加類名后綴
* @return mixed
*/
if (!function_exists('action')) {
function action($url, $vars = [], $layer = 'controller', $appendSuffix = false)
{
return Loader::action($url, $vars, $layer, $appendSuffix);
}
}
—————————————————————————————————————————————————
/**
* 實例化數據庫類
* @param string $name 操作的數據表名稱(不含前綴)
* @param array|string $config 數據庫配置參數
* @return \think\db\Query
*/
if (!function_exists('db')) {
function db($name = '', $config = [])
{
return Db::connect($config)->name($name);
}
}
—————————————————————————————————————————————————
/**
* Session管理
* @param string|array $name session名稱,如果為數組表示進行session設置
* @param mixed $value session值
* @param string $prefix 前綴
* @return mixed
*/
if (!function_exists('session')) {
function session($name, $value = '', $prefix = null)
{
if (is_array($name)) {
// 初始化
Session::init($name);
} elseif (is_null($name)) {
// 清除
Session::clear($value);
} elseif ('' === $value) {
// 判斷或獲取
return 0 === strpos($name, '?') ? Session::has(substr($name, 1), $prefix) : Session::get($name, $prefix);
} elseif (is_null($value)) {
// 刪除
return Session::delete($name, $prefix);
} else {
// 設置
return Session::set($name, $value, $prefix);
}
}
}
—————————————————————————————————————————————————
/**
* Cookie管理
* @param string|array $name cookie名稱,如果為數組表示進行cookie設置
* @param mixed $value cookie值
* @param mixed $option 參數
* @return mixed
*/
if (!function_exists('cookie')) {
function cookie($name, $value = '', $option = null)
{
if (is_array($name)) {
// 初始化
Cookie::init($name);
} elseif (is_null($name)) {
// 清除
Cookie::clear($value);
} elseif ('' === $value) {
// 獲取
return Cookie::get($name);
} elseif (is_null($value)) {
// 刪除
return Cookie::delete($name);
} else {
// 設置
return Cookie::set($name, $value, $option);
}
}
}
—————————————————————————————————————————————————
—————————————————————————————————————————————————
其他:
/**
* 快速導入Traits PHP5.5以上無需調用
* @param string $class trait庫
* @param string $ext 類庫后綴
* @return boolean
*/
if (!function_exists('load_trait')) {
function load_trait($class, $ext = EXT)
{
return Loader::import($class, TRAIT_PATH, $ext);
}
}
—————————————————————————————————————————————————
/**
* 拋出異常處理
*
* @param string $msg 異常消息
* @param integer $code 異常代碼 默認為0
* @param string $exception 異常類
*
* @throws Exception
*/
if (!function_exists('exception')) {
function exception($msg, $code = 0, $exception = '')
{
$e = $exception ?: '\think\Exception';
throw new $e($msg, $code);
}
}
—————————————————————————————————————————————————
/**
* 記錄時間(微秒)和內存使用情況
* @param string $start 開始標簽
* @param string $end 結束標簽
* @param integer|string $dec 小數位 如果是m 表示統計內存占用
* @return mixed
*/
if (!function_exists('debug')) {
function debug($start, $end = '', $dec = 6)
{
if ('' == $end) {
Debug::remark($start);
} else {
return 'm' == $dec ? Debug::getRangeMem($start, $end) : Debug::getRangeTime($start, $end, $dec);
}
}
}
—————————————————————————————————————————————————
/**
* 獲取語言變量值
* @param string $name 語言變量名
* @param array $vars 動態變量值
* @param string $lang 語言
* @return mixed
*/
if (!function_exists('lang')) {
function lang($name, $vars = [], $lang = '')
{
return Lang::get($name, $vars, $lang);
}
}
—————————————————————————————————————————————————
/**
* 獲取輸入數據 支持默認值和過濾
* @param string $key 獲取的變量名
* @param mixed $default 默認值
* @param string $filter 過濾方法
* @return mixed
*/
if (!function_exists('input')) {
function input($key, $default = null, $filter = null)
{
if (0 === strpos($key, '?')) {
$key = substr($key, 1);
$has = true;
}
if ($pos = strpos($key, '.')) {
// 指定參數來源
$method = substr($key, 0, $pos);
if (in_array($method, ['get', 'post', 'put', 'delete', 'param', 'request', 'session', 'cookie', 'server', 'env', 'path', 'file'])) {
$key = substr($key, $pos + 1);
} else {
$method = 'param';
}
} else {
// 默認為自動判斷
$method = 'param';
}
if (isset($has)) {
return request()->has($key, $method, $default);
} else {
return request()->$method($key, $default, $filter);
}
}
}
—————————————————————————————————————————————————
/**
* 渲染輸出Widget
* @param string $name Widget名稱
* @param array $data 傳人的參數
* @return mixed
*/
if (!function_exists('widget')) {
function widget($name, $data = [])
{
return Loader::action($name, $data, 'widget');
}
}
—————————————————————————————————————————————————/**
* 導入所需的類庫 同java的Import 本函數有緩存功能
* @param string $class 類庫命名空間字符串
* @param string $baseUrl 起始路徑
* @param string $ext 導入的文件擴展名
* @return boolean
*/
if (!function_exists('import')) {
function import($class, $baseUrl = '', $ext = EXT)
{
return Loader::import($class, $baseUrl, $ext);
}
}
—————————————————————————————————————————————————
/**
* 瀏覽器友好的變量輸出
* @param mixed $var 變量
* @param boolean $echo 是否輸出 默認為true 如果為false 則返回輸出字符串
* @param string $label 標簽 默認為空
* @return void|string
*/
if (!function_exists('dump')) {
function dump($var, $echo = true, $label = null)
{
return Debug::dump($var, $echo, $label);
}
}
—————————————————————————————————————————————————
/**
* 記錄日志信息
* @param mixed $log log信息 支持字符串和數組
* @param string $level 日志級別
* @return void|array
*/
if (!function_exists('trace')) {
function trace($log = '[think]', $level = 'log')
{
if ('[think]' === $log) {
return Log::getLog();
} else {
Log::record($log, $level);
}
}
}
—————————————————————————————————————————————————
/**
* 獲取當前Request對象實例
* @return Request
*/
if (!function_exists('request')) {
function request()
{
return Request::instance();
}
}
—————————————————————————————————————————————————
/**
* 創建普通 Response 對象實例
* @param mixed $data 輸出數據
* @param int|string $code 狀態碼
* @param array $header 頭信息
* @param string $type
* @return Response
*/
if (!function_exists('response')) {
function response($data = [], $code = 200, $header = [], $type = 'html')
{
return Response::create($data, $type, $code, $header);
}
}
—————————————————————————————————————————————————
/**
* 渲染模板輸出
* @param string $template 模板文件
* @param array $vars 模板變量
* @param integer $code 狀態碼
* @return \think\response\View
*/
if (!function_exists('view')) {
function view($template = '', $vars = [], $code = 200)
{
return Response::create($template, 'view', $code)->vars($vars);
}
}
—————————————————————————————————————————————————
/**
* 獲取\think\response\Json對象實例
* @param mixed $data 返回的數據
* @param integer $code 狀態碼
* @param array $header 頭部
* @param array $options 參數
* @return \think\response\Json
*/
if (!function_exists('json')) {
function json($data = [], $code = 200, $header = [], $options = [])
{
return Response::create($data, 'json', $code, $header, $options);
}
}
—————————————————————————————————————————————————
/**
* 獲取\think\response\Jsonp對象實例
* @param mixed $data 返回的數據
* @param integer $code 狀態碼
* @param array $header 頭部
* @param array $options 參數
* @return \think\response\Jsonp
*/
if (!function_exists('jsonp')) {
function jsonp($data = [], $code = 200, $header = [], $options = [])
{
return Response::create($data, 'jsonp', $code, $header, $options);
}
}
—————————————————————————————————————————————————
/**
* 獲取\think\response\Xml對象實例
* @param mixed $data 返回的數據
* @param integer $code 狀態碼
* @param array $header 頭部
* @param array $options 參數
* @return \think\response\Xml
*/
if (!function_exists('xml')) {
function xml($data = [], $code = 200, $header = [], $options = [])
{
return Response::create($data, 'xml', $code, $header, $options);
}
}
—————————————————————————————————————————————————
/**
* 獲取\think\response\Redirect對象實例
* @param mixed $url 重定向地址 支持Url::build方法的地址
* @param array|integer $params 額外參數
* @param integer $code 狀態碼
* @return \think\response\Redirect
*/
if (!function_exists('redirect')) {
function redirect($url = [], $params = [], $code = 302)
{
if (is_integer($params)) {
$code = $params;
$params = [];
}
return Response::create($url, 'redirect', $code)->params($params);
}
}
—————————————————————————————————————————————————
/**
* 拋出HTTP異常
* @param integer $code 狀態碼
* @param string $message 錯誤信息
* @param array $header 參數
*/
if (!function_exists('abort')) {
function abort($code, $message = null, $header = [])
{
throw new \think\exception\HttpException($code, $message, null, $header);
}
}
—————————————————————————————————————————————————
—————————————————————————————————————————————————