php - Codeigniter 变长参数列表

标签 php codeigniter

我正在使用 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/

相关文章:

codeigniter - 在 View 中使用 HTML 而不是 CodeIgniter 助手

php - 如何使表格中显示的数据随选择框中选择的值而变化?

php - 将数据从表单值传递到 ajax 调用 (Javascript)

php - 在虚拟名册上对用户进行分组

PHP 填充按钮 onclick 问题

Facebook 无法抓取我的网址

PHP - CodeIgniter - 为 foreach() 提供的参数无效

php - 无法删除 root 上 WordPress 站点的子文件夹中的 codeigniter 的 index.php

php - MySQL 全文搜索 where category = something

php - 查询在 phpMyAdmin 中有效,但不能通过 mysql_query