1

CodeIgniter で Web サイトを作成していますが、ベスト プラクティスについて質問があります。

私は通常、ウェブページをモジュラー チャンクに分割します。ヘッダー、コンテンツ、フッター (および必要に応じて間に散りばめられたその他のもの。

「ヘッダー」と「フッター」のチャンクは通常静的ですが、その間の内臓はいくつかの変数 (たとえば、実際のページ - ホーム、概要、連絡先など) に応じて変化する可能性があります。

以下は、ページのコントローラーです。header_view、navigation_view、および footer_view は (ほとんどの場合) 変更されません。home_main_view になります。

public function index()
{
    $this->load->view('header_view');
    $this->load->view('navigation_view');
    $this->load->view('home_main_view');
    $this->load->view('footer_view');

}

たとえば、ユーザーが about ページに移動すると、ビューは次のようになります。

public function index()
{
    $this->load->view('header_view');
    $this->load->view('navigation_view');
    $this->load->view('about_view');
    $this->load->view('footer_view');

}

CIでその変更をどのように処理しますか? home_main_view を変数にして index() 関数に渡すのが最善ですか? 私は以前にこれを行ったことがあります (ユーザーは、ビュー ($var) を呼び出すインデックス ($var) を呼び出す $var を設定するコントローラーの関数をトリガーするリンクをクリックします)。

4

3 に答える 3

2

私のお気に入りのアプローチは、他のコンポーネントを所定の位置にロードするために使用するレイアウト (単なるビュー) を使用することです。ページの種類ごとに 1 つのレイアウトが必要なのは明らかですが、レイアウトを使用すると、後で簡単に変更できます。

さらに一歩進んでMY_Controller、レイアウトをロードするために使用するカスタム コントローラーに メソッドを作成しました。

<?php
class MY_Controller extends CI_Controller{
    /**
     * Global $data variable holder
     * @var stdClass
     */
    protected $data;
    /**
     * The templates path, used by the 'render' method to determine the template to load
     * This is the 'root' of the templates folder for a specific section
     * Must NOT contain a trailing slash!
     * @var string
     */
    protected $templates_path = 'site';
    /**
     * The layout used by the 'render' method to load the specified view
     * @var string
     */
    protected $layout = 'default';
    /**
     * Class initialisation
     */
    function __construct() {
        parent::__construct();
        /*init the $data variable*/
        $this->data = new stdClass();
        /*the base url shortcut - to be used where needed*/
        $this->data->base_url = $this->config->item('base_url');
        /*page encoding header*/
        $this->output->set_header('Content-Type: text/html; charset=utf-8');
    }
    /**
     * Helper method that will load a view using the layout configured
     * @param type $view
     */
    protected function render($view = FALSE){
        $this->data->page_template = $this->templates_path.'/'.$view;
        $this->data->templates_path = $this->templates_path;
        $this->load->view($this->templates_path.'/layouts/'.$this->layout, $this->data);
    }
}

次に、レイアウトで、$page_template変数に示されているビューを適切な場所にロードします

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); ?>
<!DOCTYPE html>
<html>
    <head>
        <?$this->load->view($templates_path.'/sections/head')?>
    </head>
    <body class="<?=$page_class?>">
        <?$this->load->view($templates_path.'/sections/header')?>
        <?if(isset($hpslider) && $hpslider)
            $this->load->view($templates_path.'/sections/hp_slider')?>
        <div class="pcontent">
            <div class="page">
                <?=$this->load->view($page_template);?>
            </div>
        </div>
        <?$this->load->view($templates_path.'/sections/footer')?>
    </body>
</html>

これがお役に立てば幸いです。

于 2012-09-18T11:43:36.390 に答える
1

私は CI を使用しませんが、 $_post 、 $_get 、またはセグメントを使用して、ロードするページをアクションに知らせることができます。

セグメントの例: example.com/index.php/controller/index/home に移動すると、「ホーム」ビューが読み込まれます。

public function index(){
$page=$this->uri->segment(3);
$this->load->view('header_view');
$this->load->view('navigation_view');
$this->load->view($page);
$this->load->view('footer_view');
}

http://codeigniter.com/user_guide/libraries/uri.html

于 2012-09-18T11:08:09.783 に答える
0

Header and footer in CodeIgniterというタイトルの SO スレッドで私の回答を参照してください。これが私がすべてのビューを処理する方法です。これはかなりモジュール化されたアプローチです。

コメント後に更新

例として、ホームページは次のように読み込まれます。

class Home extends CI_Controller{
    function index(){
        $d['v'] = 'home';

        //do database interaction here and 
        //ultimately assign results to some key in $d too.

        $this->load->view('init', $d);
    }
}
于 2012-09-18T15:31:46.577 に答える