2

Ciでは、コントローラーのコンストラクターからビューを直接ロードする可能性があります。ページのヘッダーとフッターをロードしています(各関数で同じであるため)。

class Add extends CI_Controller{
    public function __construct()
    {
        parent::__construct();
        $this->load->helper('url');
        $this->load->view('header_view');
        $this->load->view('footer_view');       
    }

    function whatever()
    {
        //do stuff
    }

 }

しかし、これは私の関数をロードする前にフッタービューをロードするので、各関数の最後にビューを「手動で」ロードせずにそれを行う方法はありますか?

4

3 に答える 3

4

データとともにメイン ビューにヘッダー/フッターを追加するか、テンプレート ライブラリを使用します (これを使用します)

機能のメイン ビューにある場合。

// in view for html page
<?php $this->load->view('header'); ?>
<h1>My Page</h1>
<?php $this->load->view('footer'); ?>
于 2012-06-12T14:12:14.483 に答える
0

私はこのアプローチを思いつきました:

 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();
    }

  }
于 2014-02-19T00:47:33.807 に答える
0

コンストラクターでビューをレンダリングしないでください。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');
     }

 }
于 2012-06-12T14:12:08.590 に答える