akkun_choi pedia
mindiaakkun_choi pedia見出し語

Zend_Controller

akkun_choi · 2008-12-07 更新

とりあえず動かす

>|php|
<?php
// main
$ctrl = Zend_Controller_Front::getInstance();
$ctrl->setControllerDirectory('app/controllers');
$ctrl->dispatch();

// app/controllers/IndexController.php
class IndexController extends Zend_Controller_Action{
public function indexAction(){
$this->view->assign('time', time());
}
}
// app/controllers/ErrorController.php
class ErrorController extends Zend_Controller_Action{
public function errorAction(){
}
}
// app/views/scripts/index/index.phtmlと
// app/views/scripts/error/error.phtmlも用意しておく

//
?>
||<

エラーを捕捉したい場合

>|php|
<?php
$ctrl->dispatch();
$exceptions = $ctrl->getResponse()->getException();
if ($exceptions){
var_dump(array_shift($exceptions));
}
?>
||<

テンプレートにassignして、出力する

>|php|
<?php
function hogeAction(){
$this->view->assign('time', time();
}
?>
// app/views/scripts/index/hoge.phtml
/*
<html>
<body>
now: <?php echo $this->view->time ?>
</body>
</html>
*/
||<

Zend_Controller_Action

初期化はinit()に書く
$this->viewにデフォルトのViewが入ってる。

Zend_Controller_Router

>|php|
// Requestの書き換え
$router = new Zend_Controller_Router_Rewrite();
$router->addConfig($config);
$this->request = $router->route($this->request);

// URLの構築
$url = $baseUrl . $this->router->assemble($params, $name, true);

||<

Regexでassembleする場合は逆向きの設定をしなければならない。コンストラクタの第4引数か、reverseオプションで与える

関連する見出し語