尝试在my_controller中加载视图时出错 [英] Error when trying to load view in my_controller
问题描述
考虑我的代码:
<?php
class MY_Controller extends Controller {
public function __construct()
{
parent::Controller();
}
function _displayPage($page, $data = array()) {
$this->load->view('structure/header', $data);
$this->load->view($page, $data);
$this->load->view('structure/footer', $data);
}
}
?>
page.php
<?php
class Page extends MY_Controller {
function __construct() {
parent::__construct();
}
function index() {
$data['content'] = array('title'=>'hello world');
$this->_displayPage('home', $data);
}
}
?>
在我的浏览器上加载我的网页时,我收到此错误:
Upon loading my page on my browser, I get this error:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Page::$view
Filename: libraries/MY_Controller.php
Line Number: 11
有人知道我在做什么吗?
感谢
Does anyone know what I'm doing wrong? Thanks
推荐答案
如果我理解你想要完成的工作,你想设置一个模板包括您的页眉视图和页脚视图,但没有为您在应用程序中使用的每个控制器调用页眉和页脚视图。
If I understand what you're trying to accomplish correctly, you're wanting to setup a template that includes your header view and footer view, but without calling header and footer views for each controller you use throughout your application. Here's what I've done to accomplish this.
首先,在你的视图下创建一个新文件夹,对于这个例子,我们称之为'includes'。在新创建的includes文件夹中,创建三个文件,header.php,footer.php和template.php。适当地设置您的页眉和页脚,然后编辑您的template.php如下:
First, create a new folder under your views, for this example we'll call it 'includes'. Inside the newly created includes folder, create three files, header.php, footer.php and template.php. Setup your header and footer appropriately and then edit your template.php to look as follows:
<?php echo $this->load->view('includes/univ_header'); ?>
<?php echo $this->load->view($main_content); ?>
<?php echo $this->load->view('includes/univ_footer'); ?>
现在,从控制器,您可以定义您想要设置为'main_content'的视图。例如,如果你的视图文件夹中有home.php,并且你想用你的页眉和页脚包装你的控制器,如下:
Now, from your controller you can define what view you would like to set as your 'main_content'. For example, if you have home.php in your views folder and you want to wrap it with your header and footer you would do so in your controller as follows:
function index() {
$data['content'] = array('title'=>'hello world');
$data['main_content'] = 'home';
$this->load->view('includes/template', $data);
}
希望这有助!
这篇关于尝试在my_controller中加载视图时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!