Question

I'm working on a project built in codeigniter that makes heavy use of routes and the remap function to rewrite urls. The current implementation is confusing and messy.

Essentially this is what the designer was trying to accomplish:

www.example.com/controller/method/arg1/ TO www.example.com/arg1/controller/method/

Can anyone suggest a clean way of accomplishing this?

This actually only needs to happen for one specific controller. It's fine if all other controllers need to simply follow the normal /controller/model/arg1... pattern

Just to give you an idea of how the current code looks here is the 'routes' file: (not really looking into any insight into this code, just want to give you an idea of how cluttered this current setup is that I'm dealing with. I want to just throw this away and replace it with something better)

// we need to specify admin controller and functions so they are not treated as a contest

$route['admin/users'] = 'admin/users';
$route['admin/users/(:any)'] = 'admin/users/$1';
$route['admin'] = 'admin/index/';
$route['admin/(:any)'] = 'admin/$1';
// same goes for sessions and any other controllers
$route['session'] = 'session/index/';
$route['session/(:any)'] = 'session/$1';

// forward http://localhost/ball/contests to controller contests method index
$route['(:any)/contests'] = 'contests/index/$1';
// forward http://localhost/ball/contests/vote (example) to controller contests method $2 (variable)
$route['(:any)/contests/(:any)'] = 'contests/index/$1/$2';
// forward http://localhost/ball/contests/users/login (example) to controller users method $2 (variable)
$route['(:any)/users/(:any)'] = 'users/index/$1/$2';

// if in doubt forward to contests to see if its a contest
// this controller will 404 any invalid requests
$route['(:any)'] = 'contests/index/$1';


$route['testing/'] = 'testing/';

And the remap function that goes with it:

public function _remap($method, $params = array()){

    // example $params = array('ball', 'vote')
    // params[0] = 'ball', params[1] = 'vote'

    /*
     * Write a detailed explanation for why this method is used and that it's attempting to accomplish.
     * Currently there is no documentation detailing what you're trying to accomplish with the url here.
     * Explain how this moves the contest name url segment infront of the controller url segment. Also
     * explain how this works with the routing class.
     * */
    $count = count($params);
    if($count == 0){ // no contest specified
        redirect('http://messageamp.com');
        return;
    }

    $contest_name = $params[0];
    unset($params[0]);  //remove the contest name from params array because we are feeding this to codeigniter

    if($count < 2) // no method specified
        $method = 'index';
    else{
        $method = $params[1];
        unset($params[1]);
    }

    //We need to scrap this, lazy-loading is a best-practice we should be following
    $this->init(); //load models 

    //make sure contest is valid or 404 it
    if(!$this->central->_check_contest($contest_name)){
        show_404();
        return;
    }

    $this->data['controller'] = 'contests';
    $this->data['method'] = $method;
    $this->data['params'] = $params;
    // call the function if exists
    if(method_exists($this, $method)){
        return call_user_func_array(array($this, $method), $params);
    }
    show_404();  // this will only be reached if method doesn't exist
}
Was it helpful?

Solution

To get something like this:

www.example.com/controller/method/arg1/ TO www.example.com/arg1/controller/method/

You could do this in your routes.php config:

$route['(:any)/(:any)/(:any)'] = "$2/$3/$1";

However, if you want to have all of your other classes stick to the default routing, you would need to create routes for each of them to overwrite this default route:

$route['controller_name/(:any)'] = "controller_name/$1";
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top