控制器无法识别自定义路由


Zend custom route not being recognized for controller

我正在创建一个控制器,它将从数据库中提取画家的信息并显示它: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
    }
}
?>

正如你所看到的,路由被设置为接受任何类似domain.com/painter/12的东西,在这个例子中,id 12被传递给infoAction。

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

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',
)  

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

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

更新:在引导程序中定义时,上面的操作似乎是有效的。但是,我不喜欢在bootstrap中放入很多逻辑。是否可以在控制器本身内部定义控制器相关的路由?

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

。路由器负责查找打开哪个控制器动作。在路由器被要求路由请求之前,你必须添加所有自定义路由。这发生在前端控制器的dispatch方法中。

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

也可以通过自定义插件添加路由。如果你要使用插件。您必须添加routeStartup方法,因为它在路由器路由请求之前立即出现。