如何检索codeigniter钩子中的第三个uri段 [英] How to retrieve the third uri segment in a codeigniter hook
本文介绍了如何检索codeigniter钩子中的第三个uri段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在写一个自定义的post_controller钩子。我们知道,codeigniter uri结构是这样的:
I'm writing a custom post_controller hook. As we know, codeigniter uri structure is like this:
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文件,这让我很困惑。
I've browsed the core Router.php file, which puzzled me a lot.
谢谢。
推荐答案
使用CodeIgniter URI核心类
通常在CodeIgniter Hooks 中,我们需要加载/实例化URI核心类
Using CodeIgniter URI core class
Generally within CodeIgniter Hooks, we need to load/instantiate the URI core class to reach the methods.
- 对于
post_controller_constructor
,post_controller
,... hooks,我们可以获得CodeIgniter超级对象并使用uri
类: - For
post_controller_constructor
,post_controller
, ... hooks, we can get the CodeIgniter super object and use use theuri
class:
# Get the CI instance
$CI =& get_instance();
# Get the third segment
$CI->uri->segment(3);
- 但 > pre_controller hook,我们没有访问CodeIgniter超级对象因此我们必须手动加载URI核心类如下:
- But for
pre_controller
hook, we don't have access to CodeIgniter super object So we have to load the URI core class manually as follows:
# Load the URI core class
$uri =& load_class('URI', 'core');
# Get the third segment
$id = $uri->segment(3); // returns the id
使用纯PHP
在此方法中,您可以使用 $ _ SERVER
数组来获取URI段:
Using pure PHP
In this approach you can use $_SERVER
array to fetch the URI segments as:
$segments = explode('/', trim($_SERVER['REQUEST_URI'], '/'));
$controller = $segments[1];
$method = $segments[2];
$id = $segments[3];
这篇关于如何检索codeigniter钩子中的第三个uri段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文