Php CodeIgniter:如何获取控制器、操作、URL信息

Php CodeIgniter:如何获取控制器、操作、URL信息,php,codeigniter,codeigniter-2,Php,Codeigniter,Codeigniter 2,我有以下网址: 如何从这些URL获取控制器名称、操作名称。我是CodeIgniter新手。是否有任何帮助函数来获取此信息 例: $params=helper\u函数(当前url()) 其中,$params变成 数组( “控制器”=>“系统/设置”, “操作”=>“编辑”, '...'=>'...' ) 您可以使用: 我还被告知以下工作,但目前无法测试: $this->router->fetch_class(); $this->router->fetch_meth

我有以下网址:

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

例:

$params=helper\u函数(当前url())
其中,
$params
变成

数组(
“控制器”=>“系统/设置”,
“操作”=>“编辑”,
'...'=>'...'
)
您可以使用:

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

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

您应该执行以下操作,而不是使用URI段:

$this->router->fetch_class(); // class = controller
$this->router->fetch_method();
这样,即使您位于路由URL后面、子域中等,您也知道您始终在使用正确的值。

作为补充

$this -> router -> fetch_module(); //Module Name if you are using HMVC Component
另一种方式

$this->router->class

如果您使用$this->uri->segment,如果URL重写规则发生更改,则段名称匹配将丢失。

更新

答案是在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\u控制器
扩展您的钩子,并且它像一个符咒一样工作,您不应该使用uri段

这些方法已被弃用

$this->router->fetch_class();
$this->router->fetch_method();
您可以改为访问属性

$this->router->class;
$this->router->method;

URI路由方法fetch_目录(),fetch_类(),fetch_方法() 使用属性
CI_路由器::$directory
CI_路由器::$class
CI\u路由器::$method
是公共的,它们各自的
fetch\u*()
no 不再做任何其他事情只是返回属性-它不会 保留它们是有意义的

这些都是内部的、未记录的方法,但我们选择了 为了保持向后兼容性,暂时不要使用它们 以防万一。如果你们中的一些人已经利用了它们,那么你们现在就可以 改为访问属性:

$this->router->directory;
$this->router->class;
$this->router->method;

在类或库中的任何位置使用此代码

    $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

控制器类不使用任何函数

因此,我建议您使用以下脚本

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];
}
//fecth类控制器中的类 $this->router->fetch_method()


//方法

URL的最后一段将始终是操作。请像这样:

$this->uri->segment('last_segment');

因为我的控制器有子文件夹,所以$this->uri->segment(1)返回的控制器名称可能不正确。感谢您的帮助,我使用$this->router获取信息。要正确获取目录,您还可以使用:$this->router->fetch_Directory();嘿,如果您使用的是CodeIgniter3,那么控制器使用:$this->router->class;对于方法:$this->router->Method;对于目录:$this->router->Directory;文档:$this->router->fetch_class()$此->路由器->获取_方法();我刚刚发现,使用这个方法,在调用像
modules::run('module/controller/action')
这样的视图时,无法获得正确的类方法名称。它只显示加载页面的路由器信息。有没有办法克服这个问题?太棒了!有关于这有多安全的消息吗?也就是说,这可以被欺骗吗?这应该是可接受的答案-或者使用快捷方式:$this->router->classNot如果在routes.php中更改路由,
$this->router
返回代码运行的类,但不返回被覆盖的实际路由器。这两个答案都是非常有用的,基于你想做什么。这些方法已经被去除,使用
$this->router->class
$this->router->method
代替这个
fetch_module()
调用在CI 2.1.2中对我不起作用。在/system/router.php文件中找不到它。@Timperson,它只在回答中提到的HMVC组件中可用。有预定义的函数可用,如$this->router->fetch_class()和fetch_method(),我们不需要手动执行。
$this->router->fetch_class(); 
$this->uri->segment('last_segment');