【问题标题】:Trying to get property of non-object in Codeigniter template试图在 Codeigniter 模板中获取非对象的属性
【发布时间】:2014-05-16 03:41:29
【问题描述】:

我使用代码codeigniter 制作简单的模板。 我的代码是这样的......

这是 template.php

class Template {

protected $_ci;

function _construct() {
    $this->_ci=&get_instance();
}

function display($template, $data = null) {

    $data['_content'] = $this->_ci->load->view($template, $data, TRUE);
    $data['_header'] = $this->_ci->load->view('template/header', $data, TRUE);
    $data['_top_menu'] = $this->_ci->load->view('template/menu', $data, TRUE);
    $data['_right_menu'] = $this->_ci->load->view('template/sidebar', $data, TRUE);
    $this->_ci->load->view('/template.php', $data);
}


}

这是我的控制器welcome.php

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

class Welcome extends CI_Controller {

function __construct() {
    parent::__construct(); //you always have to call parent's constructor before ANYTHING
    $this->load->library('template');
    $this->load->helper('url');
}

public function index() {
    $this->template->display('welcome_message');
}

function contoh_parameter() {
    $this->template->display('view_parameter', array('judul' => 'Judul View'));
}

}

这是我的视图类,welcome_message.php

   <h1>Welcome to CodeIgniter!</h1>

   <div id="body">
    <p>The page you are looking at is being generated dynamically by CodeIgniter.</p>

    <p>If you would like to edit this page you'll find it located at:</p>
    <code>application/views/welcome_message.php</code>

    <p>The corresponding controller for this page is found at:</p>
    <code>application/controllers/welcome.php</code>

    <p>If you are exploring CodeIgniter for the very first time, you should start by reading the <a href="user_guide/">User Guide</a>.</p>
</div>

<p class="footer">Page rendered in <strong>{elapsed_time}</strong> seconds</p>

但我得到这样的错误:

A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: libraries/template.php

Line Number: 25

对不起,我还是 codeigniter 和 php 的新手。请帮帮我。

【问题讨论】:

  • 我认为你的构造函数只有一个_。应该是两个下划线:__constructor()

标签: php codeigniter


【解决方案1】:

首先你的构造方法不对,应该是__construct而不是_construct

第二次使用$this-&gt;_ci-&gt;load-&gt;view('/template.php', $data); 加载视图时,不应在开始路径使用任何斜线,直到视图目录可用于 CI。因此,如果您的视图文件是 CI 视图文件夹中的 template.php,则以这种方式使用它$this-&gt;_ci-&gt;load-&gt;view('template', $data);。您还使用.php 扩展 CI 自己添加了该扩展。

【讨论】:

    猜你喜欢
    • 2017-07-18
    • 2016-05-25
    • 1970-01-01
    • 2021-03-27
    • 2011-07-07
    • 2013-06-12
    相关资源
    最近更新 更多