因为专注,所以专业
2005 - 2019

结合IWeb添加类似ThinkPHP帮助函数

时间:2018-08-20 20:23:20    作者:八叔模板堂

/lib/helper.php

<?php
if (!function_exists('config')) {
    function config($name = '')
    {
		if(isset(IWeb::$app->config[$name]))
		{
			return IWeb::$app->config[$name];
		}
		
		$siteConfigObj = new Config("site_config");
		$site_config   = $siteConfigObj->getInfo();
		
		if(isset($site_config[$name]))
		{
			return $site_config[$name];
		}
		
		return NULL;
    }
}

if (!function_exists('get')) {
    function get($name = '',$filter='string',$default='')
    {
		return IReq::get($name)?IFilter::act(IReq::get($name),$filter):$default;
    }
}

if (!function_exists('url')) {
    function url($url = '')
    {
		return IUrl::getHost().IUrl::creatUrl($url);
    }
}

if (!function_exists('model')) {
    function model($name = '')
    {
		return new IModel($name);
    }
}

if (!function_exists('db')) {
    function db($name = '',$data = array())
    {
		$query = new IQuery($name);
		foreach($data as $key=>$value)
		{
			$query->$key = $value;
		}
		return $query;
    }
}

if (!function_exists('cookie')) {
    function cookie($name, $value = '')
    {
		if (is_null($name)) {
            ICookie::clearAll();
        } elseif ('' === $value) {
            // 获取
            return ICookie::get($name);
        } elseif (is_null($value)) {
            return ICookie::clear($name);
        } else {
            // 设置
            return ICookie::set($name, $value);
        }
    }
}

if (!function_exists('session')) {
    function session($name, $value = '')
    {
		if (is_null($name)) {
            ISession::clearAll();
        } elseif ('' === $value) {
            // 获取
            return ISession::get($name);
        } elseif (is_null($value)) {
            return ISession::clear($name);
        } else {
            // 设置
            return ISession::set($name, $value);
        }
    }
}

if (!function_exists('safe')) {
    function safe($name, $value = '')
    {
		if (is_null($name)) {
            ISafe::clearAll();
        } elseif ('' === $value) {
            // 获取
            return ISafe::get($name);
        } elseif (is_null($value)) {
            return ISafe::clear($name);
        } else {
            // 设置
            return ISafe::set($name, $value);
        }
    }
}

if (!function_exists('lang')) {
    function lang($name,$path = '')
    {
		if($path)
		{
			IWeb::$app->langclass->load($path);
		}
		
		return IWeb::$app->langclass->g($name);
    }
}

function format_date($time){
	$t=time()-$time;
	$f=array(
		'31536000'=>'年',
		'2592000'=>'个月',
		'604800'=>'星期',
		'86400'=>'天',
		'3600'=>'小时',
		'60'=>'分钟',
		'1'=>'秒'
	);
	foreach ($f as $k=>$v)    {
		if (0 !=$c=floor($t/(int)$k)) {
			return $c.$v.'前';
		}
	}
}
/lib/IWeb.php

<?php
//内核路径
defined('IWEB_PATH') or define('IWEB_PATH',dirname(__file__).DIRECTORY_SEPARATOR);
include IWEB_PATH.'helper.php';
...
赞(207
免费拨打  199-7086-1797
免费拨打  199-7086-1797免费拨打 199-7086-1797
微信二维码
手机二维码
返回顶部
返回顶部返回顶部