Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Codeigniter可变长度参数列表_Php_Codeigniter - Fatal编程技术网

Php Codeigniter可变长度参数列表

Php Codeigniter可变长度参数列表,php,codeigniter,Php,Codeigniter,我正在用Codeigniter制作一个教程系统,但我有点拘泥于在教程中使用子类别 URL结构如下所示:/tutorials/test/123/this-is-a-tutorial 教程是控制器 test是该类别的一个短代码 123是教程ID(用于SQL查询) 这本教程只是美化URL的一个小工具 我要做的是将类别作为第一个参数,将ID作为第二个参数传递给我的控制器函数: public function tutorial($category = NULL, $tutorial_id = NULL

我正在用Codeigniter制作一个教程系统,但我有点拘泥于在教程中使用子类别

URL结构如下所示:/tutorials/test/123/this-is-a-tutorial

  • 教程是控制器
  • test是该类别的一个短代码
  • 123是教程ID(用于SQL查询)
  • 这本教程只是美化URL的一个小工具
我要做的是将类别作为第一个参数,将ID作为第二个参数传递给我的控制器函数:

public function tutorial($category = NULL, $tutorial_id = NULL);
现在,如果我想要子类别(无限深度),比如:/tutorials/test/test2/123/另一个教程。我将如何实现这一点


谢谢

如果您需要变量类别,您可以使用类:
$this->uri->uri\u to\u assoc(n)

来读取无限参数,您至少有两个有用的工具:

因此,在控制器中:

  • 弹出最后一段/参数(这是你的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
}

其余的代码和验证取决于您具体想用参数做什么。

< P>您可能要考虑的另一个选项是CODENGITENER的控制器功能重映射功能,您可以使用它来重写默认行为。您将能够在控制器内定义一个函数,该函数将处理对该控制器的所有调用,并将剩余的URI参数作为数组传入。然后你可以对他们做任何你想做的事


有关该问题的文档参考,请参阅。

这些类别是动态的,并且也从数据库中提取。这看起来也很有趣。在PHP5.6+中,您可以使用。。。代币比如:函数sum(…$numbers){foreach($n)php.net/manual/en/functions.arguments.php