douzui6173 2014-03-30 05:53
浏览 30
已采纳

如何在ZF1路由中使用多个参数

i was trying to add new routes to my Zend Framework 1 application. But it seems to understand my 2 params as one. How can i fix that?

$route = new Zend_Controller_Router_Route(':name-:type', array('controller' => 'tour', 'action' => 'index'));
$route = $routeLang->chain($route);
$router->addRoute('tour', $route);
  • 写回答

1条回答 默认 最新

  • dprfe04886 2014-03-31 06:07
    关注

    Let me rephrase your question again so others can get it quickly:

    How to pass 'name' and 'type' parameters all together connected by dash ('-'), so if going to url: 'tour/index/sampleName-sampleType', Zend can recognize name is 'sampleName', type is 'sampleType'?

    You need: Zend_Controller_Router_Route_Regex()

    If you want url to match: 'www.example.com/sampleName-sampleType'

    protected function _initRoutes ()
    {
        $this->bootstrap('frontController');
        $front = $this->getResource('frontController');
        $router = $front->getRouter();
    
        $route = new Zend_Controller_Router_Route_Regex('(.+)-(.+)', array('controller' => 'tour', 'action' => 'index'), array(1 => 'name', 2=>'type'), '%s-%s');
        $router->addRoute('tour', $route);
    }
    

    Or if you want to match: 'www.example.com/sampleName-tour-sampleType'

    protected function _initRoutes ()
    {
        $this->bootstrap('frontController');
        $front = $this->getResource('frontController');
        $router = $front->getRouter();
    
        $route = new Zend_Controller_Router_Route_Regex('(.+)-tour-(.+)', array('controller' => 'tour', 'action' => 'index'), array(1 => 'name', 2=>'type'), '%s-tour-%s');
        $router->addRoute('tour', $route);
    }
    

    Print it out in controller 'tour', action 'index':

    echo '<br>Name is: ' . $this->getParam('name') . '<br>Type is: ' . $this->getParam('type');
    

    Then go to url: '/tour/sampleName-sampleType', you will see the result:

    Name is: sampleName

    Type is: sampleType

    A little sugar:

    Actually your route is correctly set, however by default the routing pattern is: '/tour/index/name/sampleName/type/sampleType', your dash ('-') only worked as a separator for parameters, as in Zend does not recognize it as a connection string.

    The following code is the complete code to add such a route to your Zend project for those who might need it.

    In Bootstrap.php

    protected function _initRoutes ()
    {    
        $this->bootstrap('frontController');
        $front = $this->getResource('frontController');
        $router = $front->getRouter();
    
        $route = new Zend_Controller_Router_Route(':name/:type', array('controller' => 'tour', 'action' => 'index'));
        $router->addRoute('tour', $route);
    }
    

    Print it out in controller:

    echo '<br>Name is: ' . $this->getParam('name') . '<br>Type is: ' . $this->getParam('type');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler