dongrouyuan5685 2011-07-24 16:21
浏览 43
已采纳

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

I'm creating a controller which will pull information of a painter from a database and display it: PainterController. I have defined it like so:

<?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
    }
}
?>

As you can see, a route is setup to accept anything like domain.com/painter/12 where in this example the id 12 is passed to the infoAction.

Yet, when I visit such URL I the route is not being recognized, instead I get:

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

Does anyone have a clue on why this would happen?

(FYI, I realize that the files are located in a public directory, this is due to a shared webhost constraint. The files are protected using .htaccess. This is unrelated to the question.)

Update: it appears that the above does work when defined in the bootstrap. However, I do not like to put a lot of logic in the bootstrap. Is it possible to define controller related routes inside the controller itself?

  • 写回答

1条回答 默认 最新

  • doujianzi8521 2011-07-24 18:49
    关注

    Is it possible to define controller related routes inside the controller itself?

    No. The router is responsible for finding which controller action to open. You have to add all your custom routes before the router is asked to route the request. This takes place in the front controller's dispatch method.

    Besides the bootstrap file, you can add your routes to the application.ini or other router configuration files.

    Also, you can add routes via a custom plugin. If your going to use a plugin. You must add to the routeStartup method because this comes immediately before the router routes the request.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因