Hello,
I’m working with CakePHP 4 and facing an issue with Paginator links not respecting my custom route definitions. To give you a context, I’ve set up a custom route like so:
$builder->connect(
"/categories/{category}",
['controller' => 'Items', 'action' => 'category', 'items'],
['routeClass' => DashedRoute::class, 'pass' => ['items'],]
);
In the controller ItemsController, I have the category action defined as follows:
public function category($item_type_slug)
{
$category_slug = $this->getRequest()->getParam('category');
$this->loadComponent('Paginator');
$items = $this->getItemsCatalog($item_type_slug, $category_slug);
$this->set('items', $items['items'] ? $this->paginate($items['items']) : []);
}
I am using CakePHP’s built-in pagination for displaying items. The issue arises when accessing the second page of the items list. The Paginator generates a link that follows CakePHP’s default routing pattern (“/controller/action/param”) instead of using my custom route. Consequently, the link to the second page appears as "/items/category/items?page=2"
rather than following the custom route pattern "/categories/{category}?page=2"
.
I’ve searched through the documentation and forums but haven’t found a clear solution to make Paginator respect my custom route in generating links. How can I adjust the Paginator or the routing setup so that the pagination links follow the custom route I’ve defined?
Any advice or pointers in the right direction would be greatly appreciated. Thank you in advance for your help!
CakePHP Version
4
PHP Version
8