在CodeIgniter中添加URL变量

在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.

我想在CI 3.0上添加一个新的URL变量,如 site_url code>或 BASE_URL 代码>。 p>

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

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

提前致谢。 p> div>

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.

My solution.

Create new helper file and add this file to autoload.

So..

Create file application/helpers/global_helper.php

Inside your helper create functions for example:

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

Now edit config/autoload.php

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

Add to exists array your new helper.

And now anywhere you can use your function admin_url

in system/helpers/url_helper.php you will find

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

so i guess if you create your own code like this

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

but it is better to extend the helper rather modifying it , so you can use the code above in application/helpers/MY_url_helper.php and then you can call your custom variable as you normally do it with base_url hope that helps

It's pretty straight forward.

Just go to your config.php which is located at /your_root/application/config directory

add at this line at the bottom of that file

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

To retrieve it anywhere in application use this

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

Your're in business :)