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:
http://anmsaiful.net/blog/display_successful_message
But you want to design your URI as follows
http://anmsaiful.net/blog/successful
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:
^[a-zA-Z_]+[a-zA-Z0-9_]?$
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:
http://anmsaiful.net/blog/about_me
But you want to design your URI as follows
http://anmsaiful.net/blog/about-me
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':
                        $this->about_me();
                        break;

                    case 'successful':
                        $this->display_successful_message();
                        break;

                    default:
                        $this->page_not_found();
                        break;
                }
            }

            function index()
            {
                // ---
            }

            function about_me()
            {
                // ---
            }

            function display_successful_message()
            {
                // ---
            }

            function page_not_found ()
            {
                // ---
            }

            function secure_method()
            {
                // ---
            }

            function Blog()
            {
                parent::Controller();
            }
        }
        

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. August 19th, 2009 at 22:30 | #1

    Thanks for writing this. I found it very helpful in understanding the benefits of using _remap().

    Something I discovered when I used _remap() is since it intercepts all of the normal routing that takes place to direct the request to the method defined in URI segment 2, if you don’t have a particular method defined in the switch statement inside of the _remap() function, it will never get called.

    To use the code above as an example, secure_method() would never get called. I think this was your intention for this example, but if you wanted to use _remap() to rewrite only some of the function names in a controller, but pass through any others, you could specify $this->$method() as your default case. Doing this in your example would allow a call to secure_method() to essentially get passed through _remap() unaltered.

  2. September 11th, 2009 at 00:48 | #2

    Brilliant! Thanks for the very helpful method.

  3. November 24th, 2009 at 14:29 | #3

    Hi

    Thanks for this tutorial, well in order to rewrite url i think you can also do this via routing facility in codeigniter which is also built in feature of the framework.

    Is there any difference in _remap and routing?

  4. December 13th, 2009 at 11:50 | #4

    thx for the tutorial…
    i just curious, what is the difference between _remap & routing? what _remap can&can’t do and so the routing..
    thx..

  5. December 16th, 2009 at 00:54 | #5

    Hi Rashid

    Remap is only used to change methods name, it cannot allow you to change controller name, whereas by using routes you can change the controller name, function name and whole url as well.

    Hope it clears

    Eshban

  6. January 1st, 2010 at 17:13 | #6

    very helpful post….thank a lot bro…………

  7. Khalil
    September 1st, 2010 at 09:28 | #7

    Useful Tutorial, Thanks

  8. November 29th, 2010 at 11:25 | #8

    I have a issue about _remap() function.
    With above example, suppose that I add more one case like this:
    case ”: $this->do_something(); break;
    And after that, I want to access http://anmsaiful.net/blog. I thought _remap() function will catch the case I have already added, but it catches the default case.
    Please tell me why?
    Your posts is very useful ^^ Thanks a lot!

  9. December 7th, 2010 at 08:33 | #9

    @tom: you didn’t specify the uri segment in your case which is mandatory.

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

    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

  1. No trackbacks yet.