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

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?

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');
    }
}