0

我正在开发一个内置于 codeigniter 的项目,该项目大量使用路由和 remap 函数来重写 url。当前的实现令人困惑和混乱。

本质上,这就是设计师试图完成的事情:

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

任何人都可以提出一个干净的方法来完成这个吗?

这实际上只需要发生在一个特定的控制器上。如果所有其他控制器只需要遵循正常的 /controller/model/arg1... 模式就可以了

只是为了让您了解当前代码在此处的外观是“路由”文件:(并没有真正研究对此代码的任何见解,只是想让您了解我正在处理的当前设置有多混乱和。我想把它扔掉,用更好的东西代替它)

// 我们需要指定管理控制器和函数,这样它们就不会被视为竞赛

$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
}
4

1 回答 1

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";
于 2011-08-25T19:53:12.187 回答