Hybrid

概要

REST コントローラと Template コントローラの両方の機能を一緒にもたらします

URLからのリクエストからの場合はActionメソッドをAjaxからの場合はRestコントローラーのget、postメソッドを使用します。


コントローラー

<?php
 
// サンプルコントローラー
class Controller_Sample10 extends Controller_Hybrid
{
 
	// テンプレートファイルの情報
	public $template='template_test';		// app/views/template_test.phpの場合
 
	// action前の処理を記述
	public function before()
	{
		parent::before();
	}
 
	// action後の処理を記述
	public function after($response)
	{
		$response = parent::after($response);
		return $response;
	}
 
	// アクションを省略時のデフォルトの画面
	public function action_index()
	{
		// actionの固有データ初期化
		$data = array();
		$data["username"] = 'ほげほげ10';
 
		// テンプレートに割り当てるデータを設定
		$this->template->title = 'サンプルテスト(10)';
		$this->template->content = View::forge('sample10/index', $data);
	}
 
	// アクションtestの画面
	public function action_test()
	{
		// actionの固有データ初期化
		$data = array();
		$data["username"] = 'ほげほげ10';
		$data["data"] = array(
			"p_no" => Input::post("p_no"),
			"p_name" => Input::post("p_name"),
			"p_tel" => Input::post("p_tel")
		);
 
		// テンプレートに割り当てるデータを設定
		$this->template->title = 'サンプルテスト(10)';
		$this->template->content = View::forge('sample10/test', $data);
 
	}
 
	// Ajax test
	public function get_test()
	{
		// 返却エリア
		$result = array(
			'p_no' => "get_1111",
			'p_name' => "get_2222",
			'p_tel' => "get_3333"
        );
 
		// 返却
		$this->response($result);
	}
 
	// Ajax test
	public function post_test()
	{
		// 返却エリア
		$result = array(
			'p_no' => "post_1111",
			'p_name' => "post_2222",
			'p_tel' => "post_3333"
        );
 
		// 返却
		$this->response($result);
	}
 
}
 
 

テンプレートビュー

<!DOCTYPE html>
<html>
	<head>
	    <meta charset="utf-8">
	    <title><?php echo $title; ?></title>
	    <?php echo Asset::js('jquery-1.8.2.js')?>
	</head>
	<body>
	    <div id="wrapper">
	        <h1><?php echo $title; ?></h1>
 
	        <div id="content">
	            <?php echo $content; ?>
	        </div>
	    </div>
	</body>
</html>
 
 

ビュー

<div>
			ようこそ, <?= $username ?>さん.
		</div>
		<div>
			<form id="frm_main" name="frm_main" action="./test" method="post">
				<table border="1">
					<tr>
						<td>no</td>
						<td><input id="p_no" name="p_no" type="text" value="" /></td>
					</tr>
					<tr>
						<td>name</td>
						<td><input id="p_name" name="p_name" type="text" value="" /></td>
					</tr>
					<tr>
						<td>tel</td>
						<td><input id="p_tel" name="p_tel" type="text" value="" /></td>
					</tr>
				</table>
				<br />
				<input id="btn_submit1" type="button" value="Action送信" />
				<input id="btn_submit2_1" type="button" value="Ajax送信(get)" />
				<input id="btn_submit2_2" type="button" value="Ajax送信(post)" />
			</form>
		</div>
		<script type="text/javascript">
		$(document).ready(function(){
			// ボタン押下時の処理
			$("#btn_submit1").bind("click", function(){
				$("#frm_main").submit();
			});
 
			// Ajax送信
			$("#btn_submit2_1").bind("click", function(){
 
				$.ajax({
				    url: './test',
				    type: 'get',
				    dataType: 'json',
				    timeout: 1000,
				    error: function(){
				        alert('データ取得失敗');
				    },
				    success: function(response){
				        $("#p_no").val(response.p_no);
				        $("#p_name").val(response.p_name);
				        $("#p_tel").val(response.p_tel);
				    }
				});
 
			});
 
			// Ajax送信
			$("#btn_submit2_2").bind("click", function(){
 
				$.ajax({
				    url: './test',
				    type: 'post',
				    dataType: 'json',
				    timeout: 1000,
				    error: function(){
				        alert('データ取得失敗');
				    },
				    success: function(response){
				        $("#p_no").val(response.p_no);
				        $("#p_name").val(response.p_name);
				        $("#p_tel").val(response.p_tel);
				    }
				});
 
			});
		});
		</script>
 
 

<div>
			ようこそ, <?= $username ?>さん.
		</div>
		<div>
			<table border="1">
				<tr>
					<td>no</td>
					<td><?php echo $data["p_no"]; ?><br /></td>
				</tr>
				<tr>
					<td>name</td>
					<td><?php echo $data["p_name"]; ?><br /></td>
				</tr>
				<tr>
					<td>tel</td>
					<td><?php echo $data["p_tel"]; ?><br /></td>
				</tr>
			</table>
		</div>
 
 

確認

初期画面

データを設定

画面よりSubmit

AjaxよりGETリクエスト

AjaxよりPOSTリクエスト


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