I am using Zend\Paginator to construct a paginated result set. This works fine, however, after adding a search form, I cannot get the two to play nicely together.
我正在使用Zend\Paginator来构建一个分页结果集。这很好,但是,在添加了一个搜索表单之后,我无法让这两个函数很好地结合在一起。
The URL produced by the search form on the page is:
搜索表单在页面上生成的URL为:
user/index/?searchTerm=hello
How do I amend the Zend paginator configuration so that it retains the searchTerm in the URLs produced?
如何修改Zend paginator配置,使其在生成的url中保留searchTerm ?
I was hoping for something like:
我希望的是:
user/index/page/4/?searchTerm=hello
What am I missing?
我缺少什么?
The module config route is defined as follows:
模块配置路径定义如下:
'user' => array(
'type' => 'Zend\Mvc\Router\Http\Segment',
'options' => array(
'route' => '/user[/[:action[/]]][[id/:id]][/[page/:page]]',
'defaults' => array(
'controller' => 'Application\Controller\User',
'action' => 'index',
'id' => null,
),
// the below was added to try and get the searchTerm query to be retained
'may_terminate' => true,
'child_routes' => array(
'searchTerm' => array(
'type' => 'Query',
),
),
),
),
The pagination is constructed using this in the view:
在视图中,分页是通过以下方式构建的:
echo $this->paginationControl(
$this->users, 'sliding', array('paginator', 'User'), array('route' => 'user', 'action' => 'index')
);
Pagination template snippet:
分页模板片段:
<li>
<a href="<?php echo $this->url($this->route, array('action' => $this->action, 'page' => $this->next), true); ?>">
Next »
</a>
</li>
(I was under the impression that passing true
as the third parameter to url()
would retain the query params.)
(我的印象是,将true作为第三个参数传递给url()将保留查询参数。)
1 个解决方案
#1
2
I now see what that third parameter to url()
is doing. I can simplify the pagination links and remove the 'action' key as follows:
我现在看到了url()的第三个参数在做什么。我可以简化分页链接并删除“action”键,如下所示:
<a href="<?php echo $this->url($this->route, array('page' => $this->next), true); ?>">
The page's action was matched as part of the URL (due to that third param being true) which is why that works. By the same token I can change the route to this:
页面的操作被作为URL的一部分进行匹配(由于第三个参数为true),这就是为什么它是有效的。出于同样的原因,我可以将路径改为:
'route' => '/user[/[:action[/]]][[id/:id]][/[page/:page]][/[search/:search]]',
And then the search
will be retained in the pagination links.
然后搜索将保留在分页链接中。
If I amend the search form to submit via JavaScript, I can construct the search URL and direct the user to it.
如果我将搜索表单修改为通过JavaScript提交,我可以构造搜索URL并将用户指向它。
Simple jQuery example for that approach:
该方法的简单jQuery示例:
$(".search-form").submit(function() {
var baseUrl = $(this).attr('action'),
search = $(this).find('.search').val();
window.location = baseUrl + '/search/' + search;
return false;
});
Another option would be to redirect to the current/route/search/term
route in the controller if it receives a searchTerm query.
另一个选项是,如果控制器接收到一个searchTerm查询,则将其重定向到当前/route/search/term路由。
I'm posting this as an answer but I am open to better solutions.
我将此作为一个答案发布,但我对更好的解决方案持开放态度。
#1
2
I now see what that third parameter to url()
is doing. I can simplify the pagination links and remove the 'action' key as follows:
我现在看到了url()的第三个参数在做什么。我可以简化分页链接并删除“action”键,如下所示:
<a href="<?php echo $this->url($this->route, array('page' => $this->next), true); ?>">
The page's action was matched as part of the URL (due to that third param being true) which is why that works. By the same token I can change the route to this:
页面的操作被作为URL的一部分进行匹配(由于第三个参数为true),这就是为什么它是有效的。出于同样的原因,我可以将路径改为:
'route' => '/user[/[:action[/]]][[id/:id]][/[page/:page]][/[search/:search]]',
And then the search
will be retained in the pagination links.
然后搜索将保留在分页链接中。
If I amend the search form to submit via JavaScript, I can construct the search URL and direct the user to it.
如果我将搜索表单修改为通过JavaScript提交,我可以构造搜索URL并将用户指向它。
Simple jQuery example for that approach:
该方法的简单jQuery示例:
$(".search-form").submit(function() {
var baseUrl = $(this).attr('action'),
search = $(this).find('.search').val();
window.location = baseUrl + '/search/' + search;
return false;
});
Another option would be to redirect to the current/route/search/term
route in the controller if it receives a searchTerm query.
另一个选项是,如果控制器接收到一个searchTerm查询,则将其重定向到当前/route/search/term路由。
I'm posting this as an answer but I am open to better solutions.
我将此作为一个答案发布,但我对更好的解决方案持开放态度。