CakePHPが起動されてからdispatchするまで
app/webroot/.htaccess
リクエストされたURLをクエリに付与してindex.phpを呼び出す形式に書き換え
RewriteCond %{REQUEST_FILENAME} !-d # ディレクトリが存在しない場合 RewriteCond %{REQUEST_FILENAME} !-f # ファイルが存在しない場合 RewriteRule ^(.*)$ index.php?url=$1 [QSA,L] # URLをクエリに付与してindex.phpを実行
- QSA=リクエストされたクエリ文字列も付与する
app/webroot/index.php
基本的なパス等の定数やインクルードパス等の設定を行い、Dispatcher::dispatch()をコール。
コアの設定はcake/bootstrap.phpをincludeすることで行われる。
<?php // 略(アプリ別の基本的なパス等の定数定義) if (!defined('CORE_PATH')) { // ini_setが使用できる場合は、include_pathを設定する if (function_exists('ini_set') && ini_set('include_path', CAKE_CORE_INCLUDE_PATH . PATH_SEPARATOR . ROOT . DS . APP_DIR . DS . PATH_SEPARATOR . ini_get('include_path'))) { // 相対パスでincludeできるのでnull define('APP_PATH', null); define('CORE_PATH', null); } else { // 絶対パスでファイルをincludeできるようにパスを定義 define('APP_PATH', ROOT . DS . APP_DIR . DS); define('CORE_PATH', CAKE_CORE_INCLUDE_PATH . DS); } } // cake/bootstrap.phpを読み込むことでCakephpのコアの設定を行う if (!include(CORE_PATH . 'cake' . DS . 'bootstrap.php')) { trigger_error("CakePHP core could not be found. Check the value of CAKE_CORE_INCLUDE_PATH in APP/webroot/index.php. It should point to the directory containing your " . DS . "cake core directory and your " . DS . "vendors root directory.", E_USER_ERROR); } if (isset($_GET['url']) && $_GET['url'] === 'favicon.ico') { return; } else { // dispatchする $Dispatcher = new Dispatcher(); $Dispatcher->dispatch($url); } if (Configure::read() > 0) { // debugレベルが1以上の場合にアプリ実行にかかった時間を表示する echo "<!-- " . round(getMicrotime() - $TIME_START, 4) . "s -->"; }
cake/bootstrap.php
コアファイルを読み込み、Configure::getInstance()をコール。
<?php // $bootstrapはmod_rewriteや.htaccessが使えない場合に、 // cakeディレクトリと並列のindex.phpで定義される if (!isset($bootstrap)) { // a()、h()、env()などのグローバルな関数定義の読込み require CORE_PATH . 'cake' . DS . 'basics.php'; // 起動開始時間を保持 $TIME_START = getMicrotime(); // 各種パスの定数定義読込み require CORE_PATH . 'cake' . DS . 'config' . DS . 'paths.php'; // 各種クラスの読込み require LIBS . 'object.php'; require LIBS . 'inflector.php'; require LIBS . 'configure.php'; } require LIBS . 'cache.php'; Configure::getInstance(); // コアの設定を行う $url = null; // index.phpでDispatcher::dispatch()に渡す引数 App::import('Core', array('Dispatcher'));
Configure::getInstance()
getInstance()は名前通り、唯一のインスタンスを生成・保持して返すメソッド。
__loadBootstrap()でapp以下のbootstrapの読込みと各種設定を行う。
なぜ配列にインスタンスを格納しているかはわかりません。
<?php class Configure extends Object { function &getInstance($boot = true) { static $instance = array(); if (!$instance) { $instance[0] =& new Configure(); // 唯一のインスタンス $instance[0]->__loadBootstrap($boot); } return $instance[0]; } }
Configure::__loadBootstrap()
bootstrap.phpやcore.phpの読込みとキャッシュ、タイプ別のパス設定を行う
<?php class Configure extends Object { function __loadBootstrap($boot) { // 下記のbootstrap.php読込みで上書きできる値 $modelPaths = $behaviorPaths = $controllerPaths = $componentPaths = $viewPaths = $helperPaths = $pluginPaths = $vendorPaths = $localePaths = $shellPaths = null; if ($boot) { Configure::write('App', array('base' => false, 'baseUrl' => false, 'dir' => APP_DIR, 'webroot' => WEBROOT_DIR)); // app/config/core.php読込み // セキュリティ、セッション、キャッシュなどの設定 if (!include(CONFIGS . 'core.php')) { trigger_error(sprintf(__("Can't find application core file. Please create %score.php, and make sure it is readable by PHP.", true), CONFIGS), E_USER_ERROR); } // app/config/bootstrap.php読込み if (!include(CONFIGS . 'bootstrap.php')) { trigger_error(sprintf(__("Can't find application bootstrap file. Please create %sbootstrap.php, and make sure it is readable by PHP.", true), CONFIGS), E_USER_ERROR); } if (Configure::read('Cache.disable') !== true) { // 略 (cache設定) } // model、view、controllerなど、タイプ別にそれぞれのincludeディレクトリを設定。 // app/config/bootstrap.phpで、ユーザが追加できるパス設定もここで読込み Configure::buildPaths(compact( 'modelPaths', 'viewPaths', 'controllerPaths', 'helperPaths', 'componentPaths', 'behaviorPaths', 'pluginPaths', 'vendorPaths', 'localePaths', 'shellPaths' )); } } }