【问题标题】:CodeIgniter: controller helloworld.php returns blank pageCodeIgniter:控制器 helloworld.php 返回空白页
【发布时间】:2013-07-06 00:51:41
【问题描述】:

我正在运行 CodeIgniter 2.1.3。

我已经浏览过诸如但无法解决我的问题的帖子:

Blank Screen with CodeIgniter

我已经在 /var/www/ci/ 下安装了 CodeIgniter 并以 http://localhost/ci/ 访问它

我已经创建了简单的页面 application/controllers/helloworld.php

<?php
class HelloWorld extends Controller {
  function HelloWorld() {
  //function __construct() {
    //parent::__construct();
    parent::Controller();
  }
  function index() {
    echo "Hello, World!";
  }
}

但是 http://localhost/ci/index.php/helloworld/

给我一​​个空白页。我该如何解决这个问题?

我什至尝试将 config.php 更改为包含

$config['base_url'] = 'http://localhost/ci/';

(在 localhost 中没有多余的空间)。

我启用了 mod_rewrite,我启用了 mysqli php 模块。

我哪里错了?

谢谢。

【问题讨论】:

    标签: codeigniter codeigniter-2 codeigniter-url


    【解决方案1】:

    在 CodeIgniter 的 2.x.x 版本中,这些类被称为 CI_Controller(每个其他系统类都以 CI_ 为前缀。

    尝试像这样改变它:

    class HelloWorld extends CI_Controller {
       // if you don't want to do anything in the __controller you don't have to
       // override it, so its omitted
    
       public function index() {
           echo "Hello, World!";
       }
    }
    

    【讨论】:

    • 我将 Controller 更改为 CI_Controller 但还必须删除构造函数才能使其工作。为什么需要删除构造函数?添加 CI_Controller 或 __construct 构造函数将不起作用。为什么?谢谢。
    • 这不是必需的,但如果你想包含它,请像parent::__construct() 一样写它。 php5 不推荐使用类名来调用它。在您的情况下,它会尝试调用现有方法(如果您扩展 CI_Controller 方法 Controller 将只是一些常规方法)。
    • 完全正确。函数() __construct { parent::__construct(); } 是唯一似乎有效的构造函数语法(没有引用 CI_Controller 或 HelloWorld)。谢谢。
    • 这是write a constructor in PHP 5+ 的正确方式。让它成为一种习惯;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-19
    • 2023-03-09
    • 2020-07-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多