Memahami Lebih dalam Tentang Helper URL di Codeigniter
78 / 100

What is URL Helper ?

Deep Understanding About URL Helper in Codeigniter

Deep Understanding About URL Helper in Codeigniter, URL Helper is a helper provided by Codeigniter which contains functions for processing URLs. 1

According to me:

This helper is the most frequently used helper in the application.

Helper URL we can use in Controller and View.

If it's in the model, maybe not??

It's possible.


That's not a good practice.

Because the model's job is to deal with data.

Then how to use the URL Helper?

Let's discuss:

How to Use the URL Helper

Helper URL we can use after loading the helper.



We can do load helper inside the controller.


Codeigniter URL Helper 4 already loaded automatically, so we don't need to load manually.

If you want it to be automatically loaded on Codeigniter 3, then we can specify in the file autoload.php like this:

$autoload['helper'] = array('url');

After successfully loading, then we can use the functions provided by the URL helper.

What are the functions??

Let's discuss one-by-one:

Functions in the URL Helper

The URL Helper provides several functions that make it easier for us when dealing with URLs.

1. Function site_url()

The site URL function will return a value in the form of a URL based on the configuration base_url and index_page which is already specified in the config.


For example, the configuration in our application is like this:

$config['base_url'] = '';
$config['index_page'] = 'index.php';

So when we use the function site_url(), it will produce:

When should we use site_url()?

Use the function site_url() moment…

Creating a link to a specific page:

<a href="<?= site_url('login') ?>">Login</a>

Redirect to a certain page:


Create an action URL on the form:

<form action="<?= site_url('login') ?>">


2. Function base_url()

Function base_url() almost the same as site_url() which generates URL based on configuration base_url. But, base_url() not include index.php behind him.

Function base_url() serves to generate the root URL of the application.

For example, the configuration in our application is like this:

$config['base_url'] = '';
$config['index_page'] = 'index.php';

So when we use the function base_url(), it will produce:

When should we use base_url()?

Use it base_url()

When creating links/URLs for CSS and Javascript:

<link href="<?= base_url('css/style.css') ?>" />
<script src="<?= base_url('js/app.js') ?>"></script>

Then it will generate a URL like this:

Function base_url() Usually we often use it to create URLs or static file links such as images, CSS, javascript, Video, and uploaded files.

What is the difference base_url() with site_url()?

Based on the example I gave, looks like you can already guess what's the difference site_url() with base_url().

Do! The difference lies in the URL that is generated.

Take a look at this:

// hasil ->

// hasil ->

In this case, we use base_url() and site_url() to create CSS links.

The results will of course be different.

A valid link is a link that uses base_url(). Whereas site_url() add index.php which will make the CSS file link incorrect and will definitely not be found.

Because of that, to create a link static assets use it base_url() no site_url().

3. Function current_url()

This function will return the currently opened URL.

For example we are opening the following URL:

Then when calling the function current_url(), it will generate URL exactly like above.

⚠️ Important Warning!

This function contains a bug in Codeigniter 4.1.2, and can be replaced with a function site_url(uri_string())

4. Function uri_string()

This function will generate a URI segment based on the current page.

Suppose we are opening this page:

Then the function uri_string() will produce:


This function is usually used when we want to create breadcrumb.

5. Function index_page()

This function will return value based on configuration index_page.

Suppose the configuration is like this:

$config['index_page'] = 'index.php';

Then the function index_page() will produce:


6. Function anchor()

This function will generate link element. Usually used to create links.


echo anchor('news/local/123', 'My News', 'title="News title"');
// Hasil: <a href="" title="News title">My News</a>

echo anchor('news/local/123', 'My News', array('title' => 'The best news!'));
// Hasil: <a href="" title="The best news!">My News</a>

echo anchor('', 'Click here');
// Hasil: <a href="">Click Here</a>

7. Function anchor_popup()

This function will generate a link to open a popup window.


$atts = array(
        'width'       => 800,
        'height'      => 600,
        'scrollbars'  => 'yes',
        'status'      => 'yes',
        'resizable'   => 'yes',
        'screenx'     => 0,
        'screeny'     => 0,
        'window_name' => '_blank'

echo anchor_popup('news/local/123', 'Click Me!', $atts);

So when the link is clicked, page news/local/123 will open in a new pop window.

8. Function mailto()

Function mailto() will generate a link for the email address. When the link is clicked, it will open the mail client application.


echo mailto('', 'Click Here to Contact Me');

// atau bisa juga:
$attributes = array('title' => 'Mail me');
echo mailto('', 'Contact Me', $attributes);

9. Function safe_mailto()

Same as mailto() which generates the link for the email. However, the email address is protected, so it will be safer from spam bots.


echo safe_mailto('', 'Click Here to Contact Me');

If this is a combination of the function no. 6, 7, and 9. Function auto_link() will generate a link from the given parameters.

Parameters can be email addresses or URLs.


echo auto_link('');
// hasil: <a href=""></a>

echo auto_link('');
// hasil: <a href=""></a>

11. Function url_title()

This function generates a string URL based on the text provided in the parameter.

$title = "What's wrong with CSS?";
$url_title = url_title($title);
// Hasil: Whats-wrong-with-CSS

Usually this function is used to create URLs like slug.

Function url_title() can also generate URL with underscore.


$title = "What's wrong with CSS?";
$url_title = url_title($title, 'underscore');
// Hasil: Whats_wrong_with_CSS

12. Function prep_url()

This function will add http:// on the given URL parameter.


echo prep_url('');
// hasil

13. Function redirect()

This function to do redirect or redirect to a specific page.

Examples of usage:


// with 301 redirect
redirect('/article/13', 'location', 301);

URL Helper Function in Codeigniter 4

URL Helper Function in Codeigniter 4 not much different from Codeigniter 3. It's just that there are some additional functions.

Here are the URL helper functions added in Codeigniter 4: 2

1. Function previous_url()

Function previous_url() is a function that returns the URL address of the previously visited page.

For example:

I opened this page,

Then move on to

Nah, if we run previous_url() on the login page..

echo previous_url();

Then it will produce:

2. Function url_to()

This function will return an absolute URL that goes to a method in the Controller.

Usually used to create links like site_url().

Examples of usage:

echo url_to('Home::index');
// hasil:

// bisa juga dengan argumen
echo url_to('Post::list', 'home');
// hasil:

Examples of usage in View:

<a href="<?= url_to('Auth::login')">Login</a>

3. Function url_is()

Function url_is() serves to check the URL on the page that is being opened. This function will generate true if the check result is the same as specified.


if (url_is('admin')) { ... }

If we open the following pages


Then the function url_is() will produce true, because we are opening the admin page.

Usually function url_is() used to specify the active class in the menu.


	<a href="#" class="<?= url_is('home') ? 'active': '' ?>">Home</a>
  <a href="#" class="<?= url_is('about') ? 'active': '' ?>">About</a>
  <a href="#" class="<?= url_is('contact') ? 'active': '' ?>">Contact</a>

4. Function mb_url_title()

This function is the same as url_title() which serves to convert text into a URL. However, this function also changes the accent character automatically.

Examples of usage:

echo mb_url_title("my resumé");
// hasil: my-resume

Take note, there are accent letters é which is automatically changed to e.

source :

 1,376 total views,  4 views today

By admin


Leave a Reply

Your email address will not be published.

Web Hosting Murah Yang Terbaik Di Indonesia