Code igniter parameter passing techniques


Published on

Code igniter parameter passing techniques

Published in: Technology, Design
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Code igniter parameter passing techniques

  1. 1. www.InnovativePhp.comParameter Passing Techniques in CodeIgniter2010-10-10 03:10:08 nimeshrmrI have been using code Igniter framework in my recent projects. Recently one of my friends ran into aproblem when passing optional parameters to controller function. So i decided to write this article to sharethe knowledge i gained by solving that problem.Lets start by using a simple example.1. Accessing paramets as pre defined varibles in controller functionEx : 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 ControllerClass name and Controller class function respectively. In this method there are two other parametersseparated by ‘/’ . These two components are the two get parameters passes to the functiion. We canaccess 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 beMark.Age in the create function would be the second parameter in the url after create . So value of $age will be22.2. Accessing parameters without defining variables in controller functionEx : 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 variablesstatically 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 inCodeIgniter. So if you want to access parameters , you have to start from index 3. In the above examplewe can access the two parameters using index 3 and 4. This method is useful when the number ofparameters are dynamic.3. Accessing Parameters Using Key Value PairsEx :<?phpclass 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 theparameters are optional. Example is given below. previos two urls the age is optionnal. so we dont pass it every time we use the function . Hence it isdifficult 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 touse. We can access parameters with key-value pairs to solve this problem. CodeIgniter provides amethod 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 preferthis 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 tocomment with your sugesstions and content of this post.