Skip to content

Latest commit

 

History

History
91 lines (62 loc) · 2.73 KB

README.md

File metadata and controls

91 lines (62 loc) · 2.73 KB

CakePHP SearchPagination Plugin

This plugin allows you to keep track of the search parameters through the pagination links on query strings.

You can combined this plugin with Search plugin; SearchPaginationComponent can be an alternative to PrgComponent.

NOTICE

THIS PLUGIN WAS COMPLETELY REWRITTEN FOR CAKEPHP-1.3 ON 2010-07-27. PLEASE CHECKOUT 1.2 BRANCH FOR OLDER VERSIONS.

Requirements

  • PHP 5.1 or higher
  • CakePHP 1.3

Installation

cd plugins/
git clone http://github.com/tkyk/cakephp-search-pagination search_pagination

Usage

Controllers

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

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

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

SearchPaginationComponent provides setup method, which retrieves search parameters from params['url'], stores them into data attribute, 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.

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).