ZF2: using url helper and reuse query parameters

笑着哭i 提交于 2019-12-20 04:20:48

问题


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.


回答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




回答2:


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



来源:https://stackoverflow.com/questions/13637051/zf2-using-url-helper-and-reuse-query-parameters

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