Blog Details

Controllers



                                

* What is a Controller?

 
  • A Controller is simply a class file that is named in a way that can be associated with a URI.
 

Consider this URI:

 
  • localhost/codeigniter/welcome/index.php/blog/
  • In the above example, CodeIgniter would attempt to find a controller named blog.php and load it.
  • When a controller's name matches the first segment of a URI, it will be loaded.
 

Example:

 
Step 1 :
 
  • Let's create a simple controller so you can see it in action. Using your text editor, create a file called blog.php, and put the following code in it:
  <?php class Blog extends CI_Controller { public function index() { echo 'Hello World!'; } } ?>  
  • Then save the file blog.php to your application/controllers/ folder.
  • Now visit the your site using a URL similar to this:
  • localhost/codeigniter/welcome/index.php/blog/
 

Output:

  Hello World!.Note:
  • Class names must start with an uppercase letter. In other words, this is valid:
 

* How to Create Functions In CodeIgniter :-

 

Consider this URI:

 
  • localhost/codeigniter/welcome/index.php/blog/ index.php/comments/
  • In the below example the function name is  comments().
  <?php class Blog extends CI_Controller { public function index() { echo 'Hello World!'; } public function comments() { echo 'Look at this!'; } } ?>  
  • Then save the file blog.php to your application/controllers/ folder.
  • Now visit the your site using a URL similar to this:
  • localhost/codeigniter/welcome/index.php/blog/ index.php/comments
 

Output:

  Look at this!


Categories

CodeIgnitor

Latest posts

13.05.14
Implementing CRUD Operations ¶

<ul> <li>Now comes the fun part. We would like t

13.05.14
View

<ol> <li><a href="http://www.yiiframework.com/do

13.05.14
Creating Model

<ol> <li><a href="http://www.yiiframework.com/do

Get Free Quote