简体   繁体   English

调用父构造函数时,CodeIgniter Controller会中断

[英]CodeIgniter Controller breaks when calling parent constructor

I have the following code in helloworld.php: 我在helloworld.php中有以下代码:

<?php

class Helloworld extends CI_Controller {

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

public function index()
{
    $this->load->model("helloworld_model");
    $data["result"] = $this->Helloworld_model->getData();
    $data["page_title"] = "CI Helloworld appis";
    $this->load->view("helloworld_view", $data);
}

}

?>

The code stops executing after calling the parent constructor, without giving absolutely any error messages. 代码在调用父构造函数后停止执行,而不会给出任何错误消息。 Nothing appears in /var/log/apache2/error.log either. /var/log/apache2/error.log中也没有任何内容出现。 If I echo something before constructor call, it is echoed. 如果我在构造函数调用之前回显一些东西,它就会被回显。 If I type gibberish before the constructor call, a proper error message is printed. 如果我在构造函数调用之前键入乱码,则会打印正确的错误消息。 Why is this happening? 为什么会这样?

The site is running on Ubuntu server 12.04 with Code Igniter 2.1.4. 该站点使用Code Igniter 2.1.4在Ubuntu服务器12.04上运行。 and PHP 5.3. 和PHP 5.3。

Other files are helloworld_model.php: 其他文件是helloworld_model.php:

<?php

class Helloworld_model extends CI_Model {

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

public function getData()
{
    $query = $this->db->get("data");

    if ($query->num_rows() > 0)
    {
        return $query->row_array();
    }
    else
    {
        show_error("Database is empty");
    }
}

}

?>

And helloworld_view.php: 和helloworld_view.php:

<html>
<head>
    <title><?php echo $page_title ?></title>
</head>

<body>
    <?php foreach($result as $row): ?>
        <h3><?php echo $row["title"]?></h3>
        <p><?php echo $row["text"]?></p>
        <br />
    <?php endforeach ?>

</body>
</html>

As far as I understand, the Controller constructor is what gets called absolutely first, so I don't think the rest of the files matter at this stage(?). 据我所知,Controller构造函数是绝对首先被调用的,所以我不认为其余的文件在这个阶段很重要(?)。

I had same problem that was resolved by changing 'dbdriver' => 'mysqli' to 'dbdriver' => 'mysql' in you config/database.php . 我有同样的问题,通过在config/database.php中将'dbdriver' => 'mysqli'更改为'dbdriver' => 'mysql'来解决。 Also make sure your database connection parameters are correct. 还要确保数据库连接参数正确。

My guess, and I would have to see your configs to be sure, is that there is a problem in the initialization of your Loader . 我的猜测,我必须看到你的配置,确保你的Loader初始化存在问题。 Most commonly this has to do with the automatically loaded libraries, and sometimes it has to do with a bad database configuration. 最常见的是这与自动加载的库有关,有时它与错误的数据库配置有关。 My first suggestion would be to try to get something working using the default configs. 我的第一个建议是尝试使用默认配置工作。 If that works, then you have a good starting point. 如果有效,那么你有一个很好的起点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM