dongyu3659 2016-04-03 04:48
浏览 183
已采纳

如何将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?

  • 写回答

1条回答 默认 最新

  • dpr81047 2016-04-03 06:11
    关注

    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');
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 一道python难题2
  • ¥15 一道python难题
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备