controller共通のモデル、コンポーネント、ヘルパーを設定する方法です。
We all know the problem, there are always Models, Components or Helpers that we need for about 99% of all our actions/views and therefor would fit beautifully in our AppController. The only problem is, as soon as we define them via “var $uses = array(‘…’);” (etc.), we cannot easily add more items in our derived controllers without having to repeat the entire list.
ThinkingPHP and beyond » Dessert #10 – Default Models, Components & Helpers (2006-09-19)
これ私も気になっていた箇所なので、そうそう、といった感じでした。エントリではAppControllerのコンストラクタで$uses,$components,$helpersをマージする方法が紹介されています。
こういう方法もあるかなーと見ていると、上記エントリのコメントでさっくりと突っ込まれました。実はこれControllerクラスのコンストラクタでマージしてくれるようになっているんですね。以下のソースでAppControllerで設定したコンポーネントがTestControllerで使えます。すっきり、すっきり。
[app/app_controller.php]
< ?php
class AppController extends Controller {
var $components = array('RequestHandler');
}
?>
[app/controllers/test_controller.php]
< ?php
class TestController extends AppController {
var $name = "Test";
var $components = array('Session');
function index() {
// RequestHandlerもSessionも使える
if ($this->RequestHandler->isGet()) {
$this->Session->destroy();
}
}
}
?>
- Newer: PHP開発に便利な早見表集
- Older: CakePHP vendors.phpにセキュリティホール
トラックバック:1
- このエントリーのトラックバックURL
- /blog/2006/09/cakephp_common_models_components_helpers.html/trackback
- Listed below are links to weblogs that reference
- CakePHP controller共通のモデル、コンポーネント、ヘルパーを設定する from Shin x blog
- trackback from 眠るシーラカンスと新米プログラマー 07-04-06 (金) 2:18
-
cakePHP:AppControllerで共通処理を記述す…
以前に書いた、CSSを動的に読み込むプログラムなんかは、
全てのページで利用したいので、
継承元のAppControllerで記述しておきたいもの。

