chhameed
chhameed

Reputation: 4456

codeigniter base_url change from controller

Is there any way to set my base_url() from my controller's ?

OR Can I set my base_url Dynamic ?

How can I achieved this ?

Upvotes: 1

Views: 11424

Answers (3)

chhameed
chhameed

Reputation: 4456

After Searching i found the solution . yes we can change the base_url from our controller as

$this->config->set_item('base_url','http://example.com/xyz') ;

Ref: User Guide

May this answer should help some one .

Upvotes: 15

stormdrain
stormdrain

Reputation: 7895

OK, so this was fun, but it might not work right and/or break all kinds of other stuff. But, if you make these changes in the relevant files, you can have multiple base url config settings in the config file like so:

$config['base_url']['default']  = 'http://firstbase.xyz';
$config['base_url']['otherbase']    = 'http://secondbase.xyz';

which can be called like base_url('','default');//produces http://firstbase.xyz.

It seems much easier/better to use $this->config->set_item('base_url','http://abc.com/xyz') ; as you found in the docs.


system/helpers/url_helper.php : line ~63

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

system/core/Config.php

line ~66

$this->set_item('base_url', $index);

line ~175

function item($item, $index = '')
{
    if ($index == '')
    {
        if ( ! isset($this->config[$item]))
        {
            return FALSE;
        }

        $pref = $this->config[$item];
    }
    else
    {
        if ( ! isset($this->config[$index]))
        {
            return FALSE;
        }

        if ( ! isset($this->config[$index][$item]))
        {
            return FALSE;
        }

        $pref = $this->config[$index][$item];
    }

    return $pref;
}

line ~214

function slash_item($item,$index)
{
    if ( ! isset($this->config[$item][$index]))
    {
        return FALSE;
    }
    if( trim($this->config[$item][$index]) == '')
    {
        return '';
    }
    return rtrim($this->config[$item][$index], '/').'/';
}

line ~265

function base_url($uri = '',$index='')
{
    return $this->slash_item('base_url',$index).ltrim($this->_uri_string($uri),'/');
}

line ~332

function set_item($item, $value, $index)
{
    $this->config[$item][$index] = $value;
}

Upvotes: 2

swatkins
swatkins

Reputation: 13630

The base url is set in your config file, so you can update the $config variable from your controllers before you call base_url().

http://codeigniter.com/forums/viewthread/60181/

EDIT

Of course, I haven't tested this, so don't know if the overwrite will actually work as expected.

You could always extend the url helper with your own class and override the base_url method.

Upvotes: 2

Related Questions