【问题标题】:Codeigniter routing with :any使用 :any 的 Codeigniter 路由
【发布时间】:2017-08-08 15:27:55
【问题描述】:

我从 Codeigniter 2 迁移到 3。我在默认控制器目录以及称为内容和索引的子目录中都有控制器文件。当前的routes.php 文件如下所示:

$controller_dir = opendir(APPPATH."controllers");
while (($file = readdir($controller_dir)) !== false) {
    if (substr($file, -4) == ".php" ) {
        $route[substr($file, 0, -4)."(.*)"] = substr($file, 0, -4)."$1";
    } else if (substr($file, -5) == ".php/") {
        $route[substr($file, 0, -5)."(.*)"] = substr($file, 0, -5)."$1";
    }
}

$route['reviews'] = 'content/reviews/index/profile/$1';
$route['money/(:any)'] = 'indexes/money/index/$1';
$route['faq/do-i-need-to-signup'] = 'faq/question_answer';
$route['(:any)'] = 'index/profile/$1';

index/profile/$1 具有检查数据库中用户名的逻辑。我将它用作 www.example.com/robert,其中 robert 是动态名称(将其视为参考名称)。它在 CI 2 中工作。但是,在 CI 3 中,使用相同的代码出现 404 错误。

我在这里缺少什么?如果问题不清楚,请问我。

【问题讨论】:

标签: php codeigniter codeigniter-2 codeigniter-3


【解决方案1】:

现在您需要在路由中指定它将在查询部分中采用更多参数。试试这个:

$route['reviews/(:any)'] = 'content/reviews/index/profile/$1';

这应该可以解决 404 错误。您将能够像这样访问它:http://localhost/reviews/robert

如果你想传递更多参数,只需在路由中指定,然后$number

例如:

$route['reviews/(:any)/(:any)'] = 'content/reviews/index/profile/$1/$2';

【讨论】:

  • 感谢您的回答。到目前为止,我已经以这种方式解决了它。但是,我想知道它是否可能在 URL 中没有 reviews 字样。
  • 你可以写任何关键字,如@9​​87654326@,但强烈建议使用anyword,否则它不会寻找其他控制器并尝试映射到这个函数上。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多