作成日:2018-12-20, 更新日:2018-12-20
基本
例えば下記のような場合
ユーザー一覧ページ | /user |
ユーザー詳細ページ | /user/profile |
ユーザー詳細の編集ページ | /user/profile/edit |
ユーザー詳細の編集時の確認ページ | /user/profile/confirm |
何も考えずに「controller/user.php」「controller/user/profile.php」とかすると面倒なコトになる。
「/user/profile」のページを修正しようとしたとき
A.「controller/user.php」の「action_profile()」
B.「controller/user/profile.php」の「action_index()」
のどっちにしたのかで悩む。
ファイル数が少ないなら見ただけで分かるんだけど・・・多くなった時に
1.「controller/user/」より前に「controller/user.php」を見つけてしまう。
2.「controller/user.php」を開いて「action_profile()」が見つからない
→焦るってコトになる。
ここいらで悩むことは「ありえない!」っていうなら問題無し。
暫定案
色々試した結果、下記のように落ち着いた(2018-12-20時点)
方針
コントローラー名でフォルダを作って、その中にいれる。
条件によってはフォルダ内に入れれない場合は、継承を使って誤魔化す
・・・「controller/user.php」を開くってのは前述と同じなんだけど「継承元を見ればいい!」ってすぐに気付ける。
→これが、精神的にラク。
具体例
上記のURLの場合
1.作るファイル
・controller/user.php
・controller/user/main.php
・controller/user/profile.php
2.「controller/user.php」の中身
・「controller/user/main.php」を継承させる
※継承させるだけで中身は無し。
3.「user/main/index」のアクセスを拒否りたい場合。
・「controller/user/main.php」の「before()」内で条件分岐
▼「user/main/index」のアクセスを「user/index」にリダイレクト
// 現在のコントローラー名 $now = strtolower(\Request::main()->controller); // アクセスを拒否りたいコントローラー $come = strtolower('Controller_User_Main'); // リダイレクト先 $goto = strtolower('user'); if ( $now == $come ) { $path = str_replace($come, $goto, $now); $path = str_replace('_', '/', $path) . '/' . \Request::active()->action; $path = strtolower($path); \Response::redirect(\Uri::create($path), 'location', 200); }
最終的に下記のような感じ
「user/」フォルダの中を見ればOKと分かるだけでラク・・・
ユーザー一覧ページ | /user | 「controller/user.php」から「継承元:controller/user/main.php」の「action_index()」を使う |
ユーザー詳細ページが | /user/profile | 「controller/user/profile.php」の「action_index()」を使う |
ユーザー詳細の編集ページ | /user/profile/edit | 「controller/user/profile.php」の「action_edit()」を使う |
ユーザー詳細の編集時の確認ページ | /user/profile/confirm | 「controller/user/profile.php」の「action_confirm()」を使う |