在ZF2中向URL添加参数

时间:2014-05-15 12:38:53

标签: zend-framework2 zend-route


我正在尝试构建这样的url:

 abc.com/folder?user_id=1&category=v

遵循此链接中的建议:

How can you add query parameters in the ZF2 url view helper

最初,它会引发此错误

 Query route deprecated as of ZF 2.1.4; use the "query" option of the HTTP router\'s assembling method instead

根据建议,我使用类似于

  $name    = 'index/article';
  $params  = ['article_id' => $articleId];
  $options = [
    'query' => ['param' => 'value'], 
  ];
  $this->url($name, $params, $options);

现在,我收到语法错误,

   Parse error: syntax error, unexpected '[' in /var/www/test/module/Dashboard/view/dashboard/dashboard/product.phtml on line 3

我的module.config.php配置如下:

   'browse_test_case' => array(
                        'type' => 'Literal',
                        'options' => array(
                            'route' => '/browse-case[/:productId]',
                            'defaults' => array(
                                'controller' => 'Test\Controller\Browse',
                                'action'     => 'browse-test-case',
                            ),
                        ),
                        'may_terminate' => true,
                        'child_routes'  => array(
                            'query' => array(
                                'type' => 'Query',
                            ),
                        ),
                        ),

任何想法,请帮助!

1 个答案:

答案 0 :(得分:0)

您的网址视图助手调用错误。第一个参数是已定义路径的名称,第二个参数是包含路径参数的数组。您的数组语法也应该是正确的。例如     数组(" param1" =>" value1"," param2" =>" value2")

在您的示例中,正确的url视图助手调用应该是这样的:     $ this-> url(' browse_test_case',数组(' productId' =>' 1'));

......其中" 1"例如,可以是数据库表行标识符。

路径定义中的查询子路由允许您使用除路径中指定的其他URL参数。但是这些子路线中的每一条都以" / browse-case [/:productId]"开头。

在那里你找到了ZF2的参考例子: http://framework.zend.com/manual/2.3/en/modules/zend.view.helpers.url.html#zend-view-helpers-initial-url