URL helper in CodeIgniter
×

URL helper in CodeIgniter

2306

URL helper contains functions that help us in working with URLs.

Related Topics:

Codeigniter Interview Questions
Security in CodeIgniter
CI_Security Class Reference

Loading this Helper:

Use the given line in your controller or model file to load the URL helper.

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

The following functions are available in this helper:

1 site_url(): This function returns the site URL defined by the user in the application/config/config.php file.

Syntax:

site_url([$uri = ''[, $protocol = NULL]]);

Parameter Description:

  • $uri (string) – URI string
  • $protocol (string) – Protocol, e.g. 'http' or 'https'

The return type of this function is string and it returns the Site URL.

Example:

Step 1 Open the application/controllers directory and create a file Site_controller.php.

<?php
class Site_controller extends CI_Controller {

public function index() {
$this->load->helper('url'); //load URL helper
$site_url=site_url();
echo "The site URL is : <strong>$site_url</strong>";
}
}
?>

Step 2 Enter the given URL into the browser to see the result.

http://localhost/ci/index.php/Site_controller


2 base_url(): This function used to get the site base URL. It returns the thing as site_url() but without the index_page or url_suffix being appended.

Syntax:

base_url($uri = '', $protocol = NULL);

Parameter Description:

  • $uri (string) – URI string
  • $protocol (string) – Protocol, e.g. 'http' or 'https'

The return type of this function is string and it returns the Base URL.

Example:

Step 1 Open the application/controllers directory and create a file Base_controller.php.

<?php
class Base_controller extends CI_Controller {

public function index() {
$this->load->helper('url'); //load URL helper
$base_url=base_url();
echo "The Base URL is : <strong>$base_url</strong>";
}
}
?>

Step 2 Enter the given URL into the browser to see the result.

http://localhost/ci/index.php/Base_controller


3 current_url(): This function is used to return the URL of the current page being executed.

Syntax:

current_url();

The return type of this function is string and it returns the current URL.

Example:

Step 1 Open the application/controllers directory and create a file Current_controller.php.

<?php
class Current_controller extends CI_Controller {

public function index() {
$this->load->helper('url'); //load URL helper
$current_url=current_url();
echo "The Current page URL is : <strong>$current_url</strong>";
}
}
?>

Step 2 Enter the given URL into the browser to see the result.

http://localhost/ci/index.php/Current_controller


4 uri_string(): This function is used to return URI segments of any page that contains this function.

Syntax:

uri_string();

The return type of this function is string and it returns a URI string.

Example:

Step 1 Open the application/controllers directory and create a file Uri_str_controller.php.

<?php
class Uri_str_controller extends CI_Controller {

public function index() {
$this->load->helper('url'); //load URL helper
$uri=uri_string();
echo "The URI segment of this page is : <strong>$uri</strong>";
}
}
?>

Step 2 Enter the given URL into the browser to see the result.

http://localhost/ci/index.php/Uri_str_controller


5 index_page(): This function is used to return the index page of the application specified by the developer in the config file.

Syntax:

index_page();

The return type of this function is mixed and it returns the 'index_page' value.

Step 1 Specified your index page into the application/config/config.php file.


Step 2 Open the application/controllers directory and create a file Index_page_controller.php.

<?php
class Index_page_controller extends CI_Controller {

public function index() {
$this->load->helper('url'); //load URL helper
$page=index_page();
echo "The Index page of my application is : <strong>$page</strong>";
}
}
?>

Step 3 Enter the given URL into the browser to see the result.

http://localhost/ci/index.php/Index_page_controller


6 anchor(): This function is used to create HTML anchor tag(<a></a>) based on your local site URL.

Syntax:

anchor($uri = '', $title = '', $attributes = '');

Parameter Description:

  • $uri (string) – URI string
  • $title (string) – Anchor title
  • $attributes (mixed)HTML attributes

The return type of this function is stringand it returns an HTML hyperlink (anchor tag).

Example:

Step 1 Open the application/controllers directory and create a file Anchor_controller.php.

<?php
class Anchor_controller extends CI_Controller {

public function index() {
$this->load->helper('url'); //load URL helper
$link=anchor('https://www.codingtag.com', 'Coding Tag', 'title="This is a link"');
echo "To go on codingtag.com click here : <strong>$link</strong>";
}
}
?>

Step 2 Enter the given URL into the browser to see the result.

http://localhost/ci/index.php/Anchor_controller


7 anchor_popup(): This function is used to create an HTML anchor tag that opens the URL in a new window.

Syntax:

anchor_popup($uri = '', $title = '', $attributes = FALSE);

Parameter Description:

  • $uri (string) – URI string
  • $title (string) – Anchor title
  • $attributes (mixed) – HTML attributes

You can specify JavaScript window attributes in the third parameter to control how the window is opened. For example:

$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);

The return type of this function is string and it returns a Pop-up hyperlink.

Example:

Step 1 Open the application/controllers directory and create a file Anchor_p_controller.php.

<?php
class Anchor_p_controller extends CI_Controller {

public function index() {
$this->load->helper('url'); //load URL helper
$atts = array(
'width' => 800,
'height' => 600,
'scrollbars' => 'yes',
'status' => 'yes',
'resizable' => 'yes',
'screenx' => 0,
'screeny' => 0,
'window_name' => '_blank'
);

$link=anchor_popup('https://www.codingtag.com', 'Coding Tag',$atts);
echo "To open codingtag.com in the new window click here : <strong>$link</strong>";
}
}
?>

Step 2 Enter the given URL into the browser to see the result.

http://localhost/ci/index.php/Anchor_p_controller


8 mailto(): This function is used to creates a standard HTML e-mail link.

Syntax:

mailto($email, $title = '', $attributes = '');

Parameter Description:

  • $email (string) – E-mail address
  • $title (string) – Anchor title
  • $attributes (mixed) – HTML attributes

The return type of this function is string and it returns a "mail to" hyperlink.

Example:

Step 1 Open the application/controllers directory and create a file Mailto_controller.php.

<?php
class Mailto_controller extends CI_Controller {

public function index() {
$this->load->helper('url'); //load URL helper
$attributes = array('title' => 'Mail us');
$m=mailto('you@your-site.com', 'Contact US', $attributes);
echo "For any query : <strong>$m</strong>";
}
}
?>

Step 2 Enter the given URL into the browser to see the result.

http://localhost/ci/index.php/Mailto_controller


9 safe_mailto(): This function is the same as the mailto() function but it writes an obscure version of the mailto() function using ordinal numbers written with JavaScript to help prevent the e-mail address from spams.

Syntax:

safe_mailto($email, $title = '', $attributes = '');

Parameter Description:

  • $email (string) – E-mail address
  • $title (string) – Anchor title
  • $attributes (mixed) – HTML attributes

The return type of this function is string and it returns a spam-safe "mail to" hyperlink.

Example:

Step 1 Open the application/controllers directory and create a file Safe_mailto_controller.php.

<?php
class Safe_mailto_controller extends CI_Controller {

public function index() {
$this->load->helper('url'); //load URL helper
$attributes = array('title' => 'Mail us');
$m=safe_mailto('you@your-site.com', 'Contact US', $attributes);
echo "For any query : <strong>$m</strong>";
}
}
?>

Step 2 Enter the given URL into the browser to see the result.

http://localhost/ci/index.php/Safe_mailto_controller


10 auto_link(): This function is used to convert URLs and e-mail addresses into links automatically.

Syntax:

auto_link($str, $type = 'both', $popup = FALSE);

Parameter Description:

  • $str (string) – Input string
  • $type (string) – Link type (‘email’, 'url' or 'both')
  • $popup (bool) – Whether to create popup links

The return type of this function is string and it returns a linkified string.

Example:

Step 1 Open the application/controllers directory and create a file Autolink_controller.php.

<?php
class Autolink_controller extends CI_Controller {

public function index() {
$this->load->helper('url'); //load URL helper
$string = auto_link("https://www.codingtag.com/", 'both', TRUE);
echo "To open codingtag.com in new tab Click Here: <strong>$string</strong>";
}
}
?>

Step 2 Enter the given URL into the browser to see the result.

http://localhost/ci/index.php/Autolink_controller


11 url_title(): This function is used to convert a string into a user-friendly URL. For example: If you have a blog in which you’d like to use the title of your entries as the URL, users can create by using this method.

Syntax:

url_title($str, $separator = '-', $lowercase = FALSE);

Parameter Description:

  • $str (string) – Input string
  • $separator (string) – Word separator
  • $lowercase (bool) – Whether to transform the output string to lower-case

The return type of this function is string and it returns a URL-formatted string.

Example:

Step 1 Open the application/controllers directory and create a file Urltitle_controller.php.

<?php
class Urltitle_controller extends CI_Controller {

public function index() {
$this->load->helper('url'); //load URL helper
$title="Welcome to CodingTag";
$url_title = url_title($title);
echo "Original string is: <strong>$title</strong>";
echo "<br>";
echo "Converted string is: <strong>$url_title</strong>";
}
}
?>

Step 2 Enter the given URL into the browser to see the result.

http://localhost/ci/index.php/Urltitle_controller


12 prep_url(): This function is used to add the http:// in the event that a protocol prefix is missing from a URL.

Syntax:

prep_url($str = '');

Parameter Description:

  • $str (string) – Protocol-prefixed URL string

The return type of this function is string and it returns a URL-formatted string.

Example:

Step 1 Open the application/controllers directory and create a file Prepurl_controller.php.

<?php
class Prepurl_controller extends CI_Controller {

public function index() {
$this->load->helper('url'); //load URL helper
$url='codingtag.com';
$n_url = prep_url($url);
echo "Original url is: <strong>$url</strong>";
echo "<br>";
echo "Protocol-prefixed URL is: <strong>$n_url</strong>";
}
}
?>

Step 2 Enter the given URL into the browser to see the result.

http://localhost/ci/index.php/Prepurl_controller



Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments