views:

52

answers:

1

Is it possible to use paginator with $_GET parameters?

For example i have a route like this:

$router->addRoute('ajax_gallery',
new Routes_Categories(
'/:lang/:category/age/:dep/:cat/:towns',
array(
"page" => 1,
"dep" => 0,
"cat" => 0,
"towns" => 0
),
array(
"dep" => "[0-9]+",
"cat" => "[0-9]+"
)

));

And i'm making request like this via ajax:

http://localhost/en/gallery?dep=9&cat=27&towns=1

But links that returned from results are without ?dep=9&cat=27&towns=1

How to force zend paginator to use passed $_GET params inside pagination link generation?

So that returned links were:

http://localhost/en/gallery/2?dep=9&cat=27&towns=1
http://localhost/en/gallery/3?dep=9&cat=27&towns=1
http://localhost/en/gallery/4?dep=9&cat=27&towns=1

etc...

or even

http://localhost/en/gallery/2/9/27/1
http://localhost/en/gallery/3/9/27/1
http://localhost/en/gallery/4/9/27/1

like they are defined inside route etc...

Thanks

+1  A: 

The view URL helper will always output params as part of the URL (separated by forward slashes) and doesn't, to my knowledge, support the GET parameter format.

I don't know what Routes_Categories class does, but working from the default ZF route classes try this:

$route = new Zend_Controller_Router_Route(
    '/:lang/:category/:age/:dep/:cat/:towns/*',
    array(
        "dep" => 0,
        "cat" => 0,
        "towns" => 0
    ),
    array(
        "dep" => "[0-9]+",
        "cat" => "[0-9]+"
    )
);
$router->addRoute('ajax_gallery', $route);

The * supports any additional named params after your route. The above assumes lang, category and age are required, and dep, cat and towns are optional. Bear in mind if you want to set cat you have to set dep otherwise the route will get confused which variable is what.

In your controller access the page param via the following, which sets a default of 1.

$page = $this->_getParam('page', 1);

Access the URL via AJAX as: http://localhost/en/gallery/2/9/27/1

If you want the page param, use a named parameter: http://localhost/en/gallery/2/9/27/1/page/2

To get this route to work in your pagination you need to update your paginator view controls to use the right route. See: http://framework.zend.com/manual/en/zend.paginator.usage.html#zend.paginator.usage.rendering.example-controls

Look for the code where the URL is outputted and add the route name to the URL view helper. So replace code like this:

<?php echo $this->url(array('page' => $this->previous)); ?>

With:

<?php echo $this->url(array('page' => $this->previous), 'ajax_gallery'); ?>
simonrjones