Home > CodeIgniter, PHP > CodeIgniter _remap method – Power and Benefits

CodeIgniter _remap method – Power and Benefits

March 9th, 2009

If you are using Codeigniter (CI) then you will happy to know that, CI has a built in method named _remap which allows you to overwrite the behavior of calling your controller methods over URI. Usually the second segment of the URI determines which function in the controller gets called.

Now think about the following factors:

  • You want to hide your original method name from the URI
  • You want to named second segment of your URI as you like
  • You want to make a control over your controller methods so that you can decide which method should be called using URI

Now it’s time to talk in details.

First from the above factors is hiding original method name from URI. Yes, you can do it easily by using the _remap method. Let one of your controller methods is display_successful_message which you should typically browse as:
But you want to design your URI as follows
So that it calls the method display_successful_message

Now the second factor is naming second segment of URI as you like. You know that identifiers (name of variable, function, method, constant etc.) does not allow using any character except the following rule:
So you could not use any other character rather than alpha, numeric or underscore. But you like to use the dash character ‘-’ in URI as a replacement of the underscore character ‘_’.
More practically let one of your controller methods is about_me which you should typically browse as:
But you want to design your URI as follows
So that about_me method is get called. Note that you can also do it by CI’s URI routing feature. But it’s really paining and using _remap it’s become easier and organized.

Finally the third factor. You are going to make a control over your controller methods. You like to create a method’s white list which will be allowed to call using URI. Just create the list under the _reamp method. Rest of the task will do by the _remap. As for example you have a method named secure_method and you don’t like to call the method using URI. To do so don’t include the method in your white list. You can also do it another way. Just place an underscore character as a prefix of your method name (_secure_method) so that controller ignores calling the method using URI.

Before using the _remap method you should know that _remap will always get called regardless of what your URI contains. It overrides the normal behavior in which the URI determines which function is called, allowing you to define your own function routing rules.

Many lectures have delivered already. Now it’s time to show the practical.
Look at the following script:

        class Blog extends Controller
            function _remap( $method )
                // $method contains the second segment of your URI
                switch( $method )
                    case 'about-me':

                    case 'successful':


            function index()
                // ---

            function about_me()
                // ---

            function display_successful_message()
                // ---

            function page_not_found ()
                // ---

            function secure_method()
                // ---

            function Blog()

Now review the previous factors. The examples I discussed about each factor are applied on the above script. I think now it is easier to understand how to use _remap and what’s it benefits and power.

ANM_CI is a PHP class which is developed to use as a CI’s user library. In the class there is a method named remap which will make your life easier to use the CI’s _remap method.

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • BlogMemes
  • email
  • LinkedIn
  • Live
  • Print
  • Taggly
  • TwitThis
  • Yahoo! Buzz

$@!ful CodeIgniter, PHP , , , , , , , , , , , ,

  1. tom3cang
    December 23rd, 2010 at 11:23 | #1

    Oh, I’m so sorry for so long replying your question. I think _remap() will catch the second uri segment. Is that right? I found it here: http://codeigniter.com/user_guide/general/controllers.html. I find no result about _remap() in CI framework.
    Thank you very much! :D

  2. chris
    January 9th, 2012 at 17:22 | #2

    Thanks! That’s exactly what i need!

  3. January 23rd, 2012 at 04:36 | #3

    You also have the option to prepend a function’s name with an underscore. So the method titled _secure wouldn’t be accessible via site.com/index.php/blog/_secure

    See http://codeigniter.com/user_guide/general/controllers.html#private

    KNow it’s slightly different than the _remap idea, but in relation to the secure_method you used in the example I thought it was worth mentioning :)

  4. Alok
    May 21st, 2012 at 19:21 | #4

    I found It very usefull. Thnx For posting

Comment pages
  1. No trackbacks yet.