0

私は、ルートとremap関数を多用してURLを書き換えるcodeigniterに組み込まれたプロジェクトに取り組んでいます。現在の実装は混乱し、厄介です。

基本的に、これは設計者が達成しようとしていたことです。

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

誰かがこれを達成するためのクリーンな方法を提案できますか?

これは、実際には1つの特定のコントローラーに対してのみ発生する必要があります。他のすべてのコントローラーが通常の/controller/ model /arg1...パターンに従う必要がある場合は問題ありません

現在のコードがここでどのように見えるかを示すために、「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
}
4

1 に答える 1

1

このようなものを取得するには:

www.example.com/controller/method/arg1/ TO 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 に答える