- 2008-05-15 (Thu) 2:22
- PHP | ZendFramework
ZendFrameworkはindex.phpでフロントコントローラーを作って、そこからリクエスト内容を見てコントローラー・アクションを分けていくんですが、index.phpに色んな設定を書いていくとグローバル変数が何個もできます。
< ?php
// Zend_Session
Zend_Session::start();
$admin = new Zend_Session_Namespace('admin');
Zend_Registry::set('admin', $admin);
// Zend_Config
$config = new Zend_Config_Ini($dir . '/config/config.ini', 'production');
Zend_Registry::set('config', $config);
// Zend_Layout
Zend_Layout::startMvc(array('layoutPath' => $dir . '/layouts'));
// Zend_Db
$dbAdapter = Zend_Db::factory(Zend_Registry::get('config')->database);
Zend_Registry::set('dbAdapter', $dbAdapter);
/**
* Setup Controller
*/
$front = Zend_Controller_Front::getInstance();
$front->addModuleDirectory($dir . '/modules');
$front->throwExceptions(true);
// run
$front->dispatch();
Zend_Registryでどこからでもアクセスできるようにはしてるけど、グローバル変数が残ってることにはかわりない。
ここにも書いてある。なんか気持ち悪い。
で、bootstrap.phpを作ってindex.phpから読み込むことにする。
ディレクトリ構造はこんな感じ

index.phpはbootstrap.phpを読み込むだけにするので
< ?php
set_include_path( /* path */ );
require_once "../application/bootstrap.php";
Bootstrap::init();
とかだけにしておく。
boostrapの中でクラスを作ってそこで設定を初期化する。
< ?php
/**
* Bootstrap Class
*/
class Bootstrap {
static public function init()
{
// なんか初期化的な処理をゴリゴリ
}
}
Bootstrapの中でも処理ごとにメソッドをわけておいたほうが後から変更するときに分かりやすくていいかな。
Comments:0
Trackbacks:0
- Trackback URL for this entry
- http://blog.wozozo.jp/archives/95/trackback
- Listed below are links to weblogs that reference
- ZendFrameworkのindexにあるグローバルな奴らを考える from ヲゾゾ wozozo ウンコプログラマー 〜綺麗なお姉さん〜