Codeigniter ルート正規表現 - コントローラ/メソッド名にダッシュを使用する 質問する

Codeigniter ルート正規表現 - コントローラ/メソッド名にダッシュを使用する 質問する

破線のコントローラー名とメソッド名を実際の下線付きのコントローラー名とメソッド名にルーティングする 1 行のルートを探しています。

たとえばURL

/controller-name/method-name-which-is-long/

ルーティングする

/controller_name/method_name_which_is_long/

見る:http://codeigniter.com/forums/viewreply/696690/それが私に質問するアイデアを与えました:)

ベストアンサー1

それはまさに私の要件でもあり、私は次のようなルートを使用していました

$route['logued/presse-access'] = "logued/presse_access";

以前のプロジェクトでは、300~400 個のルーティング ルールを作成する必要がありましたが、そのほとんどはダッシュをアンダースコアに変換するものでした。

次のプロジェクトでは、これを避けたいと思っています。簡単なハックを行ってテストしましたが、ライブ サーバーで使用したことはありませんが、私の環境ではうまく機能しています。次の操作を実行してください。

system/application/config/config.php の subclass_prefix が次のようになっていることを確認してください。

$config['subclass_prefix'] = 'MY_';

次に、system/application/libraries ディレクトリに MY_Router.php という名前のファイルをアップロードします。

<?php

class MY_Router extends CI_Router { 
    function set_class($class) 
    {
        //$this->class = $class;
        $this->class = str_replace('-', '_', $class);
        //echo 'class:'.$this->class;
    }

    function set_method($method) 
    {
//      $this->method = $method;
        $this->method = str_replace('-', '_', $method);
    }

    function _validate_request($segments)
    {
        // Does the requested controller exist in the root folder?
        if (file_exists(APPPATH.'controllers/'.str_replace('-', '_', $segments[0]).EXT))
        {
            return $segments;
        }
        // Is the controller in a sub-folder?
        if (is_dir(APPPATH.'controllers/'.$segments[0]))
        {       
            // Set the directory and remove it from the segment array
            $this->set_directory($segments[0]);
            $segments = array_slice($segments, 1);

            if (count($segments) > 0)
            {
                // Does the requested controller exist in the sub-folder?
                if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().str_replace('-', '_', $segments[0]).EXT))
                {
                    show_404($this->fetch_directory().$segments[0]);
                }
            }
            else
            {
                $this->set_class($this->default_controller);
                $this->set_method('index');

                // Does the default controller exist in the sub-folder?
                if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.EXT))
                {
                    $this->directory = '';
                    return array();
                }

            }

            return $segments;
        }

        // Can't find the requested controller...
        show_404($segments[0]);
    }
}

次のようなURLを自由に使用できるようになりましたhttp://example.com/logued/presse-accessダッシュをアンダースコアに自動的に変換して、適切なコントローラーと関数を呼び出します。

編集:以下は、新しい CI_Router 関数をオーバーライドする Codeigniter 2 ソリューションです。

<?php

class MY_Router extends CI_Router { 
    function set_class($class) 
    {
        $this->class = str_replace('-', '_', $class);
    }

    function set_method($method) 
    {
        $this->method = str_replace('-', '_', $method);
    }

    function set_directory($dir) {
        $this->directory = $dir.'/';
    }

    function _validate_request($segments)
    {
        if (count($segments) == 0)
        {
            return $segments;
        }

        // Does the requested controller exist in the root folder?
        if (file_exists(APPPATH.'controllers/'.str_replace('-', '_', $segments[0]).'.php'))
        {
            return $segments;
        }

        // Is the controller in a sub-folder?
        if (is_dir(APPPATH.'controllers/'.$segments[0]))
        {
            // Set the directory and remove it from the segment array
            $this->set_directory($segments[0]);
            $segments = array_slice($segments, 1);


            while(count($segments) > 0 && is_dir(APPPATH.'controllers/'.$this->directory.$segments[0]))
            {
                // Set the directory and remove it from the segment array
                $this->set_directory($this->directory . $segments[0]);
                $segments = array_slice($segments, 1);
            }

            if (count($segments) > 0)
            {
                // Does the requested controller exist in the sub-folder?
                if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().str_replace('-', '_', $segments[0]).'.php'))
                {
                    if ( ! empty($this->routes['404_override']))
                    {
                        $x = explode('/', $this->routes['404_override']);

                        $this->set_directory('');
                        $this->set_class($x[0]);
                        $this->set_method(isset($x[1]) ? $x[1] : 'index');

                        return $x;
                    }
                    else
                    {
                        show_404($this->fetch_directory().$segments[0]);
                    }
                }
            }
            else
            {
                // Is the method being specified in the route?
                if (strpos($this->default_controller, '/') !== FALSE)
                {
                    $x = explode('/', $this->default_controller);

                    $this->set_class($x[0]);
                    $this->set_method($x[1]);
                }
                else
                {
                    $this->set_class($this->default_controller);
                    $this->set_method('index');
                }

                // Does the default controller exist in the sub-folder?
                if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.'.php'))
                {
                    $this->directory = '';
                    return array();
                }

            }

            return $segments;
        }


        // If we've gotten this far it means that the URI does not correlate to a valid
        // controller class.  We will now see if there is an override
        if ( ! empty($this->routes['404_override']))
        {
            $x = explode('/', $this->routes['404_override']);

            $this->set_class($x[0]);
            $this->set_method(isset($x[1]) ? $x[1] : 'index');

            return $x;
        }


        // Nothing else to do at this point but show a 404
        show_404($segments[0]);
    }
}

$config['subclass_prefix'] = 'MY_';このファイルをapplication/core/MY_Router.phpのように配置して、subclass_prefixがapplication/config/config.phpのように定義されていることを確認する必要があります。

メソッドにいくつかのコード行が追加されました_validate_request():

while(count($segments) > 0 && is_dir(APPPATH.'controllers/'.$this->directory.$segments[0]))
{
    // Set the directory and remove it from the segment array
    $this->set_directory($this->directory . $segments[0]);
    $segments = array_slice($segments, 1);
}

これは、コントローラ ディレクトリ内の複数レベルのサブディレクトリを使用できるようにするために使用されます。通常、コントローラ フォルダー内の単一レベルのサブディレクトリを使用して、URL で呼び出すことができます。このコードは必要ない場合は削除できますが、通常のフローに害はありません。

おすすめ記事