Php/zend framework/プロジェクトを作成せずに使用する
提供: 初心者エンジニアの簡易メモ
プロジェクトを作成せずに使用する
階層を以下のように作成
+ public + application + controllers + views/scripts/
public/.htaccess
RewriteEngine on RewriteRule !\.(js|ico|gif|jpg|png|css)$ index.php
public/index.php
<?php require_once 'Zend/Controller/Front.php'; $front = Zend_Controller_Front::getInstance(); $front->setControllerDirectory(dirname(__FILE__) . '/../application/controllers'); $front->dispatch();
application/IndexController.php
<?php require_once 'Zend/Controller/Action.php'; class IndexController extends Zend_Controller_Action { public function indexAction() { $this->view->assign('test', "index"); } }
application/views/scripts/index/index.phtml
<?=$this->test ?>
public/error.php
class ErrorController extends Zend_Controller_Action { public function errorAction() { $errors = $this->_getParam('error_handler'); switch ($errors->type) { case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER: case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION: // 404 error -- controller or action not found $this->getResponse()->setHttpResponseCode(404); $this->view->message = 'Page not found'; break; default: // application error $this->getResponse()->setHttpResponseCode(500); $this->view->message = 'Application error'; break; } $this->view->exception = $errors->exception; $this->view->request = $errors->request; } }
application/views/scripts/error/error.phtml
<h1>An error occurred</h1> <h2><?php echo $this->message ?></h2> <?php if ('development' == APPLICATION_ENV): ?> <h3>Exception information:</h3> <p> <b>Message:</b> <?php echo $this->exception->getMessage() ?> </p> <h3>Stack trace:</h3> <pre><?php echo $this->exception->getTraceAsString() ?> </pre> <h3>Request Parameters:</h3> <pre><?php echo var_export($this->request->getParams(), true) ?> </pre> <?php endif ?>
Zendクラスautoloadする
require_once 'Zend/Loader/Autoloader.php'; $autoloader = Zend_Loader_Autoloader::getInstance(); $autoloader->setFallbackAutoloader(true);
エラーコントローラーカスタマイズ
エラー処理前に実行
$front = Zend_Controller_Front::getInstance(); $plugin=$front->getPlugin('Zend_Controller_Plugin_ErrorHandler'); $plugin->setErrorHandlerController('errorhoge'); $plugin->setErrorHandlerAction('errorhoge');