mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-07 15:12:48 +08:00
Merge pull request #176 from chenjxlg/master
助手函数 是否定义判断,方便用户重写helper函数
This commit is contained in:
56
helper.php
56
helper.php
@@ -33,10 +33,12 @@ use think\View;
|
|||||||
* @param string $ext 类库后缀
|
* @param string $ext 类库后缀
|
||||||
* @return boolean
|
* @return boolean
|
||||||
*/
|
*/
|
||||||
|
if (!function_exists('load_trait')) {
|
||||||
function load_trait($class, $ext = EXT)
|
function load_trait($class, $ext = EXT)
|
||||||
{
|
{
|
||||||
return Loader::import($class, TRAIT_PATH, $ext);
|
return Loader::import($class, TRAIT_PATH, $ext);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 抛出异常处理
|
* 抛出异常处理
|
||||||
@@ -47,11 +49,13 @@ function load_trait($class, $ext = EXT)
|
|||||||
*
|
*
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
|
if (!function_exists('exception')) {
|
||||||
function exception($msg, $code = 0, $exception = '')
|
function exception($msg, $code = 0, $exception = '')
|
||||||
{
|
{
|
||||||
$e = $exception ?: '\think\Exception';
|
$e = $exception ?: '\think\Exception';
|
||||||
throw new $e($msg, $code);
|
throw new $e($msg, $code);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 记录时间(微秒)和内存使用情况
|
* 记录时间(微秒)和内存使用情况
|
||||||
@@ -60,6 +64,7 @@ function exception($msg, $code = 0, $exception = '')
|
|||||||
* @param integer|string $dec 小数位 如果是m 表示统计内存占用
|
* @param integer|string $dec 小数位 如果是m 表示统计内存占用
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
|
if (!function_exists('debug')) {
|
||||||
function debug($start, $end = '', $dec = 6)
|
function debug($start, $end = '', $dec = 6)
|
||||||
{
|
{
|
||||||
if ('' == $end) {
|
if ('' == $end) {
|
||||||
@@ -68,6 +73,7 @@ function debug($start, $end = '', $dec = 6)
|
|||||||
return 'm' == $dec ? Debug::getRangeMem($start, $end) : Debug::getRangeTime($start, $end, $dec);
|
return 'm' == $dec ? Debug::getRangeMem($start, $end) : Debug::getRangeTime($start, $end, $dec);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取语言变量值
|
* 获取语言变量值
|
||||||
@@ -76,10 +82,12 @@ function debug($start, $end = '', $dec = 6)
|
|||||||
* @param string $lang 语言
|
* @param string $lang 语言
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
|
if (!function_exists('lang')) {
|
||||||
function lang($name, $vars = [], $lang = '')
|
function lang($name, $vars = [], $lang = '')
|
||||||
{
|
{
|
||||||
return Lang::get($name, $vars, $lang);
|
return Lang::get($name, $vars, $lang);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取和设置配置参数
|
* 获取和设置配置参数
|
||||||
@@ -88,6 +96,7 @@ function lang($name, $vars = [], $lang = '')
|
|||||||
* @param string $range 作用域
|
* @param string $range 作用域
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
|
if (!function_exists('config')) {
|
||||||
function config($name = '', $value = null, $range = '')
|
function config($name = '', $value = null, $range = '')
|
||||||
{
|
{
|
||||||
if (is_null($value) && is_string($name)) {
|
if (is_null($value) && is_string($name)) {
|
||||||
@@ -96,6 +105,7 @@ function config($name = '', $value = null, $range = '')
|
|||||||
return Config::set($name, $value, $range);
|
return Config::set($name, $value, $range);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取输入数据 支持默认值和过滤
|
* 获取输入数据 支持默认值和过滤
|
||||||
@@ -104,6 +114,7 @@ function config($name = '', $value = null, $range = '')
|
|||||||
* @param string $filter 过滤方法
|
* @param string $filter 过滤方法
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
|
if (!function_exists('input')) {
|
||||||
function input($key, $default = null, $filter = null)
|
function input($key, $default = null, $filter = null)
|
||||||
{
|
{
|
||||||
if (0 === strpos($key, '?')) {
|
if (0 === strpos($key, '?')) {
|
||||||
@@ -128,6 +139,7 @@ function input($key, $default = null, $filter = null)
|
|||||||
return request()->$method($key, $default, $filter);
|
return request()->$method($key, $default, $filter);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 渲染输出Widget
|
* 渲染输出Widget
|
||||||
@@ -135,10 +147,12 @@ function input($key, $default = null, $filter = null)
|
|||||||
* @param array $data 传人的参数
|
* @param array $data 传人的参数
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
|
if (!function_exists('widget')) {
|
||||||
function widget($name, $data = [])
|
function widget($name, $data = [])
|
||||||
{
|
{
|
||||||
return Loader::action($name, $data, 'widget');
|
return Loader::action($name, $data, 'widget');
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 实例化Model
|
* 实例化Model
|
||||||
@@ -147,10 +161,12 @@ function widget($name, $data = [])
|
|||||||
* @param bool $appendSuffix 是否添加类名后缀
|
* @param bool $appendSuffix 是否添加类名后缀
|
||||||
* @return \think\Model
|
* @return \think\Model
|
||||||
*/
|
*/
|
||||||
|
if (!function_exists('model')) {
|
||||||
function model($name = '', $layer = 'model', $appendSuffix = false)
|
function model($name = '', $layer = 'model', $appendSuffix = false)
|
||||||
{
|
{
|
||||||
return Loader::model($name, $layer, $appendSuffix);
|
return Loader::model($name, $layer, $appendSuffix);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 实例化验证器
|
* 实例化验证器
|
||||||
@@ -159,10 +175,12 @@ function model($name = '', $layer = 'model', $appendSuffix = false)
|
|||||||
* @param bool $appendSuffix 是否添加类名后缀
|
* @param bool $appendSuffix 是否添加类名后缀
|
||||||
* @return \think\Validate
|
* @return \think\Validate
|
||||||
*/
|
*/
|
||||||
|
if (!function_exists('validate')) {
|
||||||
function validate($name = '', $layer = 'validate', $appendSuffix = false)
|
function validate($name = '', $layer = 'validate', $appendSuffix = false)
|
||||||
{
|
{
|
||||||
return Loader::validate($name, $layer, $appendSuffix);
|
return Loader::validate($name, $layer, $appendSuffix);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 实例化数据库类
|
* 实例化数据库类
|
||||||
@@ -170,10 +188,12 @@ function validate($name = '', $layer = 'validate', $appendSuffix = false)
|
|||||||
* @param array|string $config 数据库配置参数
|
* @param array|string $config 数据库配置参数
|
||||||
* @return \think\db\Query
|
* @return \think\db\Query
|
||||||
*/
|
*/
|
||||||
|
if (!function_exists('db')) {
|
||||||
function db($name = '', $config = [])
|
function db($name = '', $config = [])
|
||||||
{
|
{
|
||||||
return Db::connect($config)->name($name);
|
return Db::connect($config)->name($name);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 实例化控制器 格式:[模块/]控制器
|
* 实例化控制器 格式:[模块/]控制器
|
||||||
@@ -182,10 +202,12 @@ function db($name = '', $config = [])
|
|||||||
* @param bool $appendSuffix 是否添加类名后缀
|
* @param bool $appendSuffix 是否添加类名后缀
|
||||||
* @return \think\Controller
|
* @return \think\Controller
|
||||||
*/
|
*/
|
||||||
|
if (!function_exists('controller')) {
|
||||||
function controller($name, $layer = 'controller', $appendSuffix = false)
|
function controller($name, $layer = 'controller', $appendSuffix = false)
|
||||||
{
|
{
|
||||||
return Loader::controller($name, $layer, $appendSuffix);
|
return Loader::controller($name, $layer, $appendSuffix);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 调用模块的操作方法 参数格式 [模块/控制器/]操作
|
* 调用模块的操作方法 参数格式 [模块/控制器/]操作
|
||||||
@@ -195,10 +217,12 @@ function controller($name, $layer = 'controller', $appendSuffix = false)
|
|||||||
* @param bool $appendSuffix 是否添加类名后缀
|
* @param bool $appendSuffix 是否添加类名后缀
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
|
if (!function_exists('action')) {
|
||||||
function action($url, $vars = [], $layer = 'controller', $appendSuffix = false)
|
function action($url, $vars = [], $layer = 'controller', $appendSuffix = false)
|
||||||
{
|
{
|
||||||
return Loader::action($url, $vars, $layer, $appendSuffix);
|
return Loader::action($url, $vars, $layer, $appendSuffix);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 导入所需的类库 同java的Import 本函数有缓存功能
|
* 导入所需的类库 同java的Import 本函数有缓存功能
|
||||||
@@ -207,10 +231,12 @@ function action($url, $vars = [], $layer = 'controller', $appendSuffix = false)
|
|||||||
* @param string $ext 导入的文件扩展名
|
* @param string $ext 导入的文件扩展名
|
||||||
* @return boolean
|
* @return boolean
|
||||||
*/
|
*/
|
||||||
|
if (!function_exists('import')) {
|
||||||
function import($class, $baseUrl = '', $ext = EXT)
|
function import($class, $baseUrl = '', $ext = EXT)
|
||||||
{
|
{
|
||||||
return Loader::import($class, $baseUrl, $ext);
|
return Loader::import($class, $baseUrl, $ext);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 快速导入第三方框架类库 所有第三方框架的类库文件统一放到 系统的Vendor目录下面
|
* 快速导入第三方框架类库 所有第三方框架的类库文件统一放到 系统的Vendor目录下面
|
||||||
@@ -218,10 +244,12 @@ function import($class, $baseUrl = '', $ext = EXT)
|
|||||||
* @param string $ext 类库后缀
|
* @param string $ext 类库后缀
|
||||||
* @return boolean
|
* @return boolean
|
||||||
*/
|
*/
|
||||||
|
if (!function_exists('vendor')) {
|
||||||
function vendor($class, $ext = EXT)
|
function vendor($class, $ext = EXT)
|
||||||
{
|
{
|
||||||
return Loader::import($class, VENDOR_PATH, $ext);
|
return Loader::import($class, VENDOR_PATH, $ext);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 浏览器友好的变量输出
|
* 浏览器友好的变量输出
|
||||||
@@ -230,10 +258,12 @@ function vendor($class, $ext = EXT)
|
|||||||
* @param string $label 标签 默认为空
|
* @param string $label 标签 默认为空
|
||||||
* @return void|string
|
* @return void|string
|
||||||
*/
|
*/
|
||||||
|
if (!function_exists('dump')) {
|
||||||
function dump($var, $echo = true, $label = null)
|
function dump($var, $echo = true, $label = null)
|
||||||
{
|
{
|
||||||
return Debug::dump($var, $echo, $label);
|
return Debug::dump($var, $echo, $label);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Url生成
|
* Url生成
|
||||||
@@ -243,10 +273,12 @@ function dump($var, $echo = true, $label = null)
|
|||||||
* @param bool|string $domain 域名
|
* @param bool|string $domain 域名
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
|
if (!function_exists('url')) {
|
||||||
function url($url = '', $vars = '', $suffix = true, $domain = false)
|
function url($url = '', $vars = '', $suffix = true, $domain = false)
|
||||||
{
|
{
|
||||||
return Url::build($url, $vars, $suffix, $domain);
|
return Url::build($url, $vars, $suffix, $domain);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Session管理
|
* Session管理
|
||||||
@@ -255,6 +287,7 @@ function url($url = '', $vars = '', $suffix = true, $domain = false)
|
|||||||
* @param string $prefix 前缀
|
* @param string $prefix 前缀
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
|
if (!function_exists('session')) {
|
||||||
function session($name, $value = '', $prefix = null)
|
function session($name, $value = '', $prefix = null)
|
||||||
{
|
{
|
||||||
if (is_array($name)) {
|
if (is_array($name)) {
|
||||||
@@ -274,6 +307,7 @@ function session($name, $value = '', $prefix = null)
|
|||||||
return Session::set($name, $value, $prefix);
|
return Session::set($name, $value, $prefix);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Cookie管理
|
* Cookie管理
|
||||||
@@ -282,6 +316,7 @@ function session($name, $value = '', $prefix = null)
|
|||||||
* @param mixed $option 参数
|
* @param mixed $option 参数
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
|
if (!function_exists('cookie')) {
|
||||||
function cookie($name, $value = '', $option = null)
|
function cookie($name, $value = '', $option = null)
|
||||||
{
|
{
|
||||||
if (is_array($name)) {
|
if (is_array($name)) {
|
||||||
@@ -301,6 +336,7 @@ function cookie($name, $value = '', $option = null)
|
|||||||
return Cookie::set($name, $value, $option);
|
return Cookie::set($name, $value, $option);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 缓存管理
|
* 缓存管理
|
||||||
@@ -309,6 +345,7 @@ function cookie($name, $value = '', $option = null)
|
|||||||
* @param mixed $options 缓存参数
|
* @param mixed $options 缓存参数
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
|
if (!function_exists('cache')) {
|
||||||
function cache($name, $value = '', $options = null)
|
function cache($name, $value = '', $options = null)
|
||||||
{
|
{
|
||||||
if (is_array($options)) {
|
if (is_array($options)) {
|
||||||
@@ -334,6 +371,7 @@ function cache($name, $value = '', $options = null)
|
|||||||
return Cache::set($name, $value, $expire);
|
return Cache::set($name, $value, $expire);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 记录日志信息
|
* 记录日志信息
|
||||||
@@ -341,6 +379,7 @@ function cache($name, $value = '', $options = null)
|
|||||||
* @param string $level 日志级别
|
* @param string $level 日志级别
|
||||||
* @return void|array
|
* @return void|array
|
||||||
*/
|
*/
|
||||||
|
if (!function_exists('trace')) {
|
||||||
function trace($log = '[think]', $level = 'log')
|
function trace($log = '[think]', $level = 'log')
|
||||||
{
|
{
|
||||||
if ('[think]' === $log) {
|
if ('[think]' === $log) {
|
||||||
@@ -349,15 +388,18 @@ function trace($log = '[think]', $level = 'log')
|
|||||||
Log::record($log, $level);
|
Log::record($log, $level);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取当前Request对象实例
|
* 获取当前Request对象实例
|
||||||
* @return Request
|
* @return Request
|
||||||
*/
|
*/
|
||||||
|
if (!function_exists('request')) {
|
||||||
function request()
|
function request()
|
||||||
{
|
{
|
||||||
return Request::instance();
|
return Request::instance();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建普通 Response 对象实例
|
* 创建普通 Response 对象实例
|
||||||
@@ -367,10 +409,12 @@ function request()
|
|||||||
* @param string $type
|
* @param string $type
|
||||||
* @return Response
|
* @return Response
|
||||||
*/
|
*/
|
||||||
|
if (!function_exists('response')) {
|
||||||
function response($data = [], $code = 200, $header = [], $type = 'html')
|
function response($data = [], $code = 200, $header = [], $type = 'html')
|
||||||
{
|
{
|
||||||
return Response::create($data, $type, $code, $header);
|
return Response::create($data, $type, $code, $header);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 渲染模板输出
|
* 渲染模板输出
|
||||||
@@ -379,10 +423,12 @@ function response($data = [], $code = 200, $header = [], $type = 'html')
|
|||||||
* @param integer $code 状态码
|
* @param integer $code 状态码
|
||||||
* @return \think\response\View
|
* @return \think\response\View
|
||||||
*/
|
*/
|
||||||
|
if (!function_exists('view')) {
|
||||||
function view($template = '', $vars = [], $code = 200)
|
function view($template = '', $vars = [], $code = 200)
|
||||||
{
|
{
|
||||||
return Response::create($template, 'view', $code)->vars($vars);
|
return Response::create($template, 'view', $code)->vars($vars);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取\think\response\Json对象实例
|
* 获取\think\response\Json对象实例
|
||||||
@@ -392,10 +438,12 @@ function view($template = '', $vars = [], $code = 200)
|
|||||||
* @param array $options 参数
|
* @param array $options 参数
|
||||||
* @return \think\response\Json
|
* @return \think\response\Json
|
||||||
*/
|
*/
|
||||||
|
if (!function_exists('json')) {
|
||||||
function json($data = [], $code = 200, $header = [], $options = [])
|
function json($data = [], $code = 200, $header = [], $options = [])
|
||||||
{
|
{
|
||||||
return Response::create($data, 'json', $code, $header, $options);
|
return Response::create($data, 'json', $code, $header, $options);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取\think\response\Jsonp对象实例
|
* 获取\think\response\Jsonp对象实例
|
||||||
@@ -405,10 +453,12 @@ function json($data = [], $code = 200, $header = [], $options = [])
|
|||||||
* @param array $options 参数
|
* @param array $options 参数
|
||||||
* @return \think\response\Jsonp
|
* @return \think\response\Jsonp
|
||||||
*/
|
*/
|
||||||
|
if (!function_exists('jsonp')) {
|
||||||
function jsonp($data = [], $code = 200, $header = [], $options = [])
|
function jsonp($data = [], $code = 200, $header = [], $options = [])
|
||||||
{
|
{
|
||||||
return Response::create($data, 'jsonp', $code, $header, $options);
|
return Response::create($data, 'jsonp', $code, $header, $options);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取\think\response\Xml对象实例
|
* 获取\think\response\Xml对象实例
|
||||||
@@ -418,10 +468,12 @@ function jsonp($data = [], $code = 200, $header = [], $options = [])
|
|||||||
* @param array $options 参数
|
* @param array $options 参数
|
||||||
* @return \think\response\Xml
|
* @return \think\response\Xml
|
||||||
*/
|
*/
|
||||||
|
if (!function_exists('xml')) {
|
||||||
function xml($data = [], $code = 200, $header = [], $options = [])
|
function xml($data = [], $code = 200, $header = [], $options = [])
|
||||||
{
|
{
|
||||||
return Response::create($data, 'xml', $code, $header, $options);
|
return Response::create($data, 'xml', $code, $header, $options);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取\think\response\Redirect对象实例
|
* 获取\think\response\Redirect对象实例
|
||||||
@@ -430,6 +482,7 @@ function xml($data = [], $code = 200, $header = [], $options = [])
|
|||||||
* @param integer $code 状态码
|
* @param integer $code 状态码
|
||||||
* @return \think\response\Redirect
|
* @return \think\response\Redirect
|
||||||
*/
|
*/
|
||||||
|
if (!function_exists('redirect')) {
|
||||||
function redirect($url = [], $params = [], $code = 302)
|
function redirect($url = [], $params = [], $code = 302)
|
||||||
{
|
{
|
||||||
if (is_integer($params)) {
|
if (is_integer($params)) {
|
||||||
@@ -438,6 +491,7 @@ function redirect($url = [], $params = [], $code = 302)
|
|||||||
}
|
}
|
||||||
return Response::create($url, 'redirect', $code)->params($params);
|
return Response::create($url, 'redirect', $code)->params($params);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 抛出HTTP异常
|
* 抛出HTTP异常
|
||||||
@@ -445,7 +499,9 @@ function redirect($url = [], $params = [], $code = 302)
|
|||||||
* @param string $message 错误信息
|
* @param string $message 错误信息
|
||||||
* @param array $header 参数
|
* @param array $header 参数
|
||||||
*/
|
*/
|
||||||
|
if (!function_exists('abort')) {
|
||||||
function abort($code, $message = null, $header = [])
|
function abort($code, $message = null, $header = [])
|
||||||
{
|
{
|
||||||
throw new \think\exception\HttpException($code, $message, null, $header);
|
throw new \think\exception\HttpException($code, $message, null, $header);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user