ZF2 paginator with query string

748 views Asked by At

I'm newby in PHP and ZF2. I have a paginator, which I need to keep the sorting, search and filters from query string like this:

http://localhost:90/admin/analyses/list?page=1&filter_analyses_categories=1&orderBy=analyses_name&orderDirection=DESC

I read about routing, but I'm not understand how to use in my situation. In module.config.php I have:

return array(
'router' => array(
    'routes' => array(
        'home' => array(
            'type' => 'Zend\Mvc\Router\Http\Literal',
            'options' => array(
                'route'    => '/',
                'defaults' => array(
                    'controller' => 'Admin\Controller',
                    'action'     => 'index',
                ),
            ),
        ), //end home
        'admin' => array(
            'type'    => 'Literal',
            'options' => array(
                'route'    => '/admin',
                'defaults' => array(
                    '__NAMESPACE__' => 'Admin\Controller',
                    'controller'    => 'Index',
                    'action'        => 'index',
                ),
            ),
            'may_terminate' => true,
            'child_routes' => array(
                'default' => array(
                    'type'    => 'Segment',
                    'options' => array(
                        'route'    => '/[:controller[/:action][/page/:page]]',
                        'constraints' => array(
                            'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                            'action'     => '[a-zA-Z][a-zA-Z0-9_-]*',
                        ),
                        'defaults' => array(
                        ),
                    ),
                ),
            ),
        ), //end admin
      'paginator' => array(
        'type' => 'segment',
        'options' => array(
            'route' => '',
            'defaults' => array(
                'page' => 1,
            ),
         'may_terminate' => true,
         'child_routes'  => array(
                'query' => array(
                        'type' => 'Query',
                ),
            ),
        ),
      ),//end paginator
    ),//end routes
),
);

The paginator partial view look like below:

<?php foreach ($this->pagesInRange as $page): ?>
             <?php if ($page != $this->current): ?>
                 <li>
                     <a href="<?php echo $this->url($this->route);?>?page=<?php echo $page; ?>">
                         <?php echo $page; ?>
                     </a>
                 </li>
             <?php else: ?>
                 <li class="active">
                     <a href="#"><?php echo $page; ?></a>
                 </li>
             <?php endif; ?>
         <?php endforeach; ?>

The link created for each page is like this:

http://localhost:90/admin/analyses/list?page=3

without the others query string parmaeters. How can achieve the right url for each page keeping the others query string parameters?

1

There are 1 answers

2
nofreeusername On

Do not pass your page parameter as query string. use your defined route parameter instead:

$this->url('admin/default', ['page' => $page], true);

The third parameter keeps your already matching route parameters