3

I've implemented an action + view that lists all the rows in a database and allows you to paginate through them using zend_paginator. I've also written an action + view that takes a form post (or get), constructs a Solr query and returns a zend_paginator adapter to page through the results.

The problem I've got is working out how to paginate through my query after the postback, it's a relatively complex search form (8 fields to search on).

The options as far as I can see are:

  • Move the parameters that are posted to a new url and redirect
  • Write my own paginator that preserve query string parameters
  • Serialize the results of the form post into a session and get it from there when paginating

Which of these is the most zend/php way of doing it - and, more importantly, quickest to implement? HELP!!!!! Thanks.

Andrew Hancox
  • 2,330
  • 1
  • 20
  • 28
  • 1
    It's a good question. I have used the first approach: accept the params in post, then redirect to a route populated with the params in which I perform the search, pagination, etc. But I confess that I have never really been happy with it (two HTTP requests for the first search). Never liked the session approach b/c I want these search result pages to be (permanently) url-addressable. Interested to see what answers we get. – David Weinraub Jun 10 '11 at 09:03
  • @David Is there a reason to accept the params in post in the first place? Strikes me that a search is quite a "GETty" operation... – Matt Gibson Jun 10 '11 at 12:34
  • I'm happy that the form action should be a get but this doesn't sit well with the paginator control for two reasons. Firstly the paginator control out the box ignores drops the query string from the page links it generates, secondly the paginator control adds elements to store the page number to the url path, not the query string, which suggests I should put my search params in the url. – Andrew Hancox Jun 10 '11 at 12:51
  • 1
    @Matt: I agree that search should be a GET to make the results url-addressable. But I seem to recall that I had a some problem reconstructing the url, making the querystring params appear in the order I wanted, working together with Zend_Paginator and the paginate partial. I'm sure there was a better way to do it, but I ended up hacking it in with one route for the empty form, another route for results. Don't tell anyone. ;-) – David Weinraub Jun 10 '11 at 13:42
  • I usually prefer the session way, as you don't loose user entries even after some url manipulation from the user or after some long & complex pages navigations. And you can store parameters in several session namespace, where some search parameters can maybe be reused on some complelty different search queires (pre-filling some search filters, trying to guess what the user is searching). You could see the url SEO parameters as an addon and add some tasks to retrieve session stored criterias and put them on the url, on a second step. – regilero Jun 11 '11 at 12:17
  • @David Your secret's safe with me :) – Matt Gibson Jun 13 '11 at 08:42

1 Answers1

2

I'm not going to accept this answer until I'm convince no one has a better one but I've come to a decision. I've gone for the PRG pattern (post-redirect-get).

    if ($request->isPost()) {
        if ($form->isValid($request->getPost())) {
            return $this->_helper->redirector('index','contact',NULL,array_filter($form->getValues()));
        }
    }

This redirects to a url with the contents of the form as parameters - www.url.com/form/param1/val/param2/val

The array_filter command strips all empty parameters and this means you end up with a nice clean, bookmarkable, seo friendly url that the zend_paginator works with out the box.

Andrew Hancox
  • 2,330
  • 1
  • 20
  • 28