Vous êtes sur la page 1sur 1

www.InnovativePhp.

com
Parameter Passing Techniques in CodeIgniter
2010-10-10 03:10:08 nimeshrmr

I have been using code Igniter framework in my recent projects. Recently one of my friends ran into a
problem when passing optional parameters to controller function. So i decided to write this article to share
the knowledge i gained by solving that problem.

Lets start by using a simple example.

1. Accessing paramets as pre defined varibles in controller function

Ex : http://www.yoursite.com/index.php/Student/Create/Mark/22

class Student extends Controller{


public function __construct() { parent::Controller();
} public function create($name,$age){
$studentName = $name; $studentAge = $age;
}
}

When you call a controller function in code igniter the first two parameters after index.php is Controller
Class name and Controller class function respectively. In this method there are two other parameters
separated by ‘/’ . These two components are the two get parameters passes to the functiion. We can
access these get parameters by using two variables as shown in the example.

Name in the create function would be the first parameter in the url after create. So value of $name will be
Mark.
Age in the create function would be the second parameter in the url after create . So value of $age will be
22.

2. Accessing parameters without defining variables in controller function

Ex : http://www.yoursite.com/index.php/Student/Create/Mark/22
class Student extends Controller{
public function __construct() {
parent::Controller();
}
public function create(){
$studentName = $this->uri->segment(3);
$studentAge = $this->uri->segment(4);
}
}

This method can be used to get parameters passed to a controller function without defining variables
statically in the function declaration. In code igniter url parameters can be accessed by using the uri-
>segment function.
$this->uri->segment(param no); // You can use $this when using inside the controller function. Parameter number is required.

The first two segments of the url will always be Controller Class and Controller Function in
CodeIgniter. So if you want to access parameters , you have to start from index 3. In the above example
we can access the two parameters using index 3 and 4. This method is useful when the number of
parameters are dynamic.

3. Accessing Parameters Using Key Value Pairs

Ex : http://www.yoursite.com/index.php/Student/Create/Name/Mark/Age/22

<?php
class Student extends Controller{
public function __construct() {
parent::Controller();
}
public function create(){
$parameters = $this->uri->uri_to_assoc();
$studentName = $parameters['Name'];
$studentAge = $parameters['Age'];
}
}
?>

Consider a situation where the number of parameters passed to a function are dynamic and some of the
parameters are optional. Example is given below.

http://www.yoursite.com/index.php/Student/Create/Mark

http://www.yoursite.com/index.php/Student/Create/Mark/22

In previos two urls the age is optionnal. so we dont pass it every time we use the function . Hence it is
difficult to access the optional parameter through predefined varibales or url indexes.

In these type of situations you cannot use the first method and also the second method is very hard to
use. We can access parameters with key-value pairs to solve this problem. CodeIgniter provides a
method called uri->uri_to_assoc();

So in the url provided in the previos example (Name/Mark/Age/22) will be broken into 2keys and 2 values.
Name and Age will be the two keys and Mark and 22 will be the values for those keys respectiively. I prefer
this methos since ii know exactly what are the parameters i am accessing.

Hope you enjoyed the post on Parameter Passing Techniques in CodeIgniter. Please feel free to
comment with your sugesstions and content of this post.