dongtou2097 2015-05-26 20:08
浏览 79
已采纳

在CodeIgniter中添加URL变量

I want to add a new URL variable on CI 3.0 like site_url or base_url.

For example; I want to add an admin_url variable for administration area and assets_url variable for assets.

I checked CI 3.0 guide but couldn't find a solution.

Thanks in advance.

  • 写回答

4条回答 默认 最新

  • dongting3135 2015-05-26 21:09
    关注

    I got the answer now,

    add these lines on system/helpers/url_helper.php file:

    if ( ! function_exists('admin_css'))
    {
        /**
         * Base URL
         *
         * Create a local URL based on your basepath.
         * Segments can be passed in as a string or an array, same as site_url
         * or a URL to a file can be passed in, e.g. to an image file.
         *
         * @param   string  $uri
         * @param   string  $protocol
         * @return  string
         */
        function admin_css($uri = '', $protocol = NULL)
        {
            return get_instance()->config->admin_css($uri, $protocol);
        }
    }
    

    and add these lines on system/core/Config.php

    public function admin_css($uri = '', $protocol = NULL)
        {
            $base_url = base_url('assets/staff/css').'/';
    
            if (isset($protocol))
            {
                $base_url = $protocol.substr($base_url, strpos($base_url, '://'));
            }
    
            if (empty($uri))
            {
                return $base_url.$this->item('index_page');
            }
    
            $uri = $this->_uri_string($uri);
    
            if ($this->item('enable_query_strings') === FALSE)
            {
                $suffix = isset($this->config['url_suffix']) ? $this->config['url_suffix'] : '';
    
                if ($suffix !== '')
                {
                    if (($offset = strpos($uri, '?')) !== FALSE)
                    {
                        $uri = substr($uri, 0, $offset).$suffix.substr($uri, $offset);
                    }
                    else
                    {
                        $uri .= $suffix;
                    }
                }
    
                return $base_url.$this->slash_item('index_page').$uri;
            }
            elseif (strpos($uri, '?') === FALSE)
            {
                $uri = '?'.$uri;
            }
    
            return $base_url.$this->item('index_page').$uri;
        }
    

    don't forget to autoload url_helper. With this way, you can use admin_css variable like this: <?php echo admin_css('foo.css'); ?>

    If you use the other answers on this post, you can not use like <?php echo admin_css('foo.css'); ?>

    Thank you all.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同