CodeIgniter’s URL helpers are groups of utility functions which will help you to call ,create and maintain url.

It mainly have more than 20 helpers some of them you might be familiar with are URL, email, form etc. These are some common helper functions that generaly used in web based application for email, files, URLs.

You will feel the need of these CodeIgniter helper functions when you have to repeat lots of code like calling base URL, formatting dates, sending emails.

In this tutorial we will give you brief description about how to use “URL Helper” in CodeIgniter which will help you in making ,calling and creating url.

Loading Helper 

Load URL helper in your controller’s constructor, write following code:

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

Here we are demonstrating an example to use the codeigniter url function.
You can also refer our live demo or download the Script file. Extract the downloaded files and run it on your Local server.

The URL Helper file contains functions that assist in working with URLs, The functions are define below.

base_url()

This function will returns your base URL.

echo base_url();

This would give you codeigniter root folder path.

http://localhost/codeigniter_url_helper/

If you want to call image or file in another folder, You have paas the parameter or array inside the function as given.

base_url (“css/logo.png″);
// Or you can pass array, this would also append the path in base_url().
$segments = array('index.php', 'url_controller', 'test');
base_url (“$segments ″);

output

http://localhost/codeigniter_url_helper/css/logo.png

http://localhost/codeigniter_url_helper/index.php/url_controller/test

site_url()

It return site url.

echo site_url();

This would give you codeigniter root folder path with “index.php”.

http://localhost/codeigniter_url_helper/index.php 

This function not work for call another folder which is presence in root folder , when you want the “url” following “index.php” then you may use site_url().

site_url("url_controller/test");

//you can pass array, this would also append the path in site_url().
$segments = array( 'url_controller', 'test');
echo site_url ($segments);

output

http://localhost/codeigniter_url_helper/index.php/url_controller/test

current_url()

This function would return the full path of the page being currently viewed.

echo current_url();
http://localhost/codeigniter_url_helper/index.php/url_controller/test

anchor()

This tag has three  parameters uri segments, text and attributes.

// Syntax for anchor.
anchor(uri segments, text, attributes);
echo anchor(http://localhost/codeigniter_url_helper/index.php/url_controller/anchor, 'Click Here', 'title="This is anchor"');

echo anchor('http://localhost/codeigniter_url_helper/index.php/url_controller/anchor', 'Click Here', array('title' => 'This is anchor!'));

Above function creates a standard HTML anchor link based on your local site URL

<a href="http://localhost/codeigniter_url_helper/index.php/url_controller/anchor" title="This is anchor"> Click Here</a>

anchor_popup()

This function will create a popup in new window. if you want to set property for new window, you can paas an array in third parameter.

anchor_popup(uri segments, text, attributes);

$attributes = array(
'width' => '500',
'height' => '500',
'scrollbars' => 'yes',
'status' => 'yes',
'resizable' => 'yes',
'screenx' => '0',
'screeny' => '0'
);

echo anchor_popup(http://localhost/codeigniter_url_helper/index.php/url_controller, 'Anchor popup', $attributes ); 

mailto()

The mailto () function allows you to send emails directly from a script and visible in view source.

echo mailto('[email protected]', 'Click Here to Contact Me');

This function would produce the standard HTML tag output.

<a href="mailto:[email protected]">Click Here to Contact Me</a>

safe_mailto()

The safe_mailto () function allows you to send emails directly from a script .
This function are encoded the link address from being harvested by spam bots.

echo safe_mailto('[email protected]', 'Click Here to Contact Me');

This function would produce the encoded script in view source.

<script type="text/javascript">

//<![CDATA[
var l=new Array();
l[0]='>';l[1]='a';l[2]='/';l[3]='<';l[4]='|41';l[5]='|108';l[6]='|105';l[7]='|97';l[8]='|109';l[9]='|95';l[10]='|101';l[11]='|102';l[12]='|97';l[13]='|115';l[14]='|40';l[15]='|32';l[16]='|101';l[17]='|114';l[18]='|101';l[19]='|72';l[20]='|32';l[21]='|107';l[22]='|99';l[23]='|105';l[24]='|108';l[25]='|67';l[26]='>';l[27]='"';l[28]='|109';l[29]='|111';l[30]='|99';l[31]='|46';l[32]='|101';l[33]='|116';l[34]='|105';l[35]='|115';l[36]='|45';l[37]='|121';l[38]='|109';l[39]='|64';l[40]='|101';l[41]='|109';l[42]=':';l[43]='o';l[44]='t';l[45]='l';l[46]='i';l[47]='a';l[48]='m';l[49]='"';l[50]='=';l[51]='f';l[52]='e';l[53]='r';l[54]='h';l[55]=' ';l[56]='a';l[57]='<';
for (var i = l.length-1; i >= 0; i=i-1){
if (l[i].substring(0, 1) == '|') document.write("&#"+unescape(l[i].substring(1))+";");
else document.write(unescape(l[i]));}
//]]>
</script>

auto_link()

Automaticaly, given URLs and EMAIL as string, turns into  a link.

$string = auto_link($string);

echo auto_link("https://www.formget.com");

The second parameter determines that both URLs and Email are converted into link or one or the other,  Email links are encoded the link address from being harvested by spam bots.

Converts only URLs:

$string = auto_link($string, 'url');

Converts only Email addresses:

$string = auto_link($string, 'email');

The third parameter determines whether links are shown in a new window. The value can be TRUE or FALSE (boolean):

$string = auto_link($string, 'both', TRUE);

prep_url()

This function will add http:// 

$url = "formget.com";
echo prep_url($url);

This would produse the output

http://formget.com

redirect()

This function first parameter is “uri segment”, you would give the link which you want to refresh or redirect, second is “location”, and third is “http responce code” .

 // Refresh the page.

redirect('/login/form/', 'refresh');

// Redirect the page with 301 response code.

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

Here you can also refer complete code.

Controller File : url_controller.php

Copy the below code in your controller.just load url class in constructor.

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Url_Controller extends CI_Controller {

// Load Library.
function __construct() {
parent::__construct();
$this->load->helper('url');
}

// View "url_view" Page.
public function url_demo() {
$this->load->view('url_view');
}

public function index() {
echo "Hello";
}
}
?>

View File : url_view.php

Copy the below code in your view.Here we are calling url class’s functions

<html>
<head>
<title>CodeIgnigter url Helper</title>
<link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>css/style.css">
<link href='http://fonts.googleapis.com/css?family=Source+Sans+Pro|Open+Sans+Condensed:300|Raleway' rel='stylesheet' type='text/css'>

</head>
<body>
<div class="main">
<h2>CodeIgniter's URL Helper</h2>
<div id="content">
<div id = "table_data">
<table >
<tr>
<td>
Function
</td>
<td >
Output
</td>

</tr>
<tr>
<td >
base_url()
</td>
<td>
<?php echo base_url(); ?>
</td>

</tr>
<tr>
<td >
base_url ("css/logo.png")
</td>
<td>

<?php echo base_url ("css/logo.png"); ?>

</td>

</tr>
<tr>
<td >
site_url()
</td>
<td>
<?php echo site_url(); ?>
</td>

</tr>
<tr>
<td >
site_url("url_controller/test")
</td>
<td>
<?php echo site_url("url_controller/test"); ?>
</td>

</tr>
<tr>
<td >
current_url()
</td>
<td>
<?php echo current_url(); ?>
</td>

</tr>
<tr>
<td >
anchor()
</td>
<td>
<?php echo anchor(base_url("index.php/url_controller/index"), 'Click Here', array('title'=>'This is anchor','target'=> '_blank' )); ?>
</td>

</tr>
<tr>
<td >
anchor_popup()
</td>
<td>
<?php $attributes = array(
'width' => '500',
'height' => '500',
'scrollbars' => 'yes',
'status' => 'yes',
'resizable' => 'yes',
'screenx' => '0',
'screeny' => '0'
);
echo anchor_popup(base_url("index.php/url_controller"), 'Anchor popup', $attributes); ?>
</td>
</tr>
<tr>
<td >
mailto()
</td>
<td>
<?php echo mailto('[email protected]', 'Click Here to Contact Me'); ?>
</td>
</tr>
<tr>
<td >
safe_mailto()
</td>
<td>
<?php echo safe_mailto('[email protected]', 'Click Here (safe_mail)'); ?>
</td>
</tr>
<tr>
<td >
auto_link()
</td>
<td>
<?php echo auto_link("https://www.formget.com"); ?>
</td>
</tr>
<tr>
<td >
prep_url()
</td>
<td>
<?php $url = "formget.com";
echo prep_url($url); ?>
</td>
</tr>

</table>
</div>
</div>
</div>
</body>
</html>

CSS File : style.css

Styling HTML Elements.

body {
font-family: 'Open Sans', sans-serif;
}
.main
{
width: 1015px;
height: 900px;
position: absolute;
top: 10%;
left: 20%;
}

#content {
position: absolute;
width: 850px;
height: auto;
}
#table_data{
border-collapse: collapse;
border-spacing: 0;

}
#table_data {
margin:0px;padding:0px;
width:116%;
border:1px solid white;

}
#table_data table{
border-collapse: collapse;
border-spacing: 0;
width:100%;
height:50%;
margin:0px;padding:0px;
}
#table_data td{
vertical-align:middle;
border:1px solid white;
border-width:0px 1px 1px 0px;
text-align:left;
padding:18px;
font-size:20px;
background-color: #DBF6ED;
color: #4f4f4f;
}
#table_data tr:first-child td{
background-color:#26C489;
border:0px solid #26C489;
border-width:0px 0px 1px 1px;
font-size:24px;
font-weight:bold;
color:#ffffff;
}
#table_data td{
padding-left:40px;
font-weight:bold;
font-size:15px;
}
a {

color: #4f4f4f;
}
h2{
padding-left:328px;
}

Conclusion

Thanks for reading the complete post. Hope you have got the concept. You can share your views in the space provided below and get in touch with us.