FuelPHPをやってみる (13) - Controller_Hybridを使う -
Controller_Template + Controller_Rest = Controller_Hybridらしい
fuel/app/classes/controller/sample.php
<?php
class Controller_Sample extends Controller_Hybrid {
public $template = "layout";
protected $no_data_status = 404;
protected $no_method_status = 404;
public function get_index() {
$this->template->content = View::forge("sample");
}
public function get_list() {
if ($this->format !== "json") {
throw new HttpNotFoundException();
}
return $this->response(
array(
"message" => "hoge fuga foobar"
)
);
}
}
まぁ見る限りController_TemplateとController_Restな方式が混ざり込んどるみたいな感じで使えると
fuel/app/views/layout.php
<html>
<body>
<?php echo $content; ?>
</body>
</html>
Controller_Templateのlayoutなんで詳細は(ry
fuel/app/views/sample.php
<script type="text/javascript">
(function(undefined) {
var xhr = new XMLHttpRequest();
xhr.open("GET", "/sample/list", true);
xhr.setRequestHeader("Accept", "application/json");
xhr.onload = function() {
if (xhr.status !== 200) {
console.log("ERROR");
return;
}
console.log(xhr.responseText);
};
xhr.send();
})();
</script>
XMLHttpRequestを使ってget_listから出されるJSONレスポンスを取得するだけ