2,729   CodeIgniter PHP

想用一些类似base_url()的全局方法,参照CI系统库里的helper方法,添加自定义的helper以及全局方法

1,自定义My_url_helper类,在application/helpers目录下新增文件,名为My_url_helper.php,请注意命名规范

2,创建全局方法static_url(),My_url_helper.php文件内容如下,

// 请参考CI系统库里的url_helper中的index_page方法,这里直接读取application/config/config.php中数组key为static_url的值
if ( ! function_exists('static_url'))
{
	/**
	 * Index page
	 *
	 * Returns the "index_page" from your config file
	 *
	 * @return	string
	 */
	function static_url()
	{
		return get_instance()->config->item('static_url');
	}
}

3,在application/config/config.php中数组添加key为static_url的值

$config['base_url'] = '';
// 静态文件读取路径
$config['static_url'] = 'http://localhost.CodeIgniter/public/static';

4,自动加载定义的My_url_helper类,修改application/config/autoload.php文件

// 添加 My_url,请注意命名规范
$autoload['helper'] = array('My_url');

5,前端views页面调用全局方法static_url()

 <link rel="stylesheet" href="<?php echo static_url(); ?>/css/bootstrap.min.css">
<link rel="stylesheet" href="<?php echo static_url(); ?>/css/styles.css">
<link rel="stylesheet" href="<?php echo static_url(); ?>/css/main.css">



Leave a Reply

Your email address will not be published. Required fields are marked *