Codeigniter - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

CodeIgniter URL

CodeIgniter URL

shape Introduction

CodeIgniter has more than 20 built-in helpers. A new controller can be created or existing controller can be used for importing one of the built-in Codeigniter Helpers. Presently in "CodeIgniter URL" tutorial, URL helper is loaded and used. CodeIgniter URL helper functions helps in creating, managing and call the URL. Step-1 : CodeIgniter applications involves loading helper classes before using. Most preferred way is to load URL helper in the applications is using "autoload.php" file which reduces the burden of loading in each controller constructor. Otherwise, URL helper can be loaded separately in any controller constructor. Open welcome.php file located in application/controllers. Write the following code in index() function which is provided by default.
$this->load->helper("url");
Step-2 : Next open controller view file welcome_message.php located at application/views folder and add the below code. [html] <div id="container"> <h1>Welcome to CodeIgniter!</h1> <div id="body"> <p>Base URL : <?=base_url()?></p> <p>Site URL : <?=site_url()?></p> </div> </div> [/html] Output:

CodeIgniter URL Helper Function

shape Description

base_url()

The base URL is returned by this function. [php]echo base_url();//default echo base_url (“css/logo.png″);//call image or file in another folder[/php]

site_url()

The site URL is returned by this function. [php]echo site_url(); //output - http://localhost/codeigniter/index.php [/php]

current_url

The present URL full path is returned by this function. [php]echo current_url();//output - http://localhost/codeigniter/[/php]

anchor()

Standard HTML anchor link is created by this function on the local site URL.It has three parameters text,uri segments and attributes. [php]// Syntax for anchor. anchor(uri segments, text, attributes); echo anchor(http://localhost/codeigniter/index.php/home/, 'Click Here', 'title="This is anchor"'); //output <a href="http://localhost/codeigniter_url_helper/index.php/home" title="This is anchor"> Click Here</a>[/php]

anchor_popup()

A popup is created in a new window by this function. [php]anchor_popup(uri segments, text, attributes); $atts = array( 'width' => '800', 'height' => '600', 'scrollbars' => 'yes', 'status' => 'yes', 'resizable' => 'yes', 'screenx' => '0', 'screeny' => '0' ); echo anchor_popup(http://localhost/codeigniter/index.php/home, 'Anchor popup', $attributes ); [/php]

mailto()

Email can be sent directly from a script using this function and it can be visible in view source. A standard output with HTML tag appears. [php]echo mailto('me@my-site.com', 'Click Here to Contact Me'); //output <a href="mailto:me@my-site.com">Click Here to Contact Me</a>[/php]

safe_mailto()

Emails can be sent by encoding it as a link address to avoid spam bots. [php]echo safe_mailto('me@my-site.com', 'Click Here to Contact Me');[/php]

auto_link()

This function will convert the Emails and URL's into links. [php]$string = auto_link($string); echo auto_link("http://www.splessons.com"); $string = auto_link($string, 'url');//Converts only URLs[/php]

prep_url()

This function adds 'http://' in the front of website url. [php]$url = "splessons.com"; echo prep_url($url); http://splessons.com //output[/php]

redirect()

The function moves the given url to specified location.

Summary

shape Key Points

  • CodeIgniter URL helper functions helps in creating, managing and call the URL.
  • $this->load->helper("url"); is used to load the URL.