StackOverflowNewbie
StackOverflowNewbie

Reputation: 40653

CodeIgniter - how to access base_url() in a helper?

I'm extending URL helper (MY_url_helper). How do I access base_url() within my helper function? I'm actually overriding base_url(), so I need to call the original base_url.

Edit 1:

This doesn't work:

$CI =& get_instance();
$CI->load->helper('url');
$base_url = $CI->base_url();

Upvotes: 3

Views: 3081

Answers (1)

complex857
complex857

Reputation: 20753

Unfortunately built-in helper functions are defined in the global php namespace, once you define a function with a name base_url you won't be able to define another one with the same name, so you can't load the original base_url function from the original helper "somwehere else" and use it.

For this reason CI's built-in helper files define function in if blocks like this:

if ( ! function_exists('FUNCTION_NAME'))
// ...
}

so even if you load the original helper file in your overridden version it doesn't create a fatal error, but also doesn't do anything meaningful.

In your concrete case, the base_url's implementation is basically a:

return get_instance()->config->base_url($uri);

you can lift it into your overridden version of base_url.

Upvotes: 2

Related Questions