Skip to content

Retaining and Restoring query strings plugin for CakePHP

License

Notifications You must be signed in to change notification settings

elstc/cakephp-restore-query

Repository files navigation

Retaining and Restoring query strings plugin for CakePHP

Software License Build Status Codecov Latest Stable Version

This plugin provides a component that makes it possible to restore the conditions in the list, search page, etc. even after transitioning to another page.

Requirements

  • CakePHP 3.x

Installation

You can install this plugin into your CakePHP application using composer.

The recommended way to install composer packages is:

composer require elstc/cakephp-restore-query

Add the following line to your application config/bootstrap.php:

use Cake\Core\Plugin;
Plugin::load('Elastic/RestoreQuery');

Usage

Retaining query string

Load the component with your controller's initialize method.

class AppController extends Controller
{
    public function initialize()
    {
        $this->loadComponent('Elastic/RestoreQuery.RestoreQuery', [
            'actions' => ['index', 'search'], // List of actions to record query string
        ]);
    }
}

The component automatically saves the Query string for the target action.

Restore saved query string

By creating a link in the template as follows, the component will call the saved query string and redirect to the target page.

    <?=
    $this->Html->link('link text', [
        'action' => 'index',
        '?' => ['_restore' => true], // NOTE: _restore=true, the component will restore the saved query.
    ]);
    ?>

Elastic/RestoreQuery.RestoreQueryComponent Options

actions

List of actions to record the query string.

default: ['index', 'search']

sessionKey

Name of session key for query string saving

default: 'StoredQuerystring'

restoreKey

Name of the query string for restore action.

default: '_restore'

redirect

Redirect when restoring the query string.

default: true