David Albrecht
David Albrecht

Reputation: 674

How to use route with specified parameters in pagination

I have two routes: one general route and one very specific. They have controller and action in common, but the special route has an additional parameter special:

$routes->connect('/special_products', ['controller' => 'Products', 'action' => 'index', 'special' => 1]);
$routes->connect('/products', ['controller' => 'Products', 'action' => 'index']);

This works as intended except for the pagination. If I am on the the /special_products page and click on page 2 I go to /products?page=2 instead of the expected /special_products?page=2.

How could I change the routes or the pagination, so that the special parameter is not lost?

Upvotes: 0

Views: 82

Answers (2)

Inigo Flores
Inigo Flores

Reputation: 4469

Instead of writing your own control logic, I recommend that you use a search plugin. The two most popular are:

Once you've configured it properly, you can use Query String to pass parameters to your action.

Your route would then look something like

$routes->connect('/special_products', [
    'controller' => 'Products', 
    'action' => 'index',
    '?' => ['special' => 1]
]);

Upvotes: 1

Jacek B Budzynski
Jacek B Budzynski

Reputation: 1413

Try adding $this->Paginator->options(['url' => $paginateUrl]) now you can pass params between pages

Here you have a simple example

index.ctp

<?= $this->Paginator->options(['url' => $paginateUrl]) ?>
<?= $this->Paginator->prev('< ' . __('previous')) ?>
<?= $this->Paginator->numbers() ?>
<?= $this->Paginator->next(__('next') . ' >') ?>

ProductsController

public function index()
{
    ...
    if($this->request->param('special')) {

        $paginateUrl = ['special' => 1];

    }
    $this->set('paginateUrl', isset($paginateUrl) ? $paginateUrl : []);
    ...
}

Upvotes: 0

Related Questions