Codeigniter URL重映射策略

时间:2011-08-25 19:16:41

标签: php codeigniter coding-style codeigniter-url code-structure

我正在开发一个使用codeigniter构建的项目,该项目大量使用路由和重映射功能来重写URL。目前的实施令人困惑和混乱。

基本上这就是设计师想要完成的事情:

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

有人能建议一种干净的方式来实现这个目标吗?

这实际上只需要为一个特定的控制器发生。如果所有其他控制器只需要遵循普通的/ controller / model / arg1 ... pattern

就没问题

只是为了让您了解当前代码在这里看起来是'routes'文件:(并没有真正深入了解这些代码,只是想让您了解这个当前设置是多么混乱我我正在处理。我想把它扔掉,用更好的东西替换它。

//我们需要指定管理员控制器和功能,以免将它们视为比赛

$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/';

与之相关的重映射功能:

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
}

1 个答案:

答案 0 :(得分:1)

得到这样的东西:

  

www.example.com/controller/method/arg1/访问www.example.com/arg1/controller/method /

您可以在 routes.php 配置中执行此操作:

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

但是,如果您希望所有其他类都坚持使用默认路由,则需要为每个类创建路由以覆盖此默认路由:

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