For previous versions of ThinkPHP5.0, the helper functions were all single-letter functions, but after ThinkPHP5, the following functions were used instead of single-letter functions:
Most commonly used:
/**
* Instantiate model
* @param string $name model name
* @param string $layer Business layer Name
* @param bool $appendSuffix Add class name suffix
* @return \think\model
*/
if (!function_exists (' model ')) {
function model ($name = ", $layer = ' model ', $appendSuffix = False)
{
Return Loader::model ($name, $layer, $appendSuffix);
}
}
—————————————————————————————————————————————————
/**
* Instantiation Validator
* @param string $name validator Name
* @param string $layer Business layer Name
* @param bool $appendSuffix Add class name suffix
* @return \think\validate
*/
if (!function_exists (' Validate ')) {
function Validate ($name = ", $layer = ' Validate ', $appendSuffix = False)
{
Return Loader::validate ($name, $layer, $appendSuffix);
}
}
—————————————————————————————————————————————————
/**
* Cache Management
* @param mixed $name cache name if the array represents cache settings
* @param mixed $value Cache value
* @param mixed $options cache parameters
* @return Mixed
*/
if (!function_exists (' cache ')) {
function cache ($name, $value = ", $options = null)
{
if (Is_array ($options)) {
Initializing a cache operation at the same time
Cache::connect ($options);
} elseif (Is_array ($name)) {
Cache initialization
Return Cache::connect ($name);
}
if ("= = = = $value) {
Get Cache
Return Cache::get ($name);
} elseif (Is_null ($value)) {
Delete Cache
Return Cache::rm ($name);
} else {
Cache data
if (Is_array ($options)) {
$expire = Isset ($options [' expire '])? $options [' expire ']: null; Fix query cache cannot set expiration time
} else {
$expire = Is_numeric ($options)? $options: null; Default shortcut cache setting expiration time
}
Return Cache::set ($name, $value, $expire);
}
}
}
—————————————————————————————————————————————————
/**
* Get and set configuration parameters
* @param string|array $name The name of the parameter
* @param mixed $value parameter values
* @param string $range scope
* @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 Generation
* @param string $url routing Address
* @param string|array $value variables
* @param bool|string $suffix Prefix
* @param bool|string $domain domain name
* @return String
*/
if (!function_exists (' url ')) {
function url ($url = ', $vars = ', $suffix = true, $domain = False)
{
Return Url::build ($url, $vars, $suffix, $domain);
}
}
—————————————————————————————————————————————————
/**
* Fast import of third-party framework class library all third-party framework class library files are placed under the vendor directory of the system.
* @param string $class class Library
* @param string $ext class library suffix
* @return Boolean
*/
if (!function_exists (' Vendor ')) {
function vendor ($class, $ext = ext)
{
Return Loader::import ($class, Vendor_path, $ext);
}
}
—————————————————————————————————————————————————
/**
* Instantiation Controller format: [module/] Controller
* @param string $name resource Address
* @param string $layer control Layer Name
* @param bool $appendSuffix Add class name suffix
* @return \think\controller
*/
if (!function_exists (' controller ')) {
function controller ($name, $layer = ' Controller ', $appendSuffix = False)
{
Return Loader::controller ($name, $layer, $appendSuffix);
}
}
—————————————————————————————————————————————————
/**
* Call Module action method parameter format [module/controller/] operation
* @param string $url call Address
* @param string|array $vars Call parameters support strings and arrays
* @param string $layer the name of the control layer to invoke
* @param bool $appendSuffix Add class name suffix
* @return Mixed
*/
if (!function_exists (' action ')) {
function action ($url, $vars = [], $layer = ' Controller ', $appendSuffix = False)
{
Return Loader::action ($url, $vars, $layer, $appendSuffix);
}
}
—————————————————————————————————————————————————
/**
* Instantiating database classes
* data table name @param string $name operation (without prefix)
* @param array|string $config Database configuration parameters
* @return \think\db\query
*/
if (!function_exists (' db ')) {
function db ($name = ", $config = [])
{
Return Db::connect ($config)->name ($name);
}
}
—————————————————————————————————————————————————
/**
* Session Management
* @param String|array $name session name, if the array represents the session setting
* @param mixed $value Session value
* @param string $prefix prefix
* @return Mixed
*/
if (!function_exists (' Session ')) {
function session ($name, $value = ", $prefix = null)
{
if (Is_array ($name)) {
Initialization
Session::init ($name);
} elseif (Is_null ($name)) {
Clear
Session::clear ($value);
} elseif ("= = = $value) {
Judging or acquiring
return 0 = = Strpos ($name, '? ')? Session::has (substr ($name, 1), $prefix): Session::get ($name, $prefix);
} elseif (Is_null ($value)) {
Delete
Return Session::d elete ($name, $prefix);
} else {
Set up
Return Session::set ($name, $value, $prefix);
}
}
}
—————————————————————————————————————————————————
/**
* Cookie Management
* @param string|array $name cookie name, if the array represents a cookie setting
* @param mixed $value cookie value
* @param mixed $option parameters
* @return Mixed
*/
if (!function_exists (' cookie ')) {
function Cookie ($name, $value = ", $option = null)
{
if (Is_array ($name)) {
Initialization
Cookie::init ($name);
} elseif (Is_null ($name)) {
Clear
Cookie::clear ($value);
} elseif ("= = = $value) {
Get
Return Cookie::get ($name);
} elseif (Is_null ($value)) {
Delete
Return Cookie::d elete ($name);
} else {
Set up
Return Cookie::set ($name, $value, $option);
}
}
}
—————————————————————————————————————————————————
—————————————————————————————————————————————————
Other:
/**
* Fast import traits PHP5.5 above without calling
* @param string $class trait Library
* @param string $ext class library suffix
* @return Boolean
*/
if (!function_exists (' load_trait ')) {
function load_trait ($class, $ext = ext)
{
Return Loader::import ($class, Trait_path, $ext);
}
}
—————————————————————————————————————————————————
/**
* Throw exception Handling
*
* @param string $msg Exception message
* @param integer $code exception code defaults to 0
* @param string $exception Exception class
*
* @throws Exception
*/
if (!function_exists (' exception ')) {
Function exception ($msg, $code = 0, $exception = ")
{
$e = $exception?: ' \think\exception ';
throw new $e ($msg, $code);
}
}
—————————————————————————————————————————————————
/**
* Recording time (microseconds) and memory usage
* @param string $start start tag
* @param string $end end tag
* @param integer|string $dec decimal Place if m indicates statistics memory consumption
* @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);
}
}
}
—————————————————————————————————————————————————
/**
* Get language variable values
* @param string $name language variable name
* @param array $vars dynamic variable values
* @param string $lang language
* @return Mixed
*/
if (!function_exists (' lang ')) {
function lang ($name, $vars = [], $lang = ")
{
Return Lang::get ($name, $vars, $lang);
}
}
—————————————————————————————————————————————————
/**
* Get input data support default values and filtering
* @param string $key gets the variable name
* @param mixed $default default value
* @param string $filter filtering method
* @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, '. ')) {
Specify the source of the parameter
$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 {
Default is auto-judgment
$method = ' param ';
}
if (Isset ($has)) {
Return request ()->has ($key, $method, $default);
} else {
$method of Return request ($key, $default, $filter);
}
}
}
—————————————————————————————————————————————————
/**
* Render Output Widget
* @param string $name widget name
* @param The parameters of the array $data descendants
* @return Mixed
*/
if (!function_exists (' widget ')) {
Function widget ($name, $data = [])
{
Return Loader::action ($name, $data, ' widgets ');
}
}
—————————————————————————————————————————————————/**
* Import the required class library with Java Import This function has the cache function
* @param string $class class library namespace strings
* @param string $baseUrl start path
* @param string $ext imported file name extensions
* @return Boolean
*/
if (!function_exists (' import ')) {
Function Import ($class, $baseUrl = ", $ext = ext)
{
Return Loader::import ($class, $BASEURL, $ext);
}
}
—————————————————————————————————————————————————
/**
* Browser-friendly variable output
* @param mixed $var variables
* @param boolean $echo whether the output defaults to True if False returns the output string
* @param string $label tag is empty by default
* @return void|string
*/
if (!function_exists (' dump ')) {
function dump ($var, $echo = true, $label = null)
{
Return Debug::d UMP ($var, $echo, $label);
}
}
—————————————————————————————————————————————————
/**
* Log information is logged
* @param mixed $log log information support strings and arrays
* @param string $level log 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);
}
}
}
—————————————————————————————————————————————————
/**
* Get the current Request object instance
* @return Request
*/
if (!function_exists (' request ')) {
function request ()
{
return Request::instance ();
}
}
—————————————————————————————————————————————————
/**
* Create a generic Response object instance
* @param mixed $data output data
* @param int|string $code Status code
* @param array $header header information
* @param string $type
* @return Response
*/
if (!function_exists (' response ')) {
function response ($data = [], $code = $, $header = [], $type = ' html ')
{
Return Response::create ($data, $type, $code, $header);
}
}
—————————————————————————————————————————————————
/**
* Render Template Output
* @param string $template template file
* @param array $vars template variable
* @param integer $code Status code
* @return \think\response\view
*/
if (!function_exists (' view ')) {
function view ($template = ", $vars = [], $code = 200)
{
Return Response::create ($template, ' view ', $code)->vars ($vars);
}
}
—————————————————————————————————————————————————
/**
* Get \think\response\json Object instance
* @param The data returned by the mixed $data
* @param integer $code status code
* @param array $header head
* @param array $options parameters
* @return \think\response\json
*/
if (!function_exists (' json ')) {
function json ($data = [], $code = $, $header = [], $options = [])
{
Return Response::create ($data, ' json ', $code, $header, $options);
}
}
—————————————————————————————————————————————————
/**
* Get \think\response\jsonp Object instance
* @param The data returned by the mixed $data
* @param integer $code Status code
* @param array $header head
* @param array $options parameters
* @return \think\response\jsonp
*/
if (!function_exists (' Jsonp ')) {
function Jsonp ($data = [], $code = $, $header = [], $options = [])
{
Return Response::create ($data, ' Jsonp ', $code, $header, $options);
}
}
—————————————————————————————————————————————————
/**
* Get \think\response\xml Object instance
* @param The data returned by the mixed $data
* @param integer $code Status code
* @param array $header head
* @param array $options parameters
* @return \think\response\xml
*/
if (!function_exists (' xml ')) {
function xml ($data = [], $code = $, $header = [], $options = [])
{
Return Response::create ($data, ' xml ', $code, $header, $options);
}
}
—————————————————————————————————————————————————
/**
* Get \think\response\redirect Object instance
* Address of Url::build method supported by @param mixed $url redirect Address
* @param array|integer $params Additional parameters
* @param integer $code Status 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);
}
}
—————————————————————————————————————————————————
/**
* Throwing HTTP Exceptions
* @param integer $code Status code
* @param string $message error message
* @param array $header parameters
*/
if (!function_exists (' abort ')) {
function Abort ($code, $message = null, $header = [])
{
throw new \think\exception\httpexception ($code, $message, NULL, $header);
}
}
—————————————————————————————————————————————————
—————————————————————————————————————————————————
ThinkPHP5 helper Functions