title: CakePHP 2 How to pass variables through the URL when using pagination?
body: <p>I have a project in CakePHP application and I want to add pagination to it. I have added pagination to it, but when I click on a page number or go to the next page my application breaks.</p>
  
  <p>The page relies on variables being passed to the controller in order to display the correct data. Here is my code for the view:</p>
  
  <pre><code>        &lt;table class="table table-striped"&gt;
              &lt;thead&gt;
                  &lt;tr&gt;
                      &lt;th&gt;Job ID&lt;/th&gt;
                      &lt;th&gt;Date&lt;/th&gt;
                      &lt;th&gt;Site&lt;/th&gt;
                      &lt;th&gt;&amp;nbsp;&lt;/th&gt;
                  &lt;/tr&gt;
              &lt;/thead&gt;
              &lt;tbody&gt;
                  &lt;?php foreach ($data as $jobsheet) { ?&gt;
                      &lt;tr&gt;
                          &lt;td&gt;
                              &lt;?php echo $jobsheet['Jobsheet']['jobnum']; ?&gt;
                          &lt;/td&gt;
                          &lt;td&gt;
                              &lt;?php echo date("l, jS F Y", strtotime($jobsheet['Jobsheet']['jobdate'])); ?&gt;
                          &lt;/td&gt;
                          &lt;td&gt;
                              &lt;span class="companyname"&gt;&lt;strong&gt;&lt;?php echo $jobsheet['Siteaddress']['sitename'] ?&gt;&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;
                              &lt;?php echo $jobsheet['Siteaddress']['addressline1']; ?&gt;, 
                              &lt;?php echo $jobsheet['Siteaddress']['addressline2']; ?&gt;, 
                              &lt;?php if ( $jobsheet['Siteaddress']['addressline3'] = '') { ?&gt;
                                  &lt;?php echo $jobsheet['Siteaddress']['addressline3']; ?&gt;, 
                              &lt;?php } else { ?&gt;
  
                              &lt;?php } ?&gt;
                              &lt;?php echo $jobsheet['Siteaddress']['city']; ?&gt;, 
                              &lt;?php echo $jobsheet['Siteaddress']['county']; ?&gt;, 
                              &lt;?php echo $jobsheet['Siteaddress']['country']; ?&gt;, 
                              &lt;?php echo $jobsheet['Siteaddress']['postcode']; ?&gt;
                          &lt;/td&gt;
                          &lt;td&gt;&lt;a data-toggle="modal" href="#viewjobsheet&lt;?php echo $jobsheet['Jobsheet']['id']; ?&gt;" onClick="document.getElementById('jbif&lt;?php echo $jobsheet['Jobsheet']['id']; ?&gt;').src='/modal/customers/&lt;?php echo $company['Company']['id']; ?&gt;/jobs/&lt;?php echo $jobsheet['Jobsheet']['id']; ?&gt;/view/';" title="View" class="icon-eye-open"&gt;&lt;/a&gt;
                          &lt;/td&gt;
                      &lt;/tr&gt;
                  &lt;?php } ?&gt;
              &lt;/tbody&gt;
          &lt;/table&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  
  &lt;div class="row"&gt;
      &lt;div class="controls"&gt;
          &lt;?php echo $this-&gt;Paginator-&gt;prev('« Previous', null, null, array('class' =&gt; 'disabled')); ?&gt; 
  
          &lt;?php echo $this-&gt;Paginator-&gt;numbers(); ?&gt;
  
          &lt;?php echo $this-&gt;Paginator-&gt;next('Next »', null, null, array('class' =&gt; 'disabled')); ?&gt;
      &lt;br /&gt;
      &lt;br /&gt;
          &lt;?php echo $this-&gt;Paginator-&gt;counter(array(
                  'format' =&gt; 'Page {:page} of {:pages}, showing {:current} records out of
                           {:count} total, starting on record {:start}, ending on {:end}'
              ));
          ?&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>This is the controller:</p>
  
  <pre><code>function index(){
  
      $companyid = $this-&gt;params['customer'];
      $this-&gt;set('companyid', $companyid);
      $siteid = $this-&gt;params['site'];
  
      $this-&gt;loadModel('Jobsheet');
  
      // Job Sheets
      $jobsheets = $this-&gt;Jobsheet-&gt;find('all', array('conditions' =&gt; array('Jobsheet.company' =&gt; $companyid), 'recursive' =&gt; 2, 'order' =&gt; 'Jobsheet.jobdate DESC', 'limit' =&gt; '10'));
      $this-&gt;set('jobsheets', $jobsheets);
  
      $this-&gt;paginate = array(
          'limit' =&gt; 10
      );
  
      $data = $this-&gt;paginate('Jobsheet');
      $this-&gt;set(compact('data'));    
  
  }
  </code></pre>
  
  <p>While the pagination does work, it ends up loosing the variable and this breaks what I'm trying to do. What am i missing?</p>