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

笑着哭i 提交于 2019-11-27 08:37:42

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

Don't forget to set may_terminate to true!

Since version 2.1.4 you come across user error

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

Usage example:

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

This can be done using the current URL view helper 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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!