【问题标题】:codeigniter controller index function call requires its namecodeigniter 控制器索引函数调用需要它的名字
【发布时间】:2012-06-02 22:53:23
【问题描述】:

我有一个名为 Categories 的控制器和一个名为 index 的函数,带有 1 个参数 $cat_id

所以它看起来像这样:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class categories extends CI_Controller {

    function __construct(){
        parent::__construct();
    }

    public function index($cat_id = null){

    }
}

当我从浏览器调用它时,问题就来了……我用这个:

http://www.mysite.dev/categories/12311323

但返回404错误页面

如果我使用

http://www.mysite.dev/categories/index/12313131

会正常工作...

我怎样才能确保它在索引功能的 URL 中不需要索引?

【问题讨论】:

    标签: php function codeigniter controller call


    【解决方案1】:

    默认的 CodeIgniter 路由是 /controller/function/argument。为了指定参数,您需要首先指定函数。如果要指定不带函数的参数,则需要定义自定义路由。将此行添加到您的路由配置文件应该可以满足您的需求。

    $route['categories/(:num)'] = "categories/index/$1";
    

    【讨论】:

    • 是的,我有这个,但我忘了添加 /index/...该死的我需要睡觉
    【解决方案2】:

    您描述的是 CI 的默认(预配置)路由。您可以定义自己的模式,以使您在问题中描述的模式起作用。见:URI Routing

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-12-13
      • 1970-01-01
      • 1970-01-01
      • 2015-11-22
      • 2012-07-10
      • 2011-07-05
      • 1970-01-01
      相关资源
      最近更新 更多