我正在用Codeigniter制作一个教程系统,但我在我的教程中使用了一些子类别。
URL-结构如下所示:/tutorials/test/123/this- is -a-教程
我所做的是将类别作为第一个参数传递,ID作为第二个参数传递给我的控制器函数:
public function tutorial($category = NULL, $tutorial_id = NULL);现在,如果我想要子类别(无限深度),比如:/tutorials/test/test 2/123/另一个教程。我将如何实现这一点?
谢谢!
发布于 2012-07-11 14:55:56
要阅读无限参数,至少有两个有用的工具:
func_get_args()所以在你的控制器里:
就像这样:
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
}其余的代码和验证取决于您具体想要对参数做什么。
发布于 2012-07-11 14:44:50
如果需要变量类别,可以使用URI类:$this->uri->uri_to_assoc(n)
发布于 2012-07-11 23:50:18
您可能需要考虑的另一个选项是CodeIgniter的控制器函数重映射功能,您可以使用它来覆盖默认行为。您将能够在控制器中定义一个函数,该函数将处理对该控制器的所有调用,并将剩余的URI参数作为数组传入。然后你可以对他们做任何你想做的事。
有关此事项的文档参考,请参见这里。
https://stackoverflow.com/questions/11435025
复制相似问题