I have a controller action in my project based on Symfony 6.3:
#[Route('/test')]
class TestController extends AbstractController
{
#[Route('/events/page/{page<\d+>?1}/itemsPerPage/{itemsPerPage<\d+>?10}', name: 'get_events')]
public function getEvents(PaginatorInterface $paginator, int $page = 1, int $itemsPerPage = 10)
{
// ...
}
}
This action uses KnpPaginatorBundle to paginate found events, that's why I need page and itemsPerPage parameters.
Everything works fine, when I call my action this way:
http://127.0.0.1:8080/test/events/page/1/itemsPerPage/7
What I want is to make /page/ and /itemsPerPage/ URL parts dynamic too. What I mean:
- if I omit
itemsPerPageparameter (http://127.0.0.1:8080/test/events/page/1), route should stay valid and use defaultitemsPerPagevalue (10). Notice, that there is not only7is absent in URL, but the whole part -/itemsPerPage/7(dynamic7and "static"/itemsPerPage/); - if I omit
pageparameter (http://127.0.0.1:8080/test/events/itemsPerPage/7), route should stay valid and use defaultpagevalue (1). Notice, that there is not only1is absent in URL, but the whole part -/page/1(dynamic1and "static"/page/); http://127.0.0.1:8080/test/events/page/1/itemsPerPage/7should still stay valid (passing both parameters).
I realize, that it could be easier to refuse "static" parts usage and do this:
#[Route('/events/{page<\d+>?1}/{itemsPerPage<\d+>?10}', name: 'get_events')]
But I plan to add much more GET parameters in future, so I wouldn't like my URL to finally look like http://127.0.0.1:8080/test/events/1/7/100/8648/78978/... (unless this is the only possible solution).
Also I could try to do something like this to cover all possible cases:
#[Route('/events/page/{page<\d+>?1}/itemsPerPage/{itemsPerPage<\d+>?10}')]
#[Route('/events/itemsPerPage/{itemsPerPage<\d+>?10}/page/{page<\d+>?1}')]
#[Route('/events/itemsPerPage/{itemsPerPage<\d+>?10}')]
#[Route('/events/page/{page<\d+>?1}')]
But as I said above, additional GET parameter will occur later, so such combinations isn't an option because of a huge number of possible cases.
Does Symfony provide a possibility to handle such situation?
At the Route I would only add variables that the page cannot work without. For example if a page lists user events, then I would add user_id at the Route. Everything else you can get from
knp_paginator configuration yamlorgetorsessionand handle them in the controller.In your current case I would use
$_GETto storepageanditemsPerPagein session variables and then access them from the session, validate them and then pass them like this:This way you can have a clean url and the flexibility to add more parameters in the future.