如何在 CodeIgniter 中显示公共页面

How to display common pages in CodeIgniter

我是 CodeIgniter 的新手,我很困惑如何将我的 header.php 加载到我创建的所有其他页面。我希望我的 header 包含在我制作的每个页面中,而无需手动放置它们,这样每次我对 header 进行更改时,我都不会编辑整个页面。相反,我只会在 header.php 中进行更改。谢谢。

您可以在视图文件夹下创建一个文件夹,例如 -Layout 并在其中添加常用页面,例如 header、页脚。

在你的控制器中调用你可以这样做-

function index() {
        $this->data['survey'] = $this->survey_model->get_survey();
        $this->data['main_content'] = $this->controller . '/show';


        $this->load->view('layouts/main_content', $this->data);
    }

在这种情况下 show 是我要显示的视图页面,main_content 是我布局中的一个页面,其中 header 并提到了页脚。

创建公共页面调用layout.php

在那里面

<?php $this->load->view('includes/header'); ?> //site header

<?php $this->load->view($main_content); ?> //comes from controller. 

<?php $this->load->view('includes/footer'); ?> //site footer 

并在控制器中

function privacy_policy() 
{
    $data['main_content'] = 'pages/privacy_policy';
    $this->load->view('layout', $data);
}

main_content应该指向页面

所以它

  1. 减少你的时间
  2. 减少代码
  3. 通俗易懂
  4. 等等……

有很多方法,但我最喜欢的方法如下:

在您的控制器文件中

SomeController.php

function someFunction() {
        $this->data['main_page'] = 'someview';
        $this->load->view('layouts/main_template', $this->data);
}

在您的main_template.php视图中

<?php 
    $this->load->view('layouts/header');  // your common  header file
    // your dynamic  page file you can choose which page to load by changing the value of $main_page in controller.    
    $this->load->view($main_page);        
    $this->load->view('layouts/footer');  // your common  footer file
?>