由于某种原因,codeigniter helloworld应用程序未运行


codeigniter helloworld app not running for some reason

我试图遵循这里的教程,它只是一个从数据库中提取数据的简单helloworld。

我使用的是CI 3.0.6。

我以前从未使用过codeigniter,但我想尝试一个框架。

无论如何,我认为我正确地遵循了教程(我确实认为教程使用了稍微旧一点的codeigniter版本,因为有些路径有点不同),但我无法使其工作,它给了我错误The page you requested was not found.

我对此有几个问题。

教程告诉我要使用模型,但是什么模型?

我该怎么做?如果我能让它正常工作,我毫不怀疑我可以把它作为一种学习经验来扩展我的项目。

我的代码:helloworld.php(控制器@BASEDIR/应用程序/控制器)_:

<?php
    class Helloworld extends Controller{
        function index()
        {
            $this->load->model('helloworld_model');
            $data['result'] = $this->helloworld_model-><span class="sql">getData</span>();
            $data['page_title'] = "CI Hello World App!";
            $this->load->view('helloworld_view',$data);
        }
    }
?>

helloworld_model.php(型号@BASEDIR/application/models):

<?php
class Helloworld_model extends Model {
    function Helloworld_model()
    {
        // Call the Model constructor
        parent::Model();
    }
    function getData()
        {
            //Query the data table for every record and row
            $query = $this->db->get('data');
            if ($query->num_rows() > 0)
            {
                //show_error('Database is empty!');
            }else{
                return $query->result();
            }
        }
}
?>

helloworld_view(view@BASEDIR/application/views):

<html>
    <head>
        <title><?=$page_title?></title>
    </head>
    <body>
        <?php foreach($result as $row):?>
        <h3><?=$row->title?></h3>
        <p><?=$row->text?></p>
        <br />
        <?php endforeach;?>
    </body>
</html>

我的数据库信息:

$db['default'] = array(
    'dsn'   => '',
    'hostname' => 'private',
    'username' => 'private',
    'password' => 'private',
    'database' => 'database',
    'dbdriver' => 'mysql';
    'dbprefix' => 'va',
    'pconnect' => FALSE,
    'db_debug' => (ENVIRONMENT !== 'production'),
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'encrypt' => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => TRUE
);

&我的自动加载:

$autoload['libraries'] = array('database');

我正试图通过以下方式访问该页面:http://myurl.com/dev/index.php/helloworld/

/dev/是我的安装目录。

再说一遍,我以前从未使用过框架,所以这个问题可能很容易解决。

如果你完全遵循了该教程,那么你需要在url中不使用index.php的情况下访问该页面。

也使用PHP5构造函数,因此更改:

function Helloworld_model()
{
    // Call the Model constructor
    parent::Model();
}

收件人:

function __construct()
{
    // Call the Model constructor
    parent::__construct();
}

这也毫无意义:

if ($query->num_rows() > 0)
{
    //show_error('Database is empty!');
}
else
{
    return $query->result();
}

if/else内部的代码应该切换。若返回的行数大于0,则返回结果else显示数据库为空的错误。

控制器中的最后一件事:

$data['result'] = $this->helloworld_model-><span class="sql">getData</span>();

只将数据库结果存储到数据变量,将其传递给视图,并包装视图中的任何数据元素。

$data['result'] = $this->helloworld_model->getData();

如果您阅读了文档控制器在此处输入链接描述

您可以看到,在application/controller/Name.php中,控制器的名称需要大写,在您需要的类中(记住扩展CI_controller)

<?php
class Controller_name extends CI_Controller {
        public function view($page = 'home')
        {
        }
}

对模型的调用是可以的$this->load->model('helloworld_model'); m,但请记住,在applicationoin/models/model_name.php中也需要大写,并且insede中需要(记住扩展CI_model)

<?php 
class Blog_model extends CI_Model {
        public function __construct()
        {
                // Call the CI_Model constructor
                parent::__construct();
        }

是的,您需要自动加载数据库,但在数据库配置(application/config/database.php)中将dbdriver更改为mysqli