我正在使用 Codeigniter 制作一个教程系统,但我有点坚持为我的教程使用子类别。
URL 结构是这样的:/tutorials/test/123/this-is-a-tutorial
- 教程是 Controller
- test 是类别的简码
- 123 是教程 ID(在 SQL 查询中使用)
- this-is-a-tutorial 只是一个用来美化 URL 的插件
我所做的是将类别作为第一个参数和 ID 作为第二个参数传递给我的 Controller 函数:
public function tutorial($category = NULL, $tutorial_id = NULL);
现在,如果我想要子类别(无限深度),例如:/tutorials/test/test2/123/another-tutorial。我将如何实现?
谢谢!
最佳答案
要阅读无穷无尽的论据,您至少有两个有用的工具:
所以在你的 Controller 中:
- 弹出最后一个片段/参数(这是你的 slug,不需要)
- 假设最后一个参数是教程 ID
- 假设其余的是类别
像这样:
public function tutorial()
{
$args = func_get_args();
// ...or use $this->uri->segment_array()
$slug = array_pop($args);
$tutorial_id = array_pop($args); // Might want to make sure this is a digit
// $args are your categories in order
// Your code here
}
其余代码和验证取决于您具体要对参数执行的操作。
关于php - Codeigniter 变长参数列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11435025/