title: How to implement a pagination for a search module in Zend Framework 2?
body: <p>I have a module <code>Search</code> in my ZF2 application. The user fills in a search form out and gets a list of courses.</p>
  
  <p>Now I'm adding the pagination to the module. The paginator is basically working: I can retrieve data over it and the pagination is displayed correctly (pagelinks 1-7 for 70 found courses with the dafault setting 10 items per page).</p>
  
  <p>But it's still not usable. When I click on a pagelink, the form POST data is lost. I know -- it cannot work the way, how I implemented it (see the code below). But I have no idea, how to do it correctly, in order to eep checking the form data and nonetheless be able to use pagination.</p>
  
  <p>That is my code:</p>
  
  <p><strong>Table class Search\Model\CourseTable</strong></p>
  
  <pre><code>class CourseTable {
  
      ...
  
      // without pagination
      // public function findAllByCriteria(CourseSearchInput $input) {
      // with pagination
      public function findAllByCriteria(CourseSearchInput $input, $pageNumber) {
          ...
          $select = new Select();
          $where = new Where();
          $having = new Having();
          ...
          // without pagination
          // $resultSet = $this-&gt;tableGateway-&gt;selectWith($select);
          // return $resultSet;
          // with pagination
          $adapter = new \MyNamespqce\Paginator\Adapter\DbSelect($select, $this-&gt;tableGateway-&gt;getAdapter());
          $paginator = new \Zend\Paginator\Paginator($adapter);
          $paginator-&gt;setCurrentPageNumber($pageNumber);
          return $paginator;
      }
  
      ... 
  
  }
  </code></pre>
  
  <p><strong>Search\Controller\SearchController</strong></p>
  
  <pre><code>class SearchController extends AbstractActionController {
  
      public function searchCoursesAction() {
          $form = $this-&gt;getServiceLocator()-&gt;get('Search\Form\CourseSearchForm');
          $request = $this-&gt;getRequest();
          if ($request-&gt;isPost()) {
              $courseSearchInput = new CourseSearchInput();
              $form-&gt;setInputFilter($courseSearchInput-&gt;getInputFilter());
              $form-&gt;setData($request-&gt;getPost());
              if ($form-&gt;isValid()) {
                  $courseSearchInput-&gt;exchangeArray($form-&gt;getData());
                  // without pagination
                  // $courses = $this-&gt;getCourseTable()-&gt;findAllByCriteria($courseSearchInput);
                  // with pagination
                  $page = $this-&gt;params()-&gt;fromRoute('page');
                  $paginator = $this-&gt;getCourseTable()-&gt;findAllByCriteria($courseSearchInput, $page);
              } else {
                  $paginator = null;
              }
          } else {
              $paginator = null;
          }
          return new ViewModel(array(
              'form' =&gt; $form,
              // without pagination
              // 'courses' =&gt; $courses,
              // with pagination
              'paginator' =&gt; $paginator,
              'cities' =&gt; ...
          ));
      }
  
      ...
  
  }
  </code></pre>
  
  <p>How to get it working?</p>