LoginSignup
6
3

More than 5 years have passed since last update.

[FuelPHP]テンプレートクラスを使ったビューの共通化と切り替え

Last updated at Posted at 2017-12-04

[FuelPHP]テンプレートクラスを使ったビューの共通化と切り替え

FuelPHPとは

初心者向きの高速軽量な「PHP」のフレームワークです。
(フレームワークとは、、簡単いいますと、誰でも楽にサイト等を作ることができる機能が豊富なものです。)

公式http://fuelphp.jp/

フレームワークのダウンロード
http://fuelphp.jp/docs/1.9/installation/download.html

目的:ヘッダー、フッターを同じものを使用し、コンテンツ部分のみをアクションごとに変更

手順

1 テンプレートを作成する(temlate.php)

\fuel\app\views\template.php


<!DOCTYPE html>
<html>
<head>
</head>
<body>
    <header>
        <!-- header.phpファイルを読み込む-->
        <?php echo $header; ?>
    </header>
    <div id="content">
        <!-- 各アクションの内容を読み込む-->
        <?php echo $content; ?>
    </div>
    <footer>
        <!-- footer.phpファイルを読み込む-->
        <?php echo $footer; ?>
    </footer>
</body>
</html>

2 共通部分で読み込ませるheader.phpとfooter.phpを作成

(共通部分用のフォルダー「base」を作成)

\fuel\app\views\base\header.php


<div>
    共通ヘッダー
</div>

\fuel\app\views\base\footer.php



<div>
    共通フッター
</div>

3 Controller_Templateを継承したController_Base(base.php)を作成する

\fuel\app\classes\controller\base.php


class Controller_Base extends Controller_Template
{
    public function before()
    {
        parent::before();
        //header.phpをテンプレートの$headerとbindさせる。
        $this->template->header = View::forge('parts/header');
        //footer.phpをテンプレートの$footerとbindさせる。
        $this->template->footer = View::forge('parts/footer');
    }

    public function after($response)
    {
        $response = parent::after($response); // あなた自身のレスポンスオブジェクトを作成する場合は必要ありません。
        return $response; // after() は確実に Response オブジェクトを返すように
    }
}

4 Controller_Baseを継承したController_Hello(hello.php)を作成し、
action名「index」を追加する


\fuel\app\classes\controller\hello.php


//extendsでbaseコントローラーを継承
class Controller_Hello extends Controller_Base
{
    public function action_index()
    {
        //template.phpのcontentとアクション毎のviewをbindさせる
        $this->template->content = View::forge('hello/index');
    }
}

5 viewファイルindex.phpを作成する

\fuel\app\views\hello\index.php



<div>
    meinのコンテンツです。
</div>

これでHelloコントローラーのindexアクションが呼ばれた時に共通分のビューも読み込まれます。

え!?これを使ってフロント側を作ったのはいいが、管理者側のページを作りたい時も共通のヘッダーを使いたいが、フロント側とかぶってしまうよぉぉ、分別の仕方がわからない!?

ご安心を私も知りませんでした。。。。

1 テンプレートを作成する(temlate_admin.php)

\fuel\app\views\temlate_admin.php



<!DOCTYPE html>
<html>
<head>
</head>
<body>
    <header>
        <!-- header.phpファイルを読み込む-->
        <?php echo $header; ?>
    </header>
    <div id="content">
        <!-- 各アクションの内容を読み込む-->
        <?php echo $content; ?>
    </div>
    <footer>
        <!-- footer.phpファイルを読み込む-->
        <?php echo $footer; ?>
    </footer>
</body>
</html>

2 Controller_Templateを継承したController_Admin(admin.php)を作成する

\fuel\app\classes\controller\admin.php


class Controller_Admin extends Controller_Template
{
    //ここで共通の基礎部分になるテンプレートtemlate_admin.phpを宣言
    public $template = 'template_admin';

    public function before()
    {
        parent::before();
        //header.phpをテンプレートの$headerとbindさせる。
        $this->template->header = View::forge('admin/header');
        //footer.phpをテンプレートの$footerとbindさせる。
        $this->template->footer = View::forge('admin/footer');
    }

    public function after($response)
    {
        $response = parent::after($response); // あなた自身のレスポンスオブジェクトを作成する場合は必要ありません。
        return $response; // after() は確実に Response オブジェクトを返すように
    }
}

3 ヘッダーとフッターを\fuel\app\views\admin\配下に同じく作成する

これであとは同じようにController_Adminを継承したコントローラーを作成してアクションを呼び出せば、
共通分のビューも読み込まれます。

以上

6
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
3