在CodeIgniter中找不到控制器错误类

3

你好,我在CodeIgniter中遇到了“Controller未找到”错误。这是我的控制器代码:

<?php

class HelloWorld extends Controller
{

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

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

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

}
?>

这是视图代码: Hello, Nice to see you! 执行时出现以下错误: Fatal error: Class 'Controller' not found in D:\wamp\www\CodeIgniter_2.0.2\application\controllers\helloworld.php on line 2 请问有人能告诉我为什么会出现这个错误吗?
3个回答

5

CodeIgniter 2.x 版本中,所有核心类都添加了 CI_ 前缀。请查看 变更日志

将所有核心类添加了 CI_ 前缀。

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

}

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

}

希望这能帮到你。谢谢!

非常感谢...我非常困惑。 - Umer Singhera

1

请确保您的控制器继承了父控制器类,并检查文件名。

<?
     class Helloworld extends CI_Controller {

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

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

        }
        ?>

0
我曾经遇到和你一样的问题,而我通过将继承父类的代码行中的Controller替换为CI_Controller来解决它,这是最简单的方法,它可以正常工作。以下是你代码的解决方案。
<?php

class HelloWorld extends CI_Controller{

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

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

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

}
?>

然后你的代码将会执行


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接