Special Offer - Get $90 Discount and Host Your Dream Web Site @ Dreamhost for 1 Year Click Here. Promo Code SLJ345
Back To Top
WordPress Web Application Development
Develop Powerfull Web Applications Using Cutting Edge WordPress Development Techniques

Create Access Controller using Codeigniter remap Function

on 2011/06/5 4:49 AM stored in: Uncategorized and tagged:

Introduction to Codeigniter remap Function

Codeigniter provides a function called _remap which can be used inside controllers. Normally first parameter in the url defines the name of the controller and second parameter defines the function to be called. But if you use the _remap function , it will be called regardless of the value of the second parameter in the url. So if you have a _remap function inside a controller, every function call will go through it and you have to define your own logic to call the other functions.

 

This function can be used to change the default method calling behavior and define your own routing rules. Function name is passed to this function by default and you can also pass the other parameters in the url as optional parameters. Lets look at the following example to get an idea about changing routes using remap function.


public function _remap($method, $params = array())
{
    if ($method == 'commoon_method'){
        $this->common_function();
    }
    else{
        $this->$method();
    }
}

As you can see in the above example we can route special functions to common function and call the default function on normal functions using the _remap function. You can define your own rules inside this method. Even though routing is the default functionality of the remap function , we can use it effectively for other areas as well. In the next section I’ll explain how to use it in access controlling to your system functions.

Access Controlling through Codeigniter remap Function

There are 2 types of protections in accessing web applications called authentication and authorization. Authentication is the process of checking a user has permission to access the application. Normally this is achieved by using user credential s and login. Authorization is the process of checking a user has access to specific part of the system. We can control the access to the system based on the user roles. Lets see how to do this using the _remap function.


public function _remap($method, $params = array())
{
    $user_type = $_SESSION['user_type'];

    $access_control = $this->validate_access($user_type,$method);
        

    if ($access_control){
        $this->$method();
    }
    else{
        $this->show_message();
    }
}


Consider a situation where there are 3 types of users levels in your system as admin,users and guests. You can define the functions accessible for each type of user in the database. Since all the requests goes through _remap function you can get the type of the user using the session variables. Then you can use a method like validate_access as shown above to pass the user type and function and validate against database to check whether user has permission to access it. If not you can redirect the user to a error page with message saying unauthorized access.

This is a basic example on how to do access control inside _remap function. you can modify this function and add complex rules according to your application requirements.

Practical usage of Codeigniter _remap Function

  • Change the default routing behavior.
  • Control access to specific users and functions
  • Save the statistics data for your page requests.
  • Common validations for all requests.

Leave a Reply

Follow Us On Facebook