如何在ZF2/ZF3 URL视图帮助器中添加查询参数 [英] How can you add query parameters in the ZF2 / ZF3 url view helper

查看:122
本文介绍了如何在ZF2/ZF3 URL视图帮助器中添加查询参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

但是,这似乎不起作用(第二个助手实际上输出/users).根据该非正式的,过时的文档,一种通过在路由名称后添加/query来实现此目的的方法,但这会给出一个路由未找到的异常.

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.

可以使用当前的网址帮助程序完成此操作吗?

Can this be done using the current url helper?

推荐答案

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

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',
        ),
    ),
),

然后您可以组装$this->url('users/query', array('sort' => 'desc')).

别忘了将may_terminate设置为true

这篇关于如何在ZF2/ZF3 URL视图帮助器中添加查询参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆