1

I'm trying to reuse query params using Url helper in a view. This is my current url:

http://localhost/events/index?__orderby=name&__order=asc

I'm using this code in the view:

$this->url('events/index', array('__page' => '2'), true);

I want to obtain this url:

http://localhost/events/index?__orderby=name&__order=asc&__page=2

But instead i get this:

http://localhost/events/index?controller=Application\Controller\Events&__page=2

This is my route inside module.config.php file:

'events' => array(
    'type' => 'segment',
    'options' => array(
        'route' => '/eventos[/:action]',
        'constraints' => array(
            'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
        ),
        'defaults' => array(
            'controller' => 'Application\Controller\Events',
            'action' => 'index',
        ),
    ),
    'may_terminate' => true,
    'child_routes' => array(
        'index' => array(
            'type' => 'Query',
        ),
    ),
),

What am i doing wrong? Thanks for your help.

prueba prueba
  • 652
  • 1
  • 8
  • 26
  • try `$this->url('events', array('action'=>'index', '__page' => '2'), true);` – Sam Nov 30 '12 at 04:46
  • It doesn't work. It produces the next Url: `http://localhost/events` – prueba prueba Dec 02 '12 at 03:41
  • Ah i just noticed, it's the child route. Maybe `$this->url('events/index', array('__page' => 2))` may work - never really worked with query routes. But the child-routes name, which you want to access, is 'events/index' and not just 'events' – Sam Dec 02 '12 at 15:55
  • Nope, I'm using `$this->url('eventos/index', array('__page' => '2'))` and still doesn't not work. – prueba prueba Dec 05 '12 at 17:19
  • It should be `'events/index'` not `'eventos/index'` you assign the route by their `NAME`. The names you assign are `events` and `events/index`. If this still doesn't work then the Query-Route simply works differently. It might then just work like `$this->url('events/index').'?__page=2` :S – Sam Dec 06 '12 at 06:58

2 Answers2

1

I think what you are looking for is a Query route type to capture the Query string for you as a child route:

'route' => array(
    'type'    => 'literal',
    'options' => array(
        'route'    => 'page',
        'defaults' => array(
        ),
    ),
    'may_terminate' => true,
    'child_routes'  => array(
        'query' => array(
            'type' => 'Query',
            'options' => array(
                'defaults' => array(
                    'foo' => 'bar'
                )
            )
        ),
    ),

You can then use the view helper to generate and append the query string for you. If you don't use a Query child route, then the helper will just ignore your query string.

$this->url(
    'page/query',
    array(
        'name'=>'my-test-page',
        'format' => 'rss',
        'limit' => 10,
    )
);

You can then set the third parameter to TRUE and allow the helper to use the current parameters as you are trying to do in your example.

There's examples in the docs:

http://framework.zend.com/manual/2.0/en/modules/zend.mvc.routing.html

Andrew
  • 12,617
  • 1
  • 34
  • 48
0

You could use something like this, but the query parameters arent reused in my case.

$this->url(
    'page/query',
    array(),
    array(
        'query' => array(
            'name'=>'my-test-page',
            'format' => 'rss',
            'limit' => 10,
        )
    ),
    true
);

So if you want to reuse query parameters you coul merge them with your new ones and then add all of them to the query array (3 parameter).

Stillmatic1985
  • 1,792
  • 6
  • 20
  • 39