作成日:2017-11-21, 更新日:2018-12-20
基本
・クラス名の「extends(継承元)」が変わる
・「parent::before();」を使わないとダメなときがある
・「public function before() {}」はPHPの「function __construct() {}」と同じ感じ。
URLとPATH関連
・URLは通常「/コントローラー/アクション」となる。
・PATHは「app\classes\controller\コントローラー.php」となる
・クラス名は「Controller_コントローラー」となる。
・アクション名は「public function action_アクション」となる。
▼URL「hoge/zebra」となる場合
・PATHは「app\classes\controller\hoge.php」となる
・クラス名は「Controller_Hoge」となる。
・アクション名は「public function action_Zebra()」となる。
テンプレートを使わない場合
・ビューで使う変数は「$content」にいれておく。
・「$content」の各キーが変数名になる。
・下記の場合、ビューで使える変数は「$title, $h1, $main」。
<?php class Controller_〇〇〇 extends Controller { public function action_□□□() { $content = array( 'title' => 'テンプレート不使用', 'h1' => 'テンプレートを使わない内容のH1', 'main' => 'テンプレートを使わない内容', ); return \View::forge('〇〇〇/□□□', $content); } }
※「〇〇〇/□□□」が「hoge/zebra」→PATHは「app\views\hoge/zebra.php」
テンプレート使用
基本:テンプレートファイルを指定しない場合
・テンプレートファイルは「views/template.php」となる。
・「$this->template->〇〇〇」がテンプレート(views/template.php)で使える変数になる。
・下記の場合、テンプレート(views/template.php)で使える変数は「$title, $content」。
・「$content」には「〇〇〇/□□□」が格納される
・「〇〇〇/□□□」で使える変数は「$bodyContent」の各キーが変数名になる。
<?php class Controller_〇〇〇 extends Controller_Template { public function action_□□□() { $bodyContent = array( 'h1' => 'テンプレートを使った内容のH1', 'main' => 'テンプレートを使った内容:「views/template.php」を使う', ); $this->template->title = "テンプレート使用"; $this->template->content = \View::forge('〇〇〇/□□□', $bodyContent); } }
※「views/template.php」→PATHは「app\views\template.php」
※「〇〇〇/□□□」が「hoge/zebra」→PATHは「app\views\hoge\zebra.php」
応用:テンプレートファイルを指定する場合
・テンプレートファイルを「views/common/template.php」にしたいとき。
<?php class Controller_〇〇〇 extends Controller_Template { public $template = 'common/template'; public function before() { parent::before(); } public function action_□□□() { $content = array( 'h1' => 'テンプレートを使った内容のH1', 'main' => 'テンプレートを使った内容:「views/common/template.php」を使う', ); $this->template->title = "テンプレート使用"; $this->template->content = View::forge('〇〇〇/□□□', $content); } }
応用:アクションごとにテンプレートファイルを指定する場合
・特定のアクションのみテンプレートファイルを「views/common/template2.php」にしたいとき。
<?php class Controller_〇〇〇 extends Controller_Template { public $template = 'common/template'; public function before() { parent::before(); } public function action_□□□() { $this->template = 'common/template2'; parent::before(); $content = array( 'h1' => 'テンプレートを使った内容のH1', 'main' => 'テンプレートを使った内容:「views/common/template.php」を使う', ); $this->template->title = "テンプレート使用"; $this->template->content = View::forge('〇〇〇/□□□', $content); } }
ビューファイルにHTMLタグを渡したい
「View::forge()」の第3引数に「false」を使う。
$content = array( 'main' => '<p>HTMLタグをビューに渡したい</p>', ); View::forge('〇〇〇/□□□', $content, false)
※第3引数の初期値が「true」となり、HTMLタグを文字として渡すことになる。
・「第3引数:true」 - HTMLタグを文字として認識
・「第3引数:false」 - HTMLタグはHTMLタグとして認識
・「第3引数:指定しない」 - 「第3引数:true」と同じ
API用
・APIとかでJSONとか返してほしいとき
・下記の場合「http://ホスト/〇〇〇/□□□.json」でアクセスするとJSON等が返ってくる
<?php class Controller_〇〇〇 extends Controller_Rest { public function action_□□□() { $result = array( 'status' => false, 'message' => '', ); return $this->response($result); } }
▼「http://ホスト/〇〇〇/□□□.xxx」の「〇〇〇/□□□」が「http://ホスト/hoge/zebra.xxx」のとき
返ってくる値 | URL(拡張子だけ異なる) |
---|---|
JSON | http://ホスト/hoge/zebra.json |
XML | http://ホスト/hoge/zebra.xml |
CSV(ダウンロードされる) | http://ホスト/hoge/zebra.csv |
階層を深くしたい
・URLを「/コントローラー/子階層/アクション」としたい。
▼「/hoge/zebra/add」でアクセスさせたい場合
・ファイルは「app\classes\controller\hoge\zebra.php」
・コントローラー名は「hoge\zebra」を使い、アクション名は「add」を使う
class Controller_Hoge_Zebra extends Controller { public function action_add() { } }
引数として受け取る場合
コントローラー名「hoge」、アクション名「zebra」、「add」は引数として使う場合
class Controller_Hoge_ extends Controller { public function action_zebra($prm='') { // $prmには「文字列:add」が格納される } }
URLとファイルPATHのサンプル
URL | ファイルPATH | コントローラー名 | アクション名 |
---|---|---|---|
http://ホスト/hoge/add | app\classes\controller\hoge.php | Controller_Hoge | action_add() |
http://ホスト/hoge/zebra/add | app\classes\controller\hoge\zebra.php | Controller_Hoge_Zebra | action_add() |
http://ホスト/hoge/zebra/add 「add」は引数扱いにする |
app\classes\controller\hoge.php | Controller_Hoge | action_zebra($prms='') |