Codeigniter Controller Function And Parameters


Codeigniter controller function and parameters : The codeigniter controller contains the methods inside it. The parameters passed in url along with function name are directly received inside the methods.


Codeigniter controller function And parameters Example

Lets Understand with the following example and demo :

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

class Home extends CI_Controller {
function index(){

echo "Welcome to index function."; 

}

function profile(){
 echo "Welcome to profile function";
}

function comments($id=null,$name=null){
   echo "Id: ". $id."<br>";
   echo "Name: ". $name."<br>";
}
}   
?>                         

1. Call index() function :

To access index() function hit like this :

http://localhost/Home/index

Where index calls index() function.

Demo

Try it »

1. Call profile() function :

To access profile() function hit like this :

http://localhost/Home/profile

Where profile calls function() function.

Demo

Try it »

1. Call comments() function with parameters :

You can define default parameter in function as :

commnts($id = null, $name = null)

Access the comments function with parameter :

http://localhost/Home/comments/9/john

Where first parameter is id and second is name as defined in parameter which will be received in parameters $id, $name .

Demo

Try it »


User Ans/Comments

📖 Read More


Advertisements