我正在创建一个 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 时,我的路线无法被识别,而是得到:
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',
)
有人知道为什么会发生这种情况吗?
(仅供引用,我意识到这些文件位于公共(public)目录中,这是由于共享网络主机的限制。这些文件使用 .htaccess 进行保护。这与问题无关。)
更新:看来上面的内容在 Bootstrap 中定义时确实有效。但是,我不喜欢在 Bootstrap 中放入大量逻辑。是否可以在 Controller 本身内部定义 Controller 相关的路由?
最佳答案
Is it possible to define controller related routes inside the controller itself?
没有。路由器负责查找要打开的 Controller 操作。在要求路由器路由请求之前,您必须添加所有自定义路由。这发生在前端 Controller 的 dispatch
方法中。
除了引导文件之外,您还可以将路由添加到 application.ini
或其他 router configuration文件。
此外,您还可以通过自定义插件添加路线。如果您要使用插件。您必须添加到 routeStartup
方法,因为该方法会在路由器路由请求之前立即发生。
关于php - Controller 无法识别 Zend 自定义路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6808035/