我正在创建一个控制器,它将从数据库中提取画家的信息并显示它: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 时,我的路由没有被识别,而是我得到:
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',
)
有谁知道为什么会发生这种情况?
(仅供参考,我意识到文件位于公共目录中,这是由于共享的虚拟主机约束。文件使用 .htaccess 保护。这与问题无关。)
更新:似乎以上在引导程序中定义时确实有效。但是,我不喜欢在引导程序中加入很多逻辑。是否可以在控制器本身内部定义与控制器相关的路由?