Home » CodeIgniter url

CodeIgniter URL

CodeIgniter URLs are SEO friendly. Instead of using a ‘query-string’ approach, it uses a segment-based approach.


Basic URL structure

abc.com/class/function/ID

class represents controller class that needs to be invoked.

function is the method that is called.

ID is any additional segment that is passed to controllers.


What is site_url();

You can pass a string or an array in a site_url() function. In this example we’ll pass a string,

echo site_url(‘book/novel/fiction’);

The above function will return something like this

http://abc.com/index.php/book/novel/fiction

In this example we’ll pass an array,

$data = array(‘book’, ‘novel’, ‘fiction’);

echo site_url($data);


What is base_url();

It returns your site base URL, if mentioned any, in the config file. On passing base_url(), it also returns the same thing as site_url() with eliminating index.php. This is useful because here you can also pass images or text files. Here also you can pass a string or an array.

In this example we’ll pass a string,

echo base_url(“book/novel/fiction”);

The above function will return something like this http://abc.com/ book/novel/fiction


What is uri_string();

It returns the URI segment of a page. For example, if your URL is,

http://abc.com/book/novel/fiction

Then, uri_string() will return

Book/novel/fiction


What is current_url();

Calling this function means, it will return the full URL of the page currently viewed.

Please note -> calling this function is same as calling uri_string() in site_url().

current_url() = site_url(uri_string());


What is index_page();

It will return your site’s index_page which you have mentioned in your config file. By default, it is always index.php file.

You can change it with the help of .htaccess file.


anchor()

It creates a standard HTML link based on your local site URL. For example,

Echo anchor(‘book/novel/fiction’, ‘My Collection, ‘title=”book name”);

It will give the following result,

My Collection


anchor_popup()

It is identical to anchor() but it opens the URL in a new window.


mailto()

It creates a HTML email link. For example,

Echo mailto(‘[email protected]_site.com’, ‘To contact me click here’)


url_title()

It takes a string as an input and creates a human friendly environment. For example,

   $title = "CodeIgniter's examples"  $url_title() = url_title($title); 

Output will be “CodeIgniters-examples”

If you’ll pass a second parameter, it defines word delimiter.

   $title = "CodeIgniter's examples"  $url_title() = url_title($title, '_'); 

Output will be “CodeIgniters_examples”

If you’ll pass a third parameter, it defines uppercase and lowercase. You have Boolean options for this, TRUE/FALSE.

   $title = "CodeIgniter's examples"  $url_title() = url_title($title, '_', TRUE); 

Output will be “codeigniters_examples”

You may also like