6

カスタムの post_controller フックを作成しています。ご存知のように、codeigniter uri 構造は次のようになります。

example.com/class/function/id/

と私のコード:

function hook_acl()
{
    global $RTR;
    global $CI;

    $controller = $RTR->class; // the class part in uri
    $method = $RTR->method; // the function part in uri
    $id = ? // how to parse this?

    // other codes omitted for brevity
}

私はコアの Router.php ファイルを閲覧しましたが、これにはかなり戸惑いました。

ありがとう。

4

2 に答える 2

8

CodeIgniter URI コア クラスの使用

通常、 CodeIgniterフック内では、メソッドに到達するために URI コア クラスをロード/インスタンス化する必要があります。

  • post_controller_constructor、、 ... フックの場合、CodeIgniter スーパー オブジェクトを取得して、次のクラスpost_controllerを使用できます。uri
# Get the CI instance
$CI =& get_instance();

# Get the third segment
$CI->uri->segment(3);
  • ただし、フックについては、CodeIgniterスーパー オブジェクトpre_controllerにアクセスできないため、次のように URI コア クラスを手動でロードする必要があります。
# Load the URI core class
$uri =& load_class('URI', 'core');

# Get the third segment
$id = $uri->segment(3); // returns the id

純粋な PHP の使用

このアプローチでは、$_SERVER配列を使用して URI セグメントを次のように取得できます。

$segments = explode('/', trim($_SERVER['REQUEST_URI'], '/'));

$controller = $segments[1];
$method     = $segments[2];
$id         = $segments[3];
于 2014-03-10T13:06:42.717 に答える
2

routerクラスを使用できます:

$this->router->fetch_class();
$this->router->fetch_method();

またはURIクラス:

$this->uri->segment(1); // the class
$this->uri->segment(2); // the function
$this->uri->segment(3); // the ID
于 2014-03-10T12:55:16.237 に答える