Zend自定义路由未被控制器识别

Tom*_*Tom 2 php routing zend-framework zend-route

我正在创建一个控制器,它将从数据库中提取画家的信息并显示出来:PainterController.我这样定义它:

<?php
class PainterController extends Zend_Controller_Action
{

    public function init()
    {
        //setup painter route
        $router = $this->getFrontController()->getRouter();
        $router->addRoute(
            'painter',
            new Zend_Controller_Router_Route(
                'painter/:id',
                array(
                    'controller' => 'painter',
                    'action' => 'info'
                )
            )
        );
    }

    public function indexAction()
    {
        //index
    }

    public function infoAction()
    {
        //info was requested for given ID
    }
}
?>
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,路由设置为接受类似于domain.com/painter/12此示例中的id 12传递给infoAction的任何内容.

然而,当我访问这样的URL时,路由未被识别,而是我得到:

Message: Action "2" does not exist and was not trapped in __call()

Stack trace:

#0 /home/httpd/vhosts/xxx.com/subdomains/peter/httpdocs/library/Zend/Controller/Action.php(515): Zend_Controller_Action->__call('2Action', Array)
#1 /home/httpd/vhosts/xxx.com/subdomains/peter/httpdocs/library/Zend/Controller/Dispatcher/Standard.php(295): Zend_Controller_Action->dispatch('2Action')
#2 /home/httpd/vhosts/xxx.com/subdomains/peter/httpdocs/library/Zend/Controller/Front.php(954): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http))
#3 /home/httpd/vhosts/xxx.com/subdomains/peter/httpdocs/library/Zend/Application/Bootstrap/Bootstrap.php(97): Zend_Controller_Front->dispatch()
#4 /home/httpd/vhosts/xxx.com/subdomains/peter/httpdocs/library/Zend/Application.php(366): Zend_Application_Bootstrap_Bootstrap->run()
#5 /home/httpd/vhosts/xxx.com/subdomains/peter/httpdocs/public/index.php(25): Zend_Application->run()
#6 {main}  
Request Parameters:

array (
      'controller' => 'painter',
  'action' => '2',
  'module' => 'default',
)  
Run Code Online (Sandbox Code Playgroud)

有没有人知道为什么会发生这种情况?

(仅供参考,我意识到文件位于公共目录中,这是由于共享的webhost约束.文件使用.htaccess保护.这与问题无关.)

更新:似乎在引导程序中定义时上述操作正常.但是,我不喜欢在bootstrap中加入很多逻辑.是否可以在控制器内部定义与控制器相关的路径?

bra*_*ano 5

是否可以在控制器内部定义与控制器相关的路径?

不.路由器负责查找要打开的控制器操作.在要求路由器路由请求之前,您必须添加所有自定义路由.这发生在前端控制器的dispatch方法中.

除了引导程序文件,您还可以将路由添加到application.ini其他路由器配置文件中.

此外,您可以通过自定义插件添加路由.如果你打算使用插件.您必须添加到routeStartup方法,因为这是在路由器路由请求之前发生的.