How to use remap method in CodeIgniter


In this tutorial I will explain how to use remap method in CodeIgniter with simple example. CodeIgniter permits you to override which method in the controller gets called through the use of the _remap() method and allow you to define your own method routing rules.

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

For Example:

If you URI is like as below:
http://www.domain.com/site/about_us/
http://www.domain.com/site/contact_us/

Here site is for controller name and about_us, contact_us are the methods in that controller. Now if you want to change your method name only from the URI (without actual change in the controller method), then you can use the _remap 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_me();
                break;
            case 'Contact-us':
                $this->contact_us();
                break;
            default:
                $this->Not_Found();
                break;
       
        }
 }

Now you can change your URL as below:
http://www.domain.com/site/About-Us // calls about_me(); using _remap method
http://www.domain.com/site/Contact-us // calls contact_us(); using _remap method

Hope this tutorial explained how to use _remap method in CodeIgniter.