Front Controller †
Zend_Controller_Request_Http(リクエストオブジェクト) †呼び出す際は Zend_Controller_Action::getRequestメソッドを呼び出す。 リクエストデータを取得するメソッド †
※スーパーグローバル変数などを使いたい場合はリクエストオブジェクト経由でアクセスする リクエスト時に使用されたHTTPメソッドを取得/判定する †
isXmlHttpRequest?()はX-Requested-Withヘッダの値がXMLHttpRequest?かどうか判定する->ヘッダを付与しないJavaScriptライブラリもあるので要注意。 コンテンツ追加/削除する †
コンテンツをいくつかのセグメイントとして分けて管理している。
getResponse後、実際に出力するのはsendResponse(<-Front Controllerはdispatch後に自動的にコールするので明示的に呼び出す必要はない) 応答ヘッダを操作する †
※header/headers_sendなどは使わない Action Controller †Front Controllerから処理を委譲されてリクエストに応じた処理を行うクラス require_once 'Zend/Controller/Action.php';
class IndexController extends Zend_Controller_Action {
public function indexAction(){
$this->view->result = 'Hello World!';
}
}
命名規則 †
Zend_Controller_Action †
処理順序 †
Error Controller †
エラーページを出力する #ErrorController.php
require_once 'Zend/Controller/Action.php';
require_once 'Zend/Controller/Plugin/ErrorHandler.php'; #ErrorHandler plugin をインポート
require_once 'Zend/View.php';
class ErrorController extends Zend_Controller_Action {
public function errorAction(){
$errs = $this->_getParam('error_handler');
switch($errors->type){
case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER: #コントローラが存在しない場合
case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION: #アクションが存在しない場合
$this->view->msg = '存在しないコントローラ/アクションです';
break;
default:
$this->view->msg = $errs->exception->getMessage();
break;
}
}
public function throwAction(){ #例外を処理する
throw new Exception('例外が発生!');
}
}
error.phtml <html> <head> <title>Error page</title> </head> <body> <?php print($this->escape($this->msg)); ?> </body> </html> 発生した例外情報は以下のようにして取得(error_handler object) Zend_Controller_Action::_getParam('error_handler')
error_handler objectのtypeプロパティ
Redirect †
_redirectメソッドがリダイレクトを行う Zend_Controller_Action::_redirect($url, $option)~ オプション
コントローラ・アクションが存在しない場合にディフォルトコントローラにリダイレクトする #ErrorController.php
public function errorAction(){
$type = $this->_getParam('error_handler')->type;
switch($type){
case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
$this->_redirect('index/index);
$this->view->msg = '存在しないコントローラ/アクションです。';
break;
~略~
}
}
Forward †
_forwardだと内部処理になるため、アドレスバーには間違ったままのものが使われる #FowardController.php
<?php
require_once 'Zend/Controller/Action.php';
class ForwardController extends Zend_Controller_Action {
public function indexAction(){ #forwad/indexアクションの定義
$req = $this->getRequest();
$req->setParam('greeting', 'こんにちは');
$this->_forwad('result');
}
public function index2Action(){
$req = $this->getRequest():
$req->setParam('greeting', 'こんばんは');
$this->_forwad('result');
}
public function resultAction(){
$req = $this->getRequest();
$this->view->result = $req->getUserParam('greeting').', '.$req->getQuery('lang').'!';
}
}
#result.phtml <html> <head> <title>forward</title> </head> <body> <?php print($this->escape($this->result)); ?> </body> </html> http://www.example.com/zend/controller/forwad/index2?lang=hoge などでアクセスする Zend_Controller_Action::_forward($action, $ctrl, $module, $params);
Module †モジュール単位で分割して管理するとよい
モジュールを使用する場合、モジュール名と対応するモジュールフォルダのパスをフロントコントローラに設定する必要がある setControllerDirectory? method †モジュール名=>コントローラフォルダの連想配列として指定 $front - Zend_Controller_Front::getInstance(); $front->setControllerDirectory( array( 'default'=> APP.'/modules/default/controllers', 'usr'=> APP.'/modules/usr/controllers', 'sendmail' => APP.'/modules/sendmail/controllers' ) ); addControllerDirectory? method †すでにコントローラフォルダが指定されている所に追加でコントローラフォルダを追加できる $front = Zend_Controller_Front::getInstance(); $front->setControllerDirectory(APP.'/modules/default/controllers'); $front->addControllerDirectory(APP.'/modules/usr/controllers', 'usr'); $front->addControllerDirectory(APP.'/modules/sendmail/controllers', 'sendmail'); 第一引数にコントローラフォルダを、第二引数にモジュール名を入れることが出来る adModuleDirectory? method †モジュールフォルダ命とモジュール名が同じな場合はaddModuleDirectory?メソッドを使うことも出来る $front->addModuleDirectory(APP.'/modules'); これでmodules以下に配置されているmodules(usrやsendmailなど)はフォルダ名と同じモジュール名として登録される アクションコントローラの命名規則 †
View Controller †view 変数 $this->view->変数名 = 値;~ Zend_Viewによって書かれたView Scriptは拡張子が.phtmlになる <html> <head> <title>View</title> </head> <body> <?php print($this->escape($this->result)); ?> </body> </html> ※View変数経由で動的に出力する場合は、エスケープ処理を行う application/views/scripts/コントローラー名/アクション名.phtml 実行URI †http://www.example.com/Zend/Controller/ctrl/action/param1/value1/param2/value2... http://www.example.com/Zend/Controller/コントローラー名/アクション名/パラメータ名/値/ http://www.example.com/Zend/Controller/index/index ->index controllerのindex action http://www.example.com/Zend/Controller/index/post ->index controllerのpost action Zend_Db †共通
基本:
O/R マッピング
その他
PHP application -> Zend_Db ->Pdo_Mysql adapter -> MySQL ->Pdo_Pgsql adapter -> PostgreSQL ->Pdo_Oci adapter -> Oracle ->その他 -> other.. Zend_Db対応しているデータベース †
<?php
require_once 'Zend_Db.php';
try {
$db = Zend_Db::factory(
'Pdo_Mysql',
array(
'host' => 'localhost',
'username'=>'hoge',
'password'=>'hogepw'
'dbname'=>'zend'
)
); #この時点ではまだ接続は確定されていない
$db->getConnection();
print('データベース接続に成功しました');
} catch (Zend_Exception $e) {
die($e->getMessage());
}
$db->closeConnection(); #通常はスクリプト終了時に切断処理が行われるので書く必要はない
}
DB接続に関しての注意事項
データベース接続情報を外部ファイルとして保持する †#hogehoge.ini [database] adapter = Pdo_Mysql params.host = localhost params.dbname = book params.username = hoger params.password = hogepw #connect_ini.php <?php require_once 'Zend/Config/Ini.php'; require_once 'Zend/Db.php'; try{
$config = new Zend_Config_Ini('../hogehoge.ini', 'database');
$db = Zend_Db::factory($config);
$db->getConnect();
print('データベース接続に成功しました');
} catch (Zend_Exception $e ) {
die($e->getMessage());
}
$db->closeConnection();
?>
|