正在CodeIgniter上添加URL变量


Adding URL Variable on CodeIgniter

我想在CI 3.0上添加一个新的URL变量,如site_urlbase_url

例如;我想为管理区域添加admin_url变量,为资产添加assets_url变量。

我查看了CI 3.0指南,但找不到解决方案。

提前谢谢。

它非常直接。

只需转到位于/your_root/application/config目录的config.php

在该文件底部的这一行添加

$config["admin_url"] = "http://www.your_url.com/admin";
$config["assets_url"] = "http://www.your_url.com/assets";

要在应用程序中的任何位置检索,请使用此

$your_admin_variable =$this->config->item("admin_url");
$your_assets_variable =$this->config->item("assets_url");

您的业务范围:(

我的解决方案。

创建新的帮助程序文件并将此文件添加到自动加载中。

所以。。

创建文件application/helpers/global_helper.php

在助手内部创建函数,例如:

<?php
    function admin_url(){
        return base_url() . "/admin/";
    }

现在编辑config/autoload.php

$autoload['helper'] = array('url','global');

将您的新助手添加到现有数组中。

现在,您可以在任何地方使用函数admin_url

在system/helpers/ul_helper.php中,您可以找到

if ( ! function_exists('base_url'))
{
    function base_url($uri = '')
    {
        $CI =& get_instance();
        return $CI->config->base_url($uri);
    }
}

所以我想如果你创建自己的代码像这个

if ( ! function_exists('your_variable_name'))
{
    function your_variable_name($uri = '')
    {
        $CI =& get_instance();
        return $CI->config->your_variable_name($uri);
    }
}

但是最好是扩展helper,而不是修改它,所以您可以在application/helpers/MY_url_helper.php中使用上面的代码然后可以像通常使用baseurl那样调用自定义变量希望对有所帮助

我现在得到了答案,

system/helpers/url_helper.php文件上添加以下行:

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);
    }
}

并在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;
    }

不要忘记自动加载url_helper。通过这种方式,您可以像这样使用admin_css变量:<?php echo admin_css('foo.css'); ?>

如果你在这篇文章中使用其他答案,你就不能像<?php echo admin_css('foo.css'); ?> 那样使用

谢谢大家。