gpt4 book ai didi

php - 如何在 codeigniter Hook 中检索第三个 uri 段

转载 作者:可可西里 更新时间:2023-11-01 13:14:38 25 4
gpt4 key购买 nike

我正在编写自定义 post_controller Hook 。正如我们所知,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, ... 钩子(Hook),我们可以获得 CodeIgniter super 对象并使用 uri 类:<
# Get the CI instance
$CI =& get_instance();

# Get the third segment
$CI->uri->segment(3);
  • 但是对于 pre_controller Hook ,我们无法访问 CodeIgniter super object 所以我们必须手动加载 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];

关于php - 如何在 codeigniter Hook 中检索第三个 uri 段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22300596/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com