使用URL视图助手生成URL,之后传递params?

时间:2011-05-10 09:43:45

标签: php zend-framework

是否有简单的方法如何使用view url helper以自定义params(控制器和动作除外)将在“?”之后的格式生成url ?

有例子:

<?php echo $this->url(array('controller' => "index", 'action' => "index", 'myParam' => "myValue")); ?>

<?php echo $this->url(array('controller' => "index", 'action' => "index", 'myParam' => "myValue")); ?>

This will generate:

domain.com/index/index/myParam/myValue

I want

domain.com?myParam=myValue or

1 个答案:

答案 0 :(得分:1)

您可以使用Regex路线的反向字段,例如:

   $myroute = new Zend_Controller_Router_Route_Regex('\?myParam=(.*)',
                    array(1 => 'defaultValue', 'controller' => 'index', 'action' => 'index', 'module' => 'index'),
                    array(1 => 'myValue'),
                    '?myParam=%s'
    );

   $wwwDomainRoute = new Zend_Controller_Router_Route_Hostname(
                    'www.domain.com');

   $plainPathRoute = new Zend_Controller_Router_Route_Static('');

   //this default route is useful for the default routing
   $router->addRoute('wwwroute', $wwwDomainRoute->chain($plainPathRoute));
   $router->addRoute('myroute', $wwwDomainRoute->chain($myroute));

并在您的视图中使用:

   <?php echo $this->url(array('controller' => "index", 'action' => "index", 'myParam' => "myValue"),'myroute'); ?>

注意:小心你的正则表达式......