CodeIgniter:如何获取Controller,Action,URL信息

non*_*.cs 120 php codeigniter codeigniter-2

我有这些网址:

如何从这些URL获取控制器名称,操作名称.我是CodeIgniter的新手.是否有任何帮助函数来获取此信息

例如:

$params = helper_function( current_url() )
Run Code Online (Sandbox Code Playgroud)

哪里$params变得像

array (
  'controller' => 'system/settings', 
  'action' => 'edit', 
  '...'=>'...'
)
Run Code Online (Sandbox Code Playgroud)

Sam*_*son 210

您可以使用URI类:

$this->uri->segment(n); // n=1 for controller, n=2 for method, etc
Run Code Online (Sandbox Code Playgroud)

我也被告知以下工作,但我目前无法测试:

$this->router->fetch_class();
$this->router->fetch_method();
Run Code Online (Sandbox Code Playgroud)

  • 嘿,如果你使用Codeigniter 3,那么对于Controller使用:$ this-> router-> class; for Method:$ this-> router-> method; 目录:$ this-> router-> directory; 文档:http://www.codeigniter.com/user_guide/installation/upgrade_300.html#uri-routing-methods-fetch-directory-fetch-class-fetch-method (5认同)
  • 要正确获取目录,您还可以使用:$ this-> router-> fetch_directory(); (2认同)

Phi*_*eon 132

您应该这样做,而不是使用URI段:

$this->router->fetch_class(); // class = controller
$this->router->fetch_method();
Run Code Online (Sandbox Code Playgroud)

这样,即使您位于路由URL,子域等后面,您也知道始终使用正确的值.

  • 是.这是最好的解决方案. (3认同)

小智 28

这些方法已弃用.

$this->router->fetch_class();
$this->router->fetch_method();
Run Code Online (Sandbox Code Playgroud)

您可以改为访问属性.

$this->router->class;
$this->router->method;
Run Code Online (Sandbox Code Playgroud)

请参阅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;
Run Code Online (Sandbox Code Playgroud)


小智 12

其他方式

$this->router->class
Run Code Online (Sandbox Code Playgroud)


Sta*_*arx 10

作为补充

$this -> router -> fetch_module(); //Module Name if you are using HMVC Component
Run Code Online (Sandbox Code Playgroud)


Muh*_*lam 6

更新资料

答案是在2015年添加的,现在不推荐使用以下方法

$this->router->fetch_class();  in favour of  $this->router->class; 
$this->router->fetch_method(); in favour of  $this->router->method;
Run Code Online (Sandbox Code Playgroud)

嗨,您应该使用以下方法

$this->router->fetch_class(); // class = controller
$this->router->fetch_method(); // action
Run Code Online (Sandbox Code Playgroud)

为此,但要使用此功能,您需要从扩展钩子,CI_Controller并且它像一个吊饰一样工作,您不应该使用uri段