如何在 Zend Framework 1.x 中执行路由并重定向自定义错误消息?
问问题
2511 次
1 回答
2
要执行自定义路线:
将以下内容放入您的bootstrap.php
文件中
function _initRoutes() {
$front_controller = Zend_Controller_Front::getInstance();
$router = $front_controller->getRouter();
$router->addRoute('customName', new Zend_Controller_Router_Route(
'/customName/:dataVariable', array('controller' => 'YourController', 'action' => 'YourAction')
));
}
然后,在您的控制器操作中,您将使用刚刚设置的数据变量访问它。
例如:对于类别 - 帖子显示
function _initRoutes() {
$front_controller = Zend_Controller_Front::getInstance();
$router = $front_controller->getRouter();
$router->addRoute('category', new Zend_Controller_Router_Route(
'/post/:postId', array('controller' => 'category', 'action' => 'post')
));
}
然后在类别控制器和发布操作中,我将使用$this->getRequest()->getParam('postId');
它将把网址设置为:example.com/post/123
ZF1为你自带了一个预定义的ErrorController,你可以使用它。
class ErrorController extends Zend_Controller_Action {
public function errorAction() {
$errors = $this->_getParam('error_handler');
if (!$errors || !$errors instanceof ArrayObject) {
$this->view->message = 'You have reached the error page';
return;
}
switch ($errors->type) {
case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE:
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);
$priority = Zend_Log::NOTICE;
$this->view->error_code = $this->getResponse()->getHttpResponseCode();
$this->view->message = "Page Not Found";
$this->renderScript('error/error_404.phtml');
break;
default:
// application error
print_r($this->getResponse());
$this->getResponse()->setHttpResponseCode(500);
$priority = Zend_Log::CRIT;
$this->view->error_code = $this->getResponse()->getHttpResponseCode();
$this->view->message = 'Application error';
if ($log = $this->getLog()) {
$log->log($this->view->message, $priority, $errors->exception);
$log->log('Request Parameters', $priority, $errors->request->getParams());
$this->renderScript('error/error_500.phtml');
}
// conditionally display exceptions
if ($this->getInvokeArg('displayExceptions') == true) {
$this->view->exception = $errors->exception;
}
$this->view->request = $errors->request;
$this->view->error_code = $this->getResponse()->getHttpResponseCode();
$this->renderScript('error/error_500.phtml');
break;
}
// Log exception, if logger available
if ($log = $this->getLog()) {
$log->log($this->view->message, $priority, $errors->exception);
$log->log('Request Parameters', $priority, $errors->request->getParams());
}
// conditionally display exceptions
if ($this->getInvokeArg('displayExceptions') == true) {
$this->view->exception = $errors->exception;
}
$this->view->request = $errors->request;
}
public function getLog() {
$bootstrap = $this->getInvokeArg('bootstrap');
if (!$bootstrap->hasResource('Log')) {
return false;
}
$log = $bootstrap->getResource('Log');
return $log;
}
}
您可以从列表中设置HTTP_STATUS_CODES,但请确保为用户的浏览器和搜索机器人提供正确的状态。
于 2012-11-17T04:46:40.410 回答