用户
 找回密码
 入住 CI 中国社区
搜索
查看: 8368|回复: 8
收起左侧

[已解决] 如何定义常量路径

[复制链接]
发表于 2012-4-2 09:21:51 | 显示全部楼层 |阅读模式
现在在CI根目录下建立了 statics/js   css  images 等文件

以后在模板中调用相对应的路径 如   site_url('statics/js')

现在想把他写的简便些, 如何处理这样的路径    例如写成 JS.文件名字 就能调用这样的效果

不要每次都输入 statics 这路径
发表于 2012-4-2 11:04:09 | 显示全部楼层
在控制器里定义:
define('BASE_URL', base_url());
记得装载 url helper
发表于 2012-4-2 12:29:34 | 显示全部楼层
你可以在config目录的constants.php里定义,全局啊亲
发表于 2012-4-3 22:02:39 | 显示全部楼层
额,又学会一招,每天都在长新知识啊
发表于 2012-4-3 23:52:26 | 显示全部楼层
俺是来学习地,嘿嘿
发表于 2014-4-14 16:15:02 | 显示全部楼层
longjianghu还是靠谱的
发表于 2018-4-11 14:02:57 | 显示全部楼层
longjianghu 发表于 2012-4-2 12:29
你可以在config目录的constants.php里定义,全局啊亲

constants.php中怎么写路径的啊,不能用base_url了?难道要写全路径
发表于 2018-5-8 17:47:15 | 显示全部楼层
本帖最后由 52lin 于 2018-5-8 17:54 编辑
CI_TC 发表于 2018-4-11 14:02
constants.php中怎么写路径的啊,不能用base_url了?难道要写全路径

那就写个helper啊,
遇到问题要思考,结合现有的“工具”去想办法实现。CI都把工具给你了,还不会用?
statics_helper.php
PHP复制代码
 
if( ! function_exists('js_url')){
        function js_url($filename, $path='')
        {
                if( ! function_exists('base_url')){
                        $CI = & get_instance();
                        $CI->load->helper('url');
                }
                $file = ($path != '' ? $path : 'statics/js/').$filename.'.js';
                $uri = base_url($file);
               
                return $uri;
        }
}
 
 
if( ! function_exists('css_url')){
        function css_url($filename, $path='')
        {
                if( ! function_exists('base_url')){
                        $CI = & get_instance();
                        $CI->load->helper('url');
                }
                $file = ($path != '' ? $path : 'statics/css/').$filename.'.css';
                $uri = base_url($file);
                       
                return $uri;
        }
}
 
 
if( ! function_exists('img_url')){
        function img_url($filename, $path='')
        {
                if( ! function_exists('base_url')){
                        $CI = & get_instance();
                        $CI->load->helper('url');
                }
                $file = ($path != '' ? $path : 'statics/images/').$filename;
                $uri = base_url($file);
                       
                return $uri;
        }
}
 
复制代码

PHP复制代码
 
echo js_url('jquery'); // http://a.b.com/statics/js/jquery.js
echo css_url('main'); // http://a.b.com/statics/css/main.css
echo img_url('img.png'); // http://a.b.com/statics/images/img. png
 
echo js_url('jquery', 'static2018/js'); // http://a.b.com/static2018/js/jquery.js
echo css_url('main', 'static2018/css'); // http://a.b.com/static2018/css/main.css
echo img_url('img.png', 'static2018/images'); // http://a.b.com/static2018/images/img. png
 
复制代码





本版积分规则