2

I want to access my site url or base url from my controller in codeigniter 3. I tried $this->url->site_url() and $this->config->site_url(). None worked. Is it even possible?

Nicholas Kajoh
  • 1,451
  • 3
  • 19
  • 28

1 Answers1

4

Load URL Helper and function site_url()

This helper is loaded using the following code:

$this->load->helper('url');
echo site_url();

site_url()

Returns your site URL, as specified in your config file. The index.php file (or whatever you have set as your site index_page in your config file) will be added to the URL, as will any URI segments you pass to the function, and the url_suffix as set in your config file.

You are encouraged to use this function any time you need to generate a local URL so that your pages become more portable in the event your URL changes.

Segments can be optionally passed to the function as a string or an array. Here is a string example:

echo site_url("news/local/123");

The above example would return something like:
http://example.com/index.php/news/local/123

Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268