codeigniter url helper 1

Here we discuss about the codeigniter URL structur. Codeigniter mainly preferes search engine freindly. that is segmented method. by default it won’t support get method.you can see my previous post for enabling get method in Codeigniter. It comes with URL helper that has many built in functions helps.

To run below examples,we use default welcome controller and welcome_message view. You can see below image where it located.when you run it executes welcome.php controller and loads view welcome_message.php

Codeigniter Download example source code

To run below examples,we have to load the URL helper by adding below line to controller constructor.

1
<?php $this->load->helper('url');?>

base_url() returns web site url.

1
2
3
4
<?php 
//output is http://example.com/
echo base_url();
?>

index_page() returns the index_page in config variable.by default it returns index.php. when you enable mod_write for Codeigniter index_page empty. Then base_url and site_url are equal.

1
2
3
4
<?php 
// out put could be index.php
echo index_page()
 ?>

site_url() return base_url with index_page in config variable. That is index.php by default.

1
2
3
4
  <?php 
//output could be http://example.com/index.php
echo site_url();
?>

site_url() takes two kinds of parameters either a string of segmets or array of segments.

Below example takes string of segments as a parameter.

1
2
3
4
5
<?php 
//output could be http://example.com/index.php/desired_controller/controller_method
echo site_url('/desired_controller/controller_method');
//out put could be http://example.com/index.php/desired_controller/controller_method/parameter1/parameter2 
echo site_url('/desired_controller/controller_method/parameter1/parameter2');?>

Below example takes array of segments as a parameter. be aware that precedence importence.

1
2
3
4
  <?php 
$segments=array('desired_controller','controller_method','parameter1','parameter2');
echo site_url($segments);
//out put could be http://example.com/index.php/desired_controller/controller_method/parameter1/parameter2 ?>

Codeigniter site folder structure

codeigniter_folder_structure

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">