Codeigniter

_remap() function in codeigniter

In this tutorial I will explain in detail about _remap() function in codeIgniter. As we already know that the second segment of the codeigniter URI typically determines which method in the controller gets called. CodeIgniter allows you to override this through the use of the _remap() method.

public function _remap()
{
   // Some code here...
}

Why _remap() ?

To hide the real method name in the URL you can use _remap() and you can define your own set of routing rules.

If your controller contains a method named _remap(), it will always get called regardless of what your URI contains.

Example:

If you URI is like as below:
http://www.example.com/blog/about/
Now if you want to change your method name only from the URI (without actual change in the controller method).

http://www.example.com/blog/about-us/

then you can use the _remap() in blog controller code to achieve this.

function _remap($method)
{
// $method contains the method name from URI, that is second URI segment.
switch($method)
{
case 'About-Us':
$this->about();
break;
default:
$this->Not_Found();
break;
}
}

Any extra segments after the method name are passed into _remap() as an optional second parameter. This array can be used in combination with PHP’s call_user_func_array() to reproduce the CodeIgniter’s default behavior.

Example:

public function _remap($method, $params = array())
{
$method = 'process_'.$method;
if (method_exists($this, $method))
{
return call_user_func_array(array($this, $method), $params);
}
show_404();
}

More about _remap() can be found in the codeigniter official website
https://www.codeigniter.com/userguide3/general/controllers.html?highlight=remap#remapping-method-calls 

I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face – we are here to solve your problems.