Php 如何在ZF2中的分页URL中保留查询参数?

Php 如何在ZF2中的分页URL中保留查询参数?,php,zend-framework2,zend-paginator,Php,Zend Framework2,Zend Paginator,我正在使用Zend\Paginator构建分页结果集。这很好,但是,在添加搜索表单之后,我无法让这两个表单很好地结合在一起 页面上搜索表单生成的URL为: user/index/?searchTerm=hello 如何修改Zend paginator配置,使其在生成的URL中保留searchTerm 我希望有这样的事情: user/index/page/4/?searchTerm=hello 我错过了什么 模块配置路由定义如下: 'user' => array( 'type'

我正在使用Zend\Paginator构建分页结果集。这很好,但是,在添加搜索表单之后,我无法让这两个表单很好地结合在一起

页面上搜索表单生成的URL为:

user/index/?searchTerm=hello
如何修改Zend paginator配置,使其在生成的URL中保留searchTerm

我希望有这样的事情:

user/index/page/4/?searchTerm=hello
我错过了什么


模块配置路由定义如下:

'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',
            ),
        ),
    ),
),
<a href="<?php echo $this->url($this->route, array('page' => $this->next), true); ?>">
分页是在视图中使用以下内容构建的:

echo $this->paginationControl(
        $this->users, 'sliding', array('paginator', 'User'), array('route' => 'user', 'action' => 'index')
    );
分页模板代码段:

    <li>
        <a href="<?php echo $this->url($this->route, array('action' => $this->action, 'page' => $this->next), true); ?>">
            Next &raquo;
        </a>
    </li>

  • (我的印象是将
    true
    作为参数传递。)

    我现在看到
    url()
    的第三个参数在做什么。我可以简化分页链接并删除“操作”键,如下所示:

    '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',
                ),
            ),
        ),
    ),
    
    <a href="<?php echo $this->url($this->route, array('page' => $this->next), true); ?>">
    
    然后,
    搜索
    将保留在分页链接中

    如果我修改搜索表单以通过JavaScript提交,我可以构建搜索URL并引导用户访问它

    该方法的简单jQuery示例:

    $(".search-form").submit(function() {
        var baseUrl = $(this).attr('action'),
            search = $(this).find('.search').val();
    
         window.location = baseUrl + '/search/' + search;
    
        return false;
    });
    
    另一个选项是,如果控制器接收到searchTerm查询,则重定向到控制器中的
    当前/route/search/term
    路由

    我将此作为一个答案发布,但我对更好的解决方案持开放态度