简体   繁体   English

Codeigniter URL重新映射策略

[英]Codeigniter URL remapping strategy

I'm working on a project built in codeigniter that makes heavy use of routes and the remap function to rewrite urls. 我正在开发一个使用codeigniter构建的项目,该项目大量使用路由和remap函数来重写url。 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/ www.example.com/controller/method/arg1/至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 如果所有其他控制器仅需要遵循正常的/ controller / model / arg1 ...模式,那就很好

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) 只是为了让您了解当前代码的外观,这里是“ routes”文件:(不是真正地研究此代码,只是想让您了解我正在处理的当前设置有多混乱我想把它扔掉,换成更好的东西)

// 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: 以及随之而来的remap函数:

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
}

To get something like this: 要获得这样的东西:

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

You could do this in your routes.php config: 您可以在routes.php配置中执行此操作:

$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";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM