私はこれらのURLを持っています:
これらのURLからコントローラー名、アクション名を取得する方法。私はCodeIgniterの初心者です。この情報を取得するためのヘルパー関数はありますか
元:
$params = helper_function( current_url() )
どこ$params
が
array (
'controller' => 'system/settings',
'action' => 'edit',
'...'=>'...'
)
私はこれらのURLを持っています:
これらのURLからコントローラー名、アクション名を取得する方法。私はCodeIgniterの初心者です。この情報を取得するためのヘルパー関数はありますか
元:
$params = helper_function( current_url() )
どこ$params
が
array (
'controller' => 'system/settings',
'action' => 'edit',
'...'=>'...'
)
URIクラスを使用できます:
$this->uri->segment(n); // n=1 for controller, n=2 for method, etc
また、次の作業についても言われましたが、現在テストできません。
$this->router->fetch_class();
$this->router->fetch_method();
URIセグメントを使用する代わりに、次のようにする必要があります。
$this->router->fetch_class(); // class = controller
$this->router->fetch_method();
そうすれば、ルーティングされたURLやサブドメインなどの背後にいる場合でも、常に正しい値を使用していることがわかります。
メソッドは非推奨になりました。
$this->router->fetch_class();
$this->router->fetch_method();
代わりに、プロパティにアクセスできます。
$this->router->class;
$this->router->method;
codeigniterユーザーガイドを参照してください
URIルーティングメソッドfetch_directory()、fetch_class()、fetch_method()
プロパティを使用し
CI_Router::$directory
、パブリックであり、それぞれがプロパティを返すために他に何もしなくなった場合、プロパティを保持することは意味がありません。CI_Router::$class
CI_Router::$method
fetch_*()
これらはすべて内部の文書化されていないメソッドですが、万が一の場合に備えて下位互換性を維持するために、現時点では非推奨にすることを選択しました。それらを利用したことがある場合は、代わりにプロパティにアクセスできます。
$this->router->directory; $this->router->class; $this->router->method;
別の方法
$this->router->class
追加として
$this -> router -> fetch_module(); //Module Name if you are using HMVC Component
アップデート
回答は2015年に追加され、次のメソッドは現在非推奨になっています
$this->router->fetch_class(); in favour of $this->router->class;
$this->router->fetch_method(); in favour of $this->router->method;
こんにちは、次のアプローチを使用する必要があります
$this->router->fetch_class(); // class = controller
$this->router->fetch_method(); // action
この目的のために、しかしこれを使用するためにあなたはからあなたのフックを伸ばす必要があります、CI_Controller
そしてそれは魅力のように働きます、あなたはuriセグメントを使うべきではありません
$ this-> uri-> segmentを使用している場合、URL書き換えルールが変更されると、セグメント名の一致が失われます。
クラスまたはライブラリのどこでもこのコードを使用する
$current_url =& get_instance(); // get a reference to CodeIgniter
$current_url->router->fetch_class(); // for Class name or controller
$current_url->router->fetch_method(); // for method name
URLの最後のセグメントは常にアクションになります。このようにしてください:
$this->uri->segment('last_segment');
$this->router->fetch_class();
//fecthクラスコントローラーのクラス$this->router-> fetch_method();
// 方法
コントローラクラスは機能していません。
したがって、次のスクリプトを使用することをお勧めします
global $argv;
if(is_array($argv)){
$action = $argv[1];
$method = $argv[2];
}else{
$request_uri = $_SERVER['REQUEST_URI'];
$pattern = "/.*?\/index\.php\/(.*?)\/(.*?)$/";
preg_match($pattern, $request_uri, $params);
$action = $params[1];
$method = $params[2];
}