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 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建