December 7, 2011 CodeIgniter Image

Remapping Function calls in Codeigniter

Remapping Function calls in Codeigniter

Remember: To make it look nice without the ‘index.php’ you should take a look at Remove index.php

Everyone that has used codeigniter for their projects, know that to be able to execute a certain function in a class from the browser, would most likely create a link with the class name, and the name of the function. You can also send in parameters through this same URL, giving the function more specific tasks. I had a situation once, where I wanted to create a controller called profile. The sole purpose of this controller was to display a specific users profile webpage. Before learning about this codeigniter _remap() function, I went along and tried to do this with the class construct. All seemed to go well until I had finally tested the logic. Guess what happened…? Everything went wrong. The idea was fabulous, since the class has to go into the construct everytime this class is called, but the problem was, for example, I wanted the url to be user friendly, and let’say for instance, my profile page address would be, Since codeigniter default structure is to, grab the class, and go to the function, when the logic sees that there is no function named ‘jotorres’, then it pops a nice error. I spent a few hours looking into this, seeing if there was another way around this, and guess what I found, yes, the _remap() function. When codeigniter encounters a _remap() function within your class, this will override the codeigniter default, and let you do as you wish with your controller.

Enough with the talking, let’s get on with the example.

To accomplish this ‘stylish’ and user friendly URL, let’s first make this ‘profile’ controller. This is created exactly the same way you would create a normal controller(class) in codeigniter, with contructor and all.

Filename: profile.php

Now, let’s add the remap function. Remember, once this remap function is coded inside your class, you are free to re-route as you wish.

Notice that the function remap takes in a parameter. This parameter in this case, will be the username that we will use to search the database. Let’s see how we would look for this:

Notice: I have made the logic within the controller file, for the sake of this post, and also, did not do any filtering, nor did any security checks for the input. Whenever you create an application that handles user input, you need to always be careful and make sure you take the proper steps in securing your logic. In a normal basis, you would create your logic within a model file. The whole reason you are using codeigniter is to seperate logic from user interface.

After retrieving user information, and making sure it exists, then we can send the user information to the browser and display it in our view. I will not create the view, as that task should be up to you, on how you would like to display your information to the users.

Inside the _remap() function, we can add the following:

This is basically it when it comes to creating a nice user friendly URL. Another thing you can add is addition parameters. These parameters should be an array. With these parameters, you can give it more functionality. Let’s say for example, you want to view the users pictures but within this same controller, how would it be done? Easy:

Remember to add the functions of view_pictures and view_videos. I think this is a very nice way of handling URL’s and making them look much more professional. I would like to know how would you use this _remap function.

10 thoughts on “Remapping Function calls in Codeigniter

  1. Hi dejitaru,

    You don’t have to do it my way if you’d rather change the config file, but that’s the thing, changing the config file might cause your process to be a bit slower, as your config file is called every time, and the controller is only accessed when its called.

    On my opinion, it’s cleaner this way. If by any chance, you have to make changes, it’s better off to make them in the controller rather than in the config file.

    1. Above, you say using _remap is quicker and more efficient than using routes. If anything, in this example, you’ve created more code in the _remap method than than a few array variables in the routes file would. Also, adding in a couple of variations of the said route would take a few minutes at most. You also make a point about loading another config file – come on, this is codeigniter. Loading a small config file, which is essentially a tiny array, isn;t going to be noticable at all.

      I’m also not a huge fan of this approach (which does have it’s uses) as it adds far too much logic in the controller, that belongs in the model (the whole fat model, skinny controller …).

    1. Hi Juan,
      Thanks for the reply. There is actually a way to achieve what you are trying to do. You need to place a logic within your routes.php file, and configure the routes. So for example, you would need to do something like this:

      This would need to be done within a foreach for all the users inside the database. The only downside to that is, that every time the page loads, it will query the database. Imagine if you have 100k users, then your site will run pretty slow.

      Another workaround would be something almost the same:

      The last example I honestly do not recommend it, since it will just probably mess up your whole site when trying to access another class and method.

      1. I’ve used the 404 catch override and a _remap which works quite well. I also have a templated 404 page which is pleasent.

        Message: call_user_func_array() expects parameter 1 to be a valid callback, class ‘URI_verify’ does not have a method ‘index’

        My email verification code on uri 3 seems to trip over it. It says I have no index() on my URI_verify controller.

        function _remap($method) {
        if($this->uri->total_segments() == 1) {
        if ($this->(redacted)->get_member_details($this->uri->segment(1))) {
        $data = array();
        $data = $this->(redacted)->get_member_details($this->uri->segment(1));
        $data[‘body’] = ‘member_profile’;
        } else {
        $data[‘body’] = ‘body_404’;
        $data[‘error’] = ‘‘.$this->uri->uri_string().’ This is not a valid username or page.’;
        } else {
        $data[‘body’] = ‘body_404’;
        echo Modules::run(‘template/skin’, $data);

  2. This is nice. Thanks for sharing with this tutorial.

    I ran into a similar issue where I needed to control the CI default when integrating wordpress and CI (don’t ask why or how, it was more or less just seeing if it can be done). But anyway, I needed to be able to use the second parameter to pass the permalink to codeigniter which of course threw errors because there was no function named “whatever permalink” .

    I had to make the url longer to get it to work. I’m going to go revisit that little project now and see if _remap() is what I need. It looks like it is.

    Thanks again

Leave a Reply

Your email address will not be published. Required fields are marked *

The qTranslate Editor has disabled itself because it hasn't been tested with your Wordpress version yet. This is done to prevent Wordpress from malfunctioning. You can reenable it by clicking here (may cause data loss! Use at own risk!). To remove this message permanently, please update qTranslate to the corresponding version.