CakePHPメモ 
top

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 -{text $this->log(‘logging test’); ~~~ log()メソッドは、Objectクラスで定義されている。第2引数を指定する事もできるが、この場合はLOG_ERRORとLOG_DEBUGのいずれかが使える。実際の出力は、CakeLogクラスが担当する。

生のPHPのerror_log()を使えば、apacheのerror_logにメッセージを出力することができる。

error_log("HELLO");

-:html # ページレイアウトが勝手に指定されている 以下のような/app/views/layout/default.thtmlを作成すると、そちらが使われるようになる。 -{text



ページレイアウトには、/cake/libs/view/templates/layout/default.thtmlが使われる。

/cake/libs/view/view.phpでこんな記述がある。
-{text
$layoutFileName = fileExistsInPath(LIB . 'view' . DS . 'templates' . DS . $type . $this->layout . '.thtml');

LIBSは、/cake/config/paths.phpで設定されているCAKE.’libs’.DSなので、/cake/libs/あたり。

imported