Home > Coding > Extending CodeIgniter – Remap Controller

Extending CodeIgniter – Remap Controller

September 2nd, 2011 Leave a comment Go to comments

Since we are about to retire one of our last DT Framework sites I thought I would try to share a few of the extensions we are using for CodeIgniter

This Controller extension solves the problem of not being able to call the index with parameters, it looks for a controller method with the same name as the parameter and if there isn’t one it just passes the parameters to index.

Lets assume we have a method named load.  And we call /controller/load – then the load method gets called, but if we go to /controller/1 – then index gets called with index(1).   – Only thing you have to worry about is namespace collisions but otherwise it can simplify your url structure.

class Remap_Controller extends CI_Controller {
 
    function _remap($method, $params) {
 
        // Remap all the functions but allow for params to index
        if (method_exists($this, $method)) {
            return call_user_func_array(array($this, $method), $params);
        } else {
            array_unshift($params, $method);
            return call_user_func_array(array($this, "index"), $params);
        }
    }
 
}

So just drop that in your applications/core directory and then when you define your controller

class Remap_Controller extends CI_Controller

And you have a fancy new controller, I am sure things could be done to improve this but it works for my purposes.

Share
Categories: Coding
  1. No comments yet.
  1. No trackbacks yet.