The helper file contains some predefined functions that are used to work with URL, such as generate the current file path, transfer control, redirect, create a link, etc.

Load the URL Helper

Before using the URL helper in the CodeIgniter application, you must load it in the controller file such as:

Syntax

Functions of URLs Helper:

  1. site_url():  It is used to return the site URL that you have defined in the config file. You can pass any string or an array that you want to appear on the browser.

Syntax

It has two parameters:

$url: It defines a URL string or an array that you can pass to the site_url () function.

$protocol: It defines the protocol such as ‘http’ or ‘https’ and if you don’t want to show any protocol before the URL, you can use NULL value.

Example

  • base_url(): A base_url () is used to return the site base URL that you set in the config file. It is similar to site_url () function used to return the site URL. However, site_url () returns the URL with index.php or url_suffix, that you defined in the config file, but base_url () returns only the URL without index.php.

Syntax

It has two parameters:

$url: It defines a URL string or an array that you can pass to the base_url () function.

$protocol: It defines the protocol such as ‘http’ or ‘https’ and if you don’t want to show any protocol before the url, you can use NULL value.

Example

  • current_url(): It is used to return the full URL of the current page with the controller’s name and the function.

Syntax

Example

  • uri_string(): It is used to fetch the URL segments of any page by defining the name of the url_string() function on any page.

Syntax

Example:

  • index_page(): It is used to return the value of the index_php, which you have set in the config file.

Syntax

Example

  • anchor() : It is used to create a link on the web page.

Syntax

It has three parameters:

$uri: It consists of a string or an array segment that can be appended to the URL.

$title: In this field, you can define the title of the link that can jump to the specific URLs.

$attributes: It contains a list of attributes that you want to display such as ‘the title of the link when the user places the mouse over the link’, etc.

Example

Create a controller file Online.php and save it in application/controller/Online.php. After that, write the following program in your controller file.

Online.php

When you execute the above program in localhost by invoking the URL localhost/CodeIgniter-3.1.11/index.php/online/url_helper function, it shows the output, as shown below.

URL Helper

When you click on the ‘Click me’ link, it shows the output as shown below.

URL Helper
  • anchor_popup(): This function is used to create a new popup window when the user clicks on the given link.

Syntax

It has three parameters:

$uri: It consists of a string or an array segment that can be appended to the URL.

$title: In this field, you can define the title of the link that can jump to the specific URLs.

$attributes: It contains a list of attributes that you want to display, such as ‘the title of the link, window_name, status, height, width, etc.

Example

Create a controller file Online.php and save it in application/controller/Online.php. After that, write the following program in your controller file.

Online.php

When you execute the above program in localhost by invoking the URL localhost/CodeIgniter-3.1.11/index.php/online/url_helper function, it shows the output, as shown below.

URL Helper

After that click on ‘Click Me!’ link, it will show a popup screen.

URL Helper
  • mailto(): It is used to create a standard email link.

Syntax

It has three parameters:

$email: In this field, you can define an email address.

$title: In this field, you can define the title of the link that can jump to the specific URLs.

$attributes: If you want to show any additional attributes, you can define it in this field.

Example

  • safe_mailto(): It is similar to the mailto () function for creating mail links, but it uses JavaScript that helps to secure an email address from spam bots.

Syntax

It has three parameters:

$email: In this field, you can define an email address.

$title: In this field, you can define the title of the link that can jump to the specific URLs.

$attributes: If you want to show any additional attributes, you can define it in this field.

Example

  1. auto_link(): As the name defines, this function is used to create an auto-link of the given string automatically.

Syntax

It has three parameters:

$str: It defines an input string for generating a link.

$type: It defines the link type such as ‘email’, ‘URL’ or ‘both’.

$popup: It allows the user to create a popup links.

Example

  1. url_title(): It is used to create a human friendly URL of the given string.

Syntax

It has three parameters:

$str: It takes an input string for creating a URL.

$separator: It is a word delimiter that is used to separate a string or statement. By default, it uses dashes (-). You can also use underscore ( _ ) for separating a string.  

$lowercase: It defines whether you want to convert the output string to lowercase.

Example

  1. prep_url(): It is used to add the prefix http:// to a protocol that is missing in the URL.

Syntax

$str: It defines a URL string that pass in prep_url() function.

Example

  1. redirect(): It is used to redirect you to any method or page of the application. In other words, it is similar to the base URL function that is used to transfer control from one method to other methods of the application.

Syntax

It has three parameters:

$uri: It contains a URL string to transfer the control to another method.

$method: It is an optional parameter that allows the user to follow a particular method strictly.

$code: It is also an optional parameter that allows the user to send a specific HTTP response code. It uses 301, 302, and 303 code to redirect with a search engine, location redirects, etc. By default, it uses a 302 responsive code. It is used to redirect the location, not for refreshing.

Example

Create a controller file Online.php and save it in application/controller/Online.php. After that, write the following program in your controller file.

Online.php

When you execute the above program in localhost by invoking the URL localhost/CodeIgniter-3.1.11/index.php/online/url_helper function. It shows the output, as shown below.

URL Helper

When the above URL will execute, it automatically redirects to Welcome/test page, as shown below.

URL Helper

Example:

Create a controller file Online.php and save it in application/controller/Online.php. After that, write the following program in your controller file.

Online.php

When you execute the above program in localhost by invoking the URL localhost/CodeIgniter-3.1.11/index.php/online/url_helper function, it shows the output, as shown below.

URL Helper

Pin It on Pinterest

Share This