Appending search parameters to patination links.
PHP
Switch branches/tags
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
Controller/Component
Test/Case
View/Helper
LICENSE
README.md

README.md

CakePHP SearchPagination Plugin

This plugin allows you to append search parameters to pagination links with querystring.

You can combine this plugin with Search plugin. SearchPaginationComponent can be used as an alternative to PrgComponent.

Requirements

  • CakePHP 2.0
  • PHP 5.2.8 or higher

Installation

cd app/Plugin
git clone http://github.com/tkyk/cakephp-search-pagination SearchPagination

I recommend you to checkout a versioning tag rather than a development branch.

cd app/Plugin/SearchPagination
git checkout x.y.z.w

Usage

Controllers

All you have to do is to call SearchPagination->setup method in your actions.

class UsersController extends AppController {
  public $components = array('SearchPagination.SearchPagination');

  public function search() {
    $this->SearchPagination->setup();
    //...
  }
}

SearchPaginationComponent provides setup method, which retrieves search parameters from request->query, stores them into request->data, and makes PaginatorHelper succeed the parameters.

setup(string $modelName, array $defaultParams);

Both arguments are optional; If $modelName is omitted, controller's modelClass is used. If $defaultParams is omitted, an empty array() is stored in the data when no search paramters are passed.

public function search() {
  $this->SearchPagination->setup('User');

  // $this->data[Model] always contains the search parameters.
  // For example, if you are using Search plugin:
  $this->paginate['conditions'] = $this->User->parseCriteria($this->data['User']);
  $this->set('users', $this->paginate('User'));

  $this->set('groups', $this->User->Group->find('list'));
}

Views

There is nothing special in views. You can use FormHelper and PaginatorHelper as usual. All pagination links generated by the PaginationHelper will have the search parameters on their query strings.

echo "<h2>Search Form<h2>";
echo $this->Form->create('User', array('type' => 'get'));
echo $this->Form->input('username');
echo $this->Form->input('created');
echo $this->Form->input('group_id');
echo $this->Form->end('Search');

echo $this->Paginator->numbers(array('modulus' => 10));

foreach($users as $user) {
  echo $user['User']['id'];
  //...
}

You may notice that the type parameter is set to 'get' in FormHelper->create. It is recommended but not necessary; If omitted, SearchPaginationComponent will automatically generate the GET url and redirect to it (PRG pattern).