用户
 找回密码
 入住 CI 中国社区
搜索
查看: 2989|回复: 3
收起左侧

[HELP] codeigniter controlers中加载config失败

[复制链接]
发表于 2011-11-16 09:40:54 | 显示全部楼层 |阅读模式

我的配置文件config.php如下:
$config['base_url']    = 'http://localhost:8080';(我加的)
$config['index_page'] = 'index.php';
.
.(中间没有改动动省略。。。。)
.
$config['css'] = "mystyles.css";(我加的)
-----------------------------------------------------
我的routers.php文件如下:
$route['default_controller'] = "Start";
$route['404_override'] = '';
-----------------------------------------------------
我的start.php文件如下:
<?php
class Start extends CI_Controller {
    var $base;
    var $css;
    function Start()
    {
        parent::CI_Controller();
        $this->config->load('config');
        $this->base = $this->config->item('base_url','config');
        $this->css = $this->config->item('css');
    }
    function index($name)
    {
        $data['css'] = $this->css;
        $data['base'] = $this->base;
        $data['mytitle'] = 'Welcome to this site';
        $data['mytext'] = "Hello, $name, now we're getting dynamic!";
        #$this->load->view('testview', $data);
    }
}
?>
---------------------------------------------------------------------------------
我的testview.php文件如下:
<html>
<head>
  <!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN'http:\/\/www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>
  <html xmlns="http:\/\/www.w3.org/1999/xhtml">
  <title>Web test Site</title>
  <base href="<?php echo "$base"; ?>">
  <link rel="stylesheet" type="text/css" href="<?php echo "$base/$css";?>">
</head>
<body>
  <h1><?php echo $mytitle; ?> </h1>
  <p class="test"> <?php echo $mytext; ?> </p>
</body>
</html>
-----------------------------------------------------
为什么我在运行的时候会如下报错误:
Fatal error:  Call to undefined method CI_Controller::CI_Controller() in D:\phpnow\htdocs\CodeIgniter\application\controllers\start.php on line 7
-----------------------------------------
要是我不要 parent::CI_Controller();这句代码,即我的start.php文件如下:
<?php
class Start extends CI_Controller {
    var $base;
    var $css;
    function Start()
    {
        #parent::CI_Controller();
        $this->config->load('config');
        $this->base = $this->config->item('base_url','config');
        $this->css = $this->config->item('css');
    }
    function index($name)
    {
        $data['css'] = $this->css;
        $data['base'] = $this->base;
        $data['mytitle'] = 'Welcome to this site';
        $data['mytext'] = "Hello, $name, now we're getting dynamic!";
        #$this->load->view('testview', $data);
    }
}
?>
则报如下错误:
A PHP Error was encounteredSeverity: Notice
Message:  Undefined property: Start:config
Filename: controllers/start.php
Line Number: 8


Fatal error:  Call to a member function load() on a non-object in D:\phpnow\htdocs\CodeIgniter\application\controllers\start.php on line 8

我该怎么加载config.php这个里面的配置啊?比如我想要我自己定义的$config['css'] = "mystyles.css";(我加的)这个变量。我的start.php该怎么写啊?是不是我对codeigniter理解有错误?是语法不对呢?还是我的配置有问题?
(要是直接输出'HELLOWORLD'这样简单的东西却不报错了。。。。)
请高手帮忙啊。小弟在此谢过了


发表于 2011-11-16 10:07:41 | 显示全部楼层
config.php会自动加载的,你只要$this->config->item();来取里面的值就是了
 楼主| 发表于 2011-11-16 10:15:28 | 显示全部楼层
jeongee 发表于 2011-11-16 10:07
config.php会自动加载的,你只要$this->config->item();来取里面的值就是了

<?php
class Start extends CI_Controller {
    var $base;
    var $css;
        function Start()
        {
                #parent::CI_Controller();
                #$this->config->load('config');
                $this->base = $this->config->item('base_url');  //这是第九行。。。。
                $this->css = $this->config->item('css');
                echo $config['css'];
        }
        function index()
        {
                $data['css'] = $this->css;
                $data['base'] = $this->base;
                $data['mytitle'] = 'Welcome to this site';
                $data['mytext'] = "Hello, now we're getting dynamic!";
                #$this->load->view('testview', $data);
        }
}
?>
我是这样子调用的啊。可是还是给我报错:A PHP Error was encountered

Severity: Notice

Message: Undefined property: Start:config

Filename: controllers/start.php

Line Number: 9

Fatal error: Call to a member function item() on a non-object in D:\phpnow\htdocs\CodeIgniter\application\controllers\start.php on line 9

提示我出现以上错误。。。。
发表于 2011-11-16 10:25:06 | 显示全部楼层
本帖最后由 jeongee 于 2011-11-16 10:25 编辑

function __construct()
        {
                parent::__construct();
                #$this->config->load('config');
                $this->base = $this->config->item('base_url');  //这是第九行。。。。

本版积分规则