Asterisk - アスタリスク -

説明するほどでもないブログ

【FuelPHP】Controller_Template クラスを使ったサンプル

どうも。

ブログのトップでスクロールするのも煩わしいと思ったので、日記以外の記事は「続きを読む」機能を使う。

今回は Controller_Template クラスを使ったときのメモ。


使用するディレクトリ構造
/usr/share/nginx/html/fuelphp/
└── fuel
     └── app
          ├── classes
          │   └── controller
          │        └── sample.php
          ├── config
          │   └── routes.php
          └── views
               └── sample
                    └── index.php

(なーんか表示がズレてる感じがするんだよねぇ・・・)

テンプレートの作成
  • /usr/share/nginx/html/fuelphp/fuel/app/views/template.php
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>サンプル</title>
    <?php echo Asset::css('bootstrap.css'); ?>
    <?php echo Asset::css('fuelphp.css');   ?>
</head>
<body>
    <div id="header">
        <div class="row">
            <div id="logo"></div>
        </div>
    </div>

    <div class="container">
        <?php echo $content; ?>
        <hr/>
        <footer>
            <p class="pull-right">Page rendered in {exec_time}s using {mem_usage}mb of memory.</p>
            <p>
                <a href="http://fuelphp.com">FuelPHP</a> is released under the MIT license.<br>
                <small>Version: <?php echo Fuel::VERSION; ?></small>
            </p>
        </footer>
    </div>
</body>
</html>

このようにファイル名 template.php で作成すると、デフォルトで読み込んでくれる。

コントローラの作成
  • /usr/share/nginx/html/fuelphp/fuel/app/classes/controller/sample.php
<?php
    class Controller_Sample extends Controller_Template
    {
        public function action_index()
        {
            $this->template->content = View::forge('sample/index');
            $this->template->content->set('hoge', 'サンプル');
        }
    }
ビューの作成
  • /usr/share/nginx/html/fuelphp/fuel/app/views/sample/index.php
<?php
    echo $hoge;
?>
routes.php の編集
  • /usr/share/nginx/html/fuelphp/fuel/app/config/routes.php
<?php
return array(
    '_root_'  => 'sample/index',  // The default route
);
動作確認
  • ブラウザで localhost にアクセスする。

f:id:binder:20130427225433j:plain:w600:h378


以上です。