加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > PHP > 正文

php – 如何在codeigniter钩子中检索第三个uri段

发布时间:2020-05-25 08:32:27 所属栏目:PHP 来源:互联网
导读:我正在编写一个自定义的post_controller钩子.我们知道,codeigniter uri结构是这样的: example.com/class/function/id/ 和我的代码: function hook_acl(){ global $RTR; global $CI; $controller = $RTR-class; // the class part

我正在编写一个自定义的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文件,这让我很困惑.

谢谢.

使用CodeIgniter URI核心类

通常在CodeIgniter Hooks中,我们需要加载/实例化URI核心类以到达方法.

>对于post_controller_constructor,post_controller,… hooks,我们可以获取CodeIgniter超级对象并使用uri类:

# Get the CI instance
$CI =& get_instance();

# Get the third segment
$CI->uri->segment(3);

>但是对于pre_controller挂钩,我们无法访问CodeIgniter超级对象所以我们必须手动加载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];

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读