Php 在控制器末端对点火器加载视图进行编码

Php 在控制器末端对点火器加载视图进行编码,php,codeigniter,Php,Codeigniter,在Ci上,您可以直接从控制器的构造函数加载视图,我正在加载页面的页眉和页脚(因为每个函数的页眉和页脚是相同的) 但这将在加载我的函数之前加载页脚视图,因此有没有办法不在每个函数末尾“手动”加载视图呢?您不应该在构造函数中呈现任何视图。CI控制器应更像这样: class Add extends CI_Controller { public function __construct() { parent::__construct(); $this-&

在Ci上,您可以直接从控制器的构造函数加载视图,我正在加载页面的页眉和页脚(因为每个函数的页眉和页脚是相同的)


但这将在加载我的函数之前加载页脚视图,因此有没有办法不在每个函数末尾“手动”加载视图呢?

您不应该在构造函数中呈现任何视图。CI控制器应更像这样:

class Add extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->helper('url'); 
    }

     function index()
     {
        $this->load->view('header_view');
        $this->load->view('home_page');
        $this->load->view('footer_view');
     }

     function whatever()
     {
        /*
         * Some logic stuff
         */

        $data_for_view = array(
            'product' => 'thing',
            'foo'     => 'bar'
        );

        $this->load->view('header_view');
        $this->load->view('show_other_stuff', $data_for_view);
        $this->load->view('footer_view');
     }

 }

您不应该在构造函数中呈现任何视图。CI控制器应更像这样:

class Add extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->helper('url'); 
    }

     function index()
     {
        $this->load->view('header_view');
        $this->load->view('home_page');
        $this->load->view('footer_view');
     }

     function whatever()
     {
        /*
         * Some logic stuff
         */

        $data_for_view = array(
            'product' => 'thing',
            'foo'     => 'bar'
        );

        $this->load->view('header_view');
        $this->load->view('show_other_stuff', $data_for_view);
        $this->load->view('footer_view');
     }

 }

我会在主视图中添加带有数据的页眉/页脚,或者使用模板库(我使用这个)

如果在功能的主视图中

// in view for html page
<?php $this->load->view('header'); ?>
<h1>My Page</h1>
<?php $this->load->view('footer'); ?>
//在html页面的视图中
我的页面

我会在主视图中添加带有数据的页眉/页脚,或者使用模板库(我使用这个)

如果在功能的主视图中

// in view for html page
<?php $this->load->view('header'); ?>
<h1>My Page</h1>
<?php $this->load->view('footer'); ?>
//在html页面的视图中
我的页面

我想出了这个方法:

 class Add extends CI_Controller{
    public function __construct()
    {
        parent::__construct();

        // load some static
        $this->data['page_footer'] = $this->common_model->get_footer();

    }
    private function view_loader () {
        //decide what to load based on local environment
        if(isset($_SESSION['user'])){
            $this->load->view('profile_view',  $this->data);
        } else {
             $this->load->view('unlogged_view',  $this->data);
        }
    }


    function index()
    {
        $this->data['page_content'] = $this->profile_model->do_stuff();

        // call once in every function. this is the only thing to repeat.
        $this->view_loader();
    }

  }

我提出了这种方法:

 class Add extends CI_Controller{
    public function __construct()
    {
        parent::__construct();

        // load some static
        $this->data['page_footer'] = $this->common_model->get_footer();

    }
    private function view_loader () {
        //decide what to load based on local environment
        if(isset($_SESSION['user'])){
            $this->load->view('profile_view',  $this->data);
        } else {
             $this->load->view('unlogged_view',  $this->data);
        }
    }


    function index()
    {
        $this->data['page_content'] = $this->profile_model->do_stuff();

        // call once in every function. this is the only thing to repeat.
        $this->view_loader();
    }

  }

您可以使用在控制器函数(post\u controller\u构造函数)之前加载页眉,然后加载页脚(post\u controller)。@Rocket他不能也使用
\uu destruct()
方法吗?或者我有错误的概念吗?使用钩子是一种创造性的方法,但是如果你不想在100%的时间里都使用页眉/页脚,它会变得很麻烦。你可以在控制器函数(post_controller_构造函数)之前加载页眉,然后在控制器函数(post_controller)之后加载页脚。@Rocket他不能也使用
\uu destruct()
方法也是如此吗?或者我有错误的概念吗?使用钩子是一种创造性的方法,但是如果你不想在100%的时间里使用页眉/页脚,它可能会变得毛茸茸的。我认为他试图避免编写
$this->load->view('header_view')
$this->load->view('footer_view')$this->load->view('header_view')
$this->load->view('footer_view')模板为+1。来自文档:“如果您不喜欢从每个控制器方法调用页眉、页脚和其他全局视图,那么模板非常适合您。”:-)+1表示模板。来自文档:“如果您不喜欢从每个控制器方法调用页眉、页脚和其他全局视图,则模板非常适合您。”:-)