【问题标题】:codeginiter controller found but still 404 error找到 codeigniter 控制器但仍然 404 错误
【发布时间】:2013-08-14 01:15:35
【问题描述】:

我有这个以下控制器 -

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

class News extends CI_Controller {

public function __construct()   {
    parent::__construct();
    $this->load->database();     

    $catid = $this->input->post('cat');

    if(isset($catid)){
        $this->filter($catid);
    }
}

private function filter($catid){
    $sql = "select * from news_master where instr(concat(',', `Category`, ','), ', $catid ,') <> 0 ";
    echo $sql;
}

}

提交表单时调用。如果我提交表单,控制器会调用filter 函数并打印$sql。奇怪的是,在它下面我收到默认的 404 错误消息。 这是截图。!

我无法理解可能的原因。

【问题讨论】:

  • 如何访问News Controller?你用route吗?
  • 并且不要使用isset()函数检查post()返回的数据。如果您尝试检索的项目不存在,该函数将返回 FALSE(布尔值)。所以isset() 总是会返回TRUE
  • 嗨,我没有使用路由。控制器是从 form action 调用的。提交表单后,它会转到新闻控制器。
  • 你在用什么action
  • @HashemQolami 哦.. 愚蠢的错误。我没有定义 index() 函数。相反,我直接从构造函数调用过滤器。这种愚蠢的高度。感谢并抱歉浪费您的时间。

标签: php codeigniter http-status-code-404 codeigniter-2


【解决方案1】:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class News extends CI_Controller {

public function __construct()   {
    parent::__construct();
    $this->load->database();     
}
public function index()
{
    $catid = $this->input->post('cat');

    if(isset($catid)){
        $this->filter($catid);
    }

}
private function filter($catid){
    $sql = "select * from news_master where instr(concat(',', `Category`, ','), ', $catid ,') <> 0 ";
    echo $sql;
}

}

刚刚注意到您从构造函数调用它。它应该在索引函数中。

【讨论】:

    【解决方案2】:

    私有函数在 codeigniter 中不充当页面

    private function filter($catid){
        $sql = "select * from news_master where instr(concat(',', `Category`, ','), ', $catid ,') <> 0 ";
        echo $sql;
    }
    

    需要更新为 PUBLIC

    public function filter($catid){
        $sql = "select * from news_master where instr(concat(',', `Category`, ','), ', $catid ,') <> 0 ";
        echo $sql;
    }
    

    【讨论】:

    • 是的.. 但我没有直接调用私有函数。它是从 index() 函数中调用的。即使我更改为public 错误仍然存​​在。
    • 哦.. 愚蠢的错误。我没有定义 index() 函数。相反,我直接从构造函数调用过滤器。这种愚蠢的高度。感谢并抱歉浪费您的时间。
    猜你喜欢
    • 2016-10-12
    • 1970-01-01
    • 2019-01-05
    • 2015-11-10
    • 1970-01-01
    • 2014-06-03
    • 2015-09-19
    • 1970-01-01
    • 2013-10-04
    相关资源
    最近更新 更多