ZF2: Using Helper and Reuse URL Parameters

I am trying to reuse query parameters using the Url helper in the view. This is my current url:

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

I use this code in a view:

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

I want to get this URL:

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

But instead, I get the following:

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

This is my route inside the 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? Thank you for your help.

+1
source share
2 answers

I think you are looking for the type of query route to grab 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 create and add a query string for you. If you do not use the Query child route, the helper will simply ignore your query string.

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

Then you can set the third parameter to TRUE and allow the assistant to use the current parameters that you are trying to make in your example.

Examples in the docs:

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

+1
source

You can use something like this, but the arent query parameters are 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 the request parameters, you connect them to your new ones, and then add all of them to the request array (parameter 3).

0
source

Source: https://habr.com/ru/post/950236/


All Articles