将Zend控制器与数字操作一起使用


Using Zend Controllers with numeric Actions

通常,如果我想创建页面/user/profile,我会在用户控制器中创建函数profileAction()。现在,如果我想创建一个名为/error/404的页面,我无法创建404Action(),因为合成错误unexpected '404', expecting 'identifier'有没有一种方法可以用这样的数字创建页面?

一种方法是如下定义自定义路线:

resources.routes.error.route = "/error/:id"
resources.routes.error.type = "Zend_Controller_Router_Route" 
resources.routes.error.defaults.module = default
resources.routes.error.defaults.controller = error
resources.routes.error.defaults.action = index
resources.routes.error.reqs.id = "'d+"

在您的indexAction中,您会转发一个针对特定错误代码的操作。例如,对于404错误:

class ErrorController extends Zend_Controller_Action {

    public function indexAction() {
        $errorId = $this->_getParam('id', null);            
        return $this->_forward("error$errorId" );
    }
    public function error404Action() {
        echo "error 404"; 
    }
}

这是一个非常简单的例子,但它应该足以说明如何做到这一点。

另一种方法,而不是转发,将只是呈现适当的视图脚本,例如

class ErrorController extends Zend_Controller_Action {

    public function indexAction() {
        $errorId = $this->_getParam('id', null);           
        // e.g. redner error/error404.phtml 
        $this->_helper->viewRenderer('error$errorId');
    }
}