Error Class Controller not found in CodeIgniter

谁都会走 提交于 2019-12-31 02:29:44

问题


Hello, I am getting Controller not found error in CodeIgniter. This is my Controller code

<?php

class HelloWorld extends Controller
{

    function HelloWorld()
    {
        parent::Controller();
    }

    function index()
    {
        $this->load->view('index_view');
    }

    function hello()
    {
        $this->load->view('hello_view');
    }

}
?>

This is the view code:

Hello, Nice to see you!

I have this error when it executes:

Fatal error: Class 'Controller' not found in D:\wamp\www\CodeIgniter_2.0.2\application\controllers\helloworld.php on line 2

Can anyone tell me why I get this error?


回答1:


As of CodeIgniter 2.x CI_ prefix is added to all core classes. Check the Change Log.

Added CI_ Prefix to all core classes.

For CodeIgniter 2.x

<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class HelloWorld extends CI_Controller
{

    function __construct()
    {
        parent::__construct();
    }

    function index()
    {
        $this->load->view('index_view');
    }

    function hello()
    {
        $this->load->view('hello_view');
    }

}

For CodeIgniter 1.x

<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class HelloWorld extends Controller
{

    function HelloWorld()
    {
        parent::Controller();
    }

    function index()
    {
        $this->load->view('index_view');
    }

    function hello()
    {
        $this->load->view('hello_view');
    }

}

Hope this helps you. Thanks!!




回答2:


Make sure your controller extends the parent controller class and also check your file name .

<?
     class Helloworld extends CI_Controller {

            function index()
            {
                   $this->load->view('index_view');    
            }

                function hello(){
                $this->load->view('hello_view'); 
                }

        }
        ?>



回答3:


i faced the same problem as you, and i solved it as simple as possible just by replacing the word Controller in the line of extending parent class by CI_Controller and it's work probably and here is the solution in your code

<?php

class HelloWorld extends CI_Controller{

   function HelloWorld() {
    parent::Controller();
   }

  function index() {
    $this->load->view('index_view');
  }

  function hello() {
    $this->load->view('hello_view');
  }

}
?>

and your code will execute



来源:https://stackoverflow.com/questions/6492544/error-class-controller-not-found-in-codeigniter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!