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 <h2><?php echo $this->message ?>
<?php if ('development' == APPLICATION_ENV): ?>
<h3>Exception information:
<p>
<b>Message: <?php echo $this->exception->getMessage() ?>
<h3>Stack trace:
<pre><?php echo $this->exception->getTraceAsString() ?>
<h3>Request Parameters:
<pre><?php echo var_export($this->request->getParams(), true) ?>
<?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');