passing values to controllers in codeigniter

If you are thinking of passing some values to the controllers in codeigniter then you have come to the right place. This post will help you accomplish the task. Its not a magical anyway. A simply function remap() does all the trick. Lets create a simple controller which can takes some parameter, i.e. where we can pass values.



<?php
class Page extends CI_Controller {

public function _index($param)
{
                 if($index=='index'){
                           //if nothing is passed as parameter in $param this section will be executed
                  }else{
                            //if some parameter is passes in $param then this section will be executed
                 }
}
  
        function _remap($param){
              $this->_index($param);
        }

}
?>

Using this simple trick, now, you can pass parameter to the controller e.g. http://domain.com/page/first-page or http://domain.com/page/1 or whatever you have in your mind that you want to pass in the parameter field.

The reason behind all this is: If a controller contains a function named _remap(), it will always get called first regardless of what the URI contains. Also it overrides the normal behavior in which the URI determines which function is called, allowing us to define our own function routing rules.

Comments

Popular posts from this blog

Automate file upload in Selenium IDE

How To Install and Configure Nextcloud