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 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探