Codeigniter : passing parameters to controller index

if you want passing parameters to index page of a controller and you don’t know about it, you should read this article.

in general, URLs in codeigniter are something like : 

example.com/controller_name/method/parameter1/parameter2/..../parameterN

then if we have a controller as Product and some methods (functions) such as add_product, edit_product our class would be like :

class Product  extends CI_Controller
{
     function index(){}

     function add_product(){}

     function edit_product ($ID){}
}

and we can have 3 URLs as :

 example.com/product/
 example.com/product/add_product
 example.com/product/edit_product/product_id/color

the first URL use index() method and the second one use add_product() and the last one use edit_product() with the one parameter as product_id.

but what happens if we want to show a product in a URL as :

example.com/product/product_id/color

by default CodeIgniter consider product_id as a method and it will search for it and you don’t have such a method (function). then it will return 404 page.

Solution: Using Remapping Function

_remap() is a built-in function and is helpful for solving our problem. this method should be like :

function _remap($method,$args)
{

    if (method_exists($this, $method))
    {
	       $this->$method($args);
    }
    else
    {
        $this->index($method,$args);
    }

}

by using this function in our controller class, it checks the methods of the class, if there is such a method, it goes to that method, otherwise it will pass the parameters to the index method. 

then for following URL

example.com/product/product_id

we can use the following class :

class Product  st extends CI_Controller
{
    function _remap($method,$args)
    {

	       if (method_exists($this, $method))
	       {
	           $this->$method($args);
	       }
       	else
	       {
	           $this->index($method,$args);
 	      }
   }

   function index($ID)
   {
	 //     $ID is equal product_id in the URL
   }

}

if we want to pass more than one parameters index() method, for example, if we have color and size for our product and we want to pass them to index() method we can do it like :

example.com/product/product_id/color/size

class Product extends CI_Controller
{
    function _remap($method,$args)
    {

	      if (method_exists($this, $method))
	      {
	          $this->$method($args);
	      }
	      else
	      {
	    		    $this->index($method,$args);
	      }
    }

    function index($method, $args=array())
   {
	//  $method is the product_id in the url
        // $args is an array as
        //  array(‘value of color’ , ‘value of size’)
   }

   function add_product(){}

   function edit_product ($ID){}
}

Important Tip

when we don’t use _remap() function the parameters for method are like below :

example.com/product/edit_product/id/color/size

class Product  extends CI_Controller
{

    index(){}

   	edit_product ($ID,$Color,$Size)
    {
         //$ID, $Color, $Size are equal id, color, size in the URL respectively
         //we can return all the args by func_get_args() as an array.
    }
}

but when we use _remap() function as it shows above, the parameters work in different way:

class Product st extends CI_Controller
{
    function index($method, $args=array())
    {
        // $method is product_id in the url
        // $args is an array as
        //array(‘value of color’ , ‘value of size’)
    }

    edit_product($method, $args)
    {
        // $method is the first argument (in this case id) and
        // the rest of arguments return in an array as args.
        // $method = value of id
        // $args = array(‘value of color’, ‘value of size’)
    }
} 

then if we want to echo the results :

class Product extends CI_Controller
{

    function _remap($method, $args)
    {

	       if (method_exists($this, $method))
	       {
	           $this->$method($args);
 	       }
	       else
	       {
	            $this->index($method, $args);
	       }
    }

    function index($method, $args = array())
    {
        echo 'Product ID is '. $method.'<br/>';
        echo 'Color is '.$args[0].'<br/>';
        echo 'Size is '.$args[1].'<br/>';

    }

    function edit_product($args)
    {
        echo 'ID of product is'. $args[0].'<br/>';
        echo 'Color is '.$args[1].'<br/>';
        echo 'Size is '.$args[2].'<br/>';
    }
}

example.com/product/34/red/12

output :
ID of product is 34
Color is red
Size is 12

example.com/product/edit_product/34/red/12

output :
ID of product is 34
Color is red
Size is 12

2 thoughts on “Codeigniter : passing parameters to controller index

    1. saeed pourali Post author

      you don’t need 2 indexs.
      if you don’t use _remap() add a validation and use $id=” as argument for the case you don’t have any id

      public function index($id='') {
         if($id) { 
              //show one item
         } else {
              //show all the items
         }
      }
      

      if you use _remap() then

      
      public function index($method ='', $args = array()) {
         if($method ){ 
               //show one item
         } else{
               //show all the items
         }
      }
      
      Reply

Leave a Reply

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