3

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 Answers 4

4

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 :)

Sign up to request clarification or add additional context in comments.

1 Comment

first, welcome to stack overflow, second this is the best answer
0

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

7 Comments

Yeah, actually I thought that but it doesn't look like the perfect solution but thanks for your help :)
what kind of perfect solution, do you need , can you explain Deniz B. ?
Actually I found some information on web, they're looking more complicated and I'm confused; if it is easily as this, why they choose complicated way? I was using this way but now I'm in doubt that what if this is not the best way for it?
@pr0metheus you missed ; after return base_url() . "/admin/", I can not edit because edits should be more than 6 characters. Can you fix it?
Fixed. This is very good solution. You should use it without any problems.
|
0

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

Comments

0

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.

1 Comment

glad it worked out for you, but it's not a good idea to modify the core files, try always to extend them

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.