URLのディスパッチ
URLのディスパッチは、config/routes.phpで行われている。
$Route->connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
$Routeで使われているのは、class Routerで、
Parses the request URL into controller, action, and parameters.
とのこと。上の例だと、URLが/pages/*にマッチすると、pagesコントローラーのdisplayアクションを実行するのがデフォルトの動作となる。
component
componentは、controllerに対して機能をよく使われる提供するもの。app/controllers/componentsに置かれ、Objectクラスを継承し、xyzComponentというクラス名として用意される。
componentを利用するには、controllerクラスから、
class ... {
var $components = array('Session');
...
}
などとすれば良い。
クラス継承
class rdSimpleAuthComponent extends Object {
var $allow = null;
function startup() {
}
}
__FILE__
PHPの__FILE__は、現在アクセスされているファイル名が返される。
__FILE__ → /web/tohdoh/webroot/index.php
dirname()は、ディレクトリ名を返す。
dirname("/web/tohdoh/webroot/index.php") → /web/tohdoh/webroot
Some of the flow
app/webroot/index.php calls
Dispatcher and it calls
Router::parse() in
app/config/routes.php where it connects '/' to
page controller's
display action.
Dump array into a string
Set the second parameter of print_r() to true.
$myvar = print_r($array_var, true);
ログ出力
cakephpのエラーログはなぜかApacheのerror_logにはいかず、独自のファイルにログが出力される。出力先は/cake/app/tmp/logs/error.log
$this->log('logging test');
log()メソッドは、Objectクラスで定義されている。第2引数を指定する事もできるが、この場合はLOG_ERRORとLOG_DEBUGのいずれかが使える。実際の出力は、CakeLogクラスが担当する。
生のPHPのerror_log()を使えば、apacheのerror_logにメッセージを出力することができる。
error_log("HELLO");
ページレイアウトが勝手に指定されている
以下のような/app/views/layout/default.thtmlを作成すると、そちらが使われるようになる。
<html>
<body>
<?php echo $content_for_layout ?>
</body>
</html>
ページレイアウトには、/cake/libs/view/templates/layout/default.thtmlが使われる。
/cake/libs/view/view.phpでこんな記述がある。
$layoutFileName = fileExistsInPath(LIB . 'view' . DS . 'templates' . DS . $type . $this->layout . '.thtml');
LIBSは、/cake/config/paths.phpで設定されているCAKE.'libs'.DSなので、/cake/libs/あたり。