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

3

21

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

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?

Photomechanical answered 8/10, 2012 at 15:54 Comment(0)
J
10

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!

Jabberwocky answered 8/10, 2012 at 18:22 Comment(3)
Thanks! This does work, except when I use url($routeName, $params, true) to inherit parameters from a parent route it adds the default controller and action as query parameters - is there a way to avoid that?Photomechanical
interesting... does it also add the other existing params? didn't use that yet, but maybe there's some trick-ish worakound to unset controller and action params before assembling...Jabberwocky
query routes are deprecated framework.zend.com/manual/current/en/modules/…Punic
O
59

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);
Ornithischian answered 14/3, 2013 at 23:50 Comment(1)
Bless you! This was driving me insane! For anyone looking to redirect in your controllers, try: $this->redirect()->toRoute("index/article", array("article_id" => $articleId), array("query" => array("param" => "value"))); - hope this helps! :)Prue
B
11

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.

Band answered 22/5, 2015 at 16:10 Comment(0)
J
10

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!

Jabberwocky answered 8/10, 2012 at 18:22 Comment(3)
Thanks! This does work, except when I use url($routeName, $params, true) to inherit parameters from a parent route it adds the default controller and action as query parameters - is there a way to avoid that?Photomechanical
interesting... does it also add the other existing params? didn't use that yet, but maybe there's some trick-ish worakound to unset controller and action params before assembling...Jabberwocky
query routes are deprecated framework.zend.com/manual/current/en/modules/…Punic

© 2022 - 2024 — McMap. All rights reserved.