ビューモデル

概要

classes/view配下にViewModelを定義して、データの編集とビューへ定義する変数を設定する

コントローラーで行っている処理をここで行う


ビューモデル定義

app/classes/view/sample05/index.phpの場合

<?php
 
// ビューモデル
class View_Sample05_Index extends ViewModel
{
	// ビューメソッド
	public function view()
	{
		$this->username = 'ほげほげ7';
		$this->title = 'サンプルテスト(ビューモデル1)';
	}
}
 
 

コントローラー

app/classes/controller/sample05.phpの場合

<?php
 
// サンプルコントローラー
class Controller_Sample05 extends Controller
{
	// アクションを省略時のデフォルトの画面
	public function action_index()
	{
		// ビューモデルで出力
		return Response::forge(ViewModel::forge('sample05/index'));
	}
 
}
 
 

ビュー

app/views/sample05/index.phpの場合

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title><?= $title ?></title>
	</head>
	<body>
		ようこそ, <?php echo $username; ?>さん.
	</body>
</html>
 
 

確認




最終更新:2013年05月22日 23:35