如何在ZF2 / ZF3 url视图助手中添加查询参数

时间:2021-02-15 23:55:44

I'm attempting to create a url with a query string using a route, like so:

我正在尝试使用路由创建带有查询字符串的URL,如下所示:

$this->url('users') -> /users
$this->url('users', ['sort' => 'desc']) -> /users?sort=desc

However this doesn't seem to work (the second helper actually outputs /users). According to this unofficial, out-of-date documentation there was once a way to do this by appending /query to the route name, however this gives a route-not-found exception.

然而,这似乎不起作用(第二个助手实际输出/用户)。根据这个非官方的,过时的文档,曾经有一种方法可以通过将/ query附加到路由名称来实现这一点,但是这会给出一个route-not-found异常。

Can this be done using the current url helper?

可以使用当前的url帮助程序完成吗?

3 个解决方案

#1


11  

You can create a child route for your users route like this:

您可以为用户路由创建子路由,如下所示:

'users' => array(
    'type' => 'Literal',
    'options' => array(
        'route' => '/users',
        'defaults' => array(
            '__NAMESPACE__' => 'User\Controller',
            'controller' => 'Index',
            'action' => 'list',
        ),
    ),
    'may_terminate' => true,
    'child_routes'  => array(
        'query' => array(
            'type' => 'Query',
        ),
    ),
),

then you can assemble $this->url('users/query', array('sort' => 'desc')).

然后你可以汇编$ this-> url('users / query',array('sort'=>'desc'))。

Don't forget to set may_terminate to true!

别忘了将may_terminate设置为true!

#2


57  

Since version 2.1.4 you come across user error

自2.1.4版以来,您遇到了用户错误

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

自ZF 2.1.4起不推荐使用查询路由;使用HTTP路由器的汇编方法的“查询”选项代替

Usage example:

用法示例:

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

#3


9  

This can be done using the current URL view helper yes.

这可以使用当前URL视图帮助程序yes来完成。

$this->url('users', [], array('query' => array('sort' => 'desc')))

You do not need to have query string child routes setup. As long as you have a route setup for 'users', you can just look for the 'sort' param in your controller and use where required.

您不需要设置查询字符串子路由。只要您为“用户”设置了路径,就可以在控制器中查找“排序”参数,并在需要时使用。

#1


11  

You can create a child route for your users route like this:

您可以为用户路由创建子路由,如下所示:

'users' => array(
    'type' => 'Literal',
    'options' => array(
        'route' => '/users',
        'defaults' => array(
            '__NAMESPACE__' => 'User\Controller',
            'controller' => 'Index',
            'action' => 'list',
        ),
    ),
    'may_terminate' => true,
    'child_routes'  => array(
        'query' => array(
            'type' => 'Query',
        ),
    ),
),

then you can assemble $this->url('users/query', array('sort' => 'desc')).

然后你可以汇编$ this-> url('users / query',array('sort'=>'desc'))。

Don't forget to set may_terminate to true!

别忘了将may_terminate设置为true!

#2


57  

Since version 2.1.4 you come across user error

自2.1.4版以来,您遇到了用户错误

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

自ZF 2.1.4起不推荐使用查询路由;使用HTTP路由器的汇编方法的“查询”选项代替

Usage example:

用法示例:

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

#3


9  

This can be done using the current URL view helper yes.

这可以使用当前URL视图帮助程序yes来完成。

$this->url('users', [], array('query' => array('sort' => 'desc')))

You do not need to have query string child routes setup. As long as you have a route setup for 'users', you can just look for the 'sort' param in your controller and use where required.

您不需要设置查询字符串子路由。只要您为“用户”设置了路径,就可以在控制器中查找“排序”参数,并在需要时使用。