如何将Codeigniter中的welcome.php更改为index.php?

我正在学习Codeigniter。 如您所知,首次安装软件包时,控制器中会有一个名为welcome.php的默认文件。 </ p>

我试图将该页面修改为index.php,这里是代码:</ p>

 &lt;?php 
defined(' BASEPATH')或退出('不允许直接访问脚本');

class索引扩展CI_Controller {
公共函数索引()
{
$ this-&gt; load-&gt; view('welcome_message') ;
}
}
</ code> </ pre>

我还更改了配置文件中的route.php:</ p>

   &lt;?php 
defined('BASEPATH')或退出('不允许直接访问脚本');
$ route ['default_controller'] ='索引';
$ route ['404_override'] ='';

$ route ['translate_uri_dashes'] = FALSE;
</ code> </ pre>

然后我通过输入以下路径访问该页面: http:// localhost / CI / index.php </ code>,但它说有两个错误:</ p>



  1. 消息:未定义属性:Index :: $ load。 文件名:controllers / Index.php </ li>
  2. 消息:在null上调用成员函数视图()。 文件名:controllers / Index.php </ li>
    </ ol>
    </ blockquote>

    我是否忘记更改其他内容以使其正常工作?</ p>
    </ DIV>

展开原文

原文

I'm currently learning Codeigniter. As you know, there is a default file called welcome.php in the controller when you first install the package.

I tried to modify that page to index.php, and here is the code:

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

class Index extends CI_Controller {
    public function index()
    {
        $this->load->view('welcome_message');
    }
}

I also changed the route.php in the config file:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$route['default_controller'] = 'Index';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

Then I access the page by entering this path: http://localhost/CI/index.php, but it says there are two errors:

  1. Message: Undefined property: Index::$load. Filename: controllers/Index.php
  2. Message: Call to a member function view() on null. Filename:controllers/Index.php

Did I forget to change something else in order to make it work?

duanduan1993
duanduan1993 您不能使用名称索引创建类。
4 年多之前 回复

1个回答



我下载了CI3.0.2并在我的计算机上尝试了您的代码。 我遇到了同样的问题,并且经过几次调试我发现了导致这个问题的原因。</ p>

你的类是索引,你的函数是索引2,在你没有定义时在php类中 构造函数 __ construct </ code>它将尝试查找是否存在与类名 Index </ code>具有相同名称的方法,因此在这种情况下 index </ code>函数是构造函数 类索引</ code>。 如果这让你感到困惑,你会看到这个文件:构造函数php官方文档 </ p >

解决方案:</ p>

 类索引扩展了CI_Controller {
公共函数__construct()
{

parent :: __ construct();
}
公共函数索引()
{
$ this-&gt; load-&gt; view('welcome_message');
}
}
</ code> </ pre>
</ div >

展开原文

原文

I downloaded CI3.0.2 and tried your code in my computer. I encountered the same problem, and with a few time debug I found what caused this problem.

Your class is Index and your function is index two, in php class when you don't define constructor __construct it will try to find if there's a method that have same name with class nameIndex, so in this situation index function is the constructor of class Index. if this confuse you see this document : constructor php official document

Solution:

class Index extends CI_Controller {
    public function __construct()
    {
        parent::__construct();
    }
    public function index()
    {
        $this->load->view('welcome_message');
    }
}

dongwei9771
dongwei9771 像魅力一样工作
4 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐