Skip to content

Commit 82781e8

Browse files
committed
add new option to enhance first page link
1 parent f334d9e commit 82781e8

21 files changed

+112
-91
lines changed

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ chapter of the documentation.
2222

2323
## Requirements:
2424

25-
- Knp Pager component `>=2.0`.
25+
- Knp Pager component `>=4.4`.
2626
- KnpPaginatorBundle's master is compatible with Symfony `>=6.4` versions.
2727
- Twig `>=3.0` version is required if you use the Twig templating engine.
2828

@@ -71,19 +71,23 @@ public function registerBundles()
7171

7272
### Configuration example
7373

74-
You can configure default query parameter names and templates
74+
You can configure default query parameter names and templates, and a few other options:
7575

7676
#### YAML:
7777
```yaml
7878
knp_paginator:
79+
convert_exception: false # throw a 404 exception when an invalid page is requested
7980
page_range: 5 # number of links shown in the pagination menu (e.g: you have 10 pages, a page_range of 3, on the 5th page you'll see links to page 4, 5, 6)
81+
remove_first_page_param: false # remove the page query parameter from the first page link
8082
default_options:
8183
page_name: page # page query parameter name
8284
sort_field_name: sort # sort field query parameter name
8385
sort_direction_name: direction # sort direction query parameter name
8486
distinct: true # ensure distinct results, useful when ORM queries are using GROUP BY statements
8587
filter_field_name: filterField # filter field query parameter name
8688
filter_value_name: filterValue # filter value query parameter name
89+
page_out_of_range: ignore # ignore, fix, or throwException when the page is out of range
90+
default_limit: 10 # default number of items per page
8791
template:
8892
pagination: '@KnpPaginator/Pagination/sliding.html.twig' # sliding pagination controls template
8993
rel_links: '@KnpPaginator/Pagination/rel_links.html.twig' # <link rel=...> tags template
@@ -101,14 +105,18 @@ use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigura
101105
return static function (ContainerConfigurator $configurator): void
102106
{
103107
$configurator->extension('knp_paginator', [
108+
'convert_exception' => false, // throw a 404 exception when an invalid page is requested
104109
'page_range' => 5, // number of links shown in the pagination menu (e.g: you have 10 pages, a page_range of 3, on the 5th page you'll see links
110+
'remove_first_page_param' => false, // remove the page query parameter from the first page link
105111
'default_options' => [
106112
'page_name' => 'page', // page query parameter name
107113
'sort_field_name' => 'sort', // sort field query parameter name
108114
'sort_direction_name' => 'direction', // sort direction query parameter name
109115
'distinct' => true, // ensure distinct results, useful when ORM queries are using GROUP BY statements
110116
'filter_field_name' => 'filterField', // filter field query parameter name
111117
'filter_value_name' => 'filterValue' // filter value query parameter name
118+
'page_out_of_range' => 'ignore', // ignore, fix, or throwException when the page is out of range
119+
'default_limit' => 10 // default number of items per page
112120
],
113121
'template' => [
114122
'pagination' => '@KnpPaginator/Pagination/sliding.html.twig', // sliding pagination controls template

config/paginator.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@
5656

5757
<service id="Knp\Bundle\PaginatorBundle\Twig\Extension\PaginationRuntime">
5858
<argument type="service" id="knp_paginator.helper.processor" />
59+
<argument type="string">%knp_paginator.page_name%</argument>
60+
<argument>%knp_paginator.remove_first_page_param%</argument>
5961
<tag name="twig.runtime" />
6062
</service>
6163
</services>

src/DependencyInjection/Configuration.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ public function getConfigTreeBuilder(): TreeBuilder
5555
->booleanNode('convert_exception')
5656
->defaultFalse()
5757
->end()
58+
->booleanNode('remove_first_page_param')
59+
->defaultFalse()
60+
->end()
5861
->end()
5962
;
6063

src/DependencyInjection/KnpPaginatorExtension.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ public function load(array $configs, ContainerBuilder $container): void
4141
$container->setParameter('knp_paginator.template.sortable', $config['template']['sortable']);
4242
$container->setParameter('knp_paginator.page_range', $config['page_range']);
4343
$container->setParameter('knp_paginator.page_limit', $config['page_limit']);
44+
$container->setParameter('knp_paginator.page_name', $config['default_options']['page_name']);
45+
$container->setParameter('knp_paginator.remove_first_page_param', $config['remove_first_page_param']);
4446

4547
$paginatorDef = $container->getDefinition('knp_paginator');
4648
$paginatorDef->addMethodCall('setDefaultPaginatorOptions', [[

src/Helper/Processor.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,10 @@
1515
*/
1616
final class Processor
1717
{
18-
private UrlGeneratorInterface $router;
19-
20-
private TranslatorInterface $translator;
21-
22-
public function __construct(UrlGeneratorInterface $router, TranslatorInterface $translator)
23-
{
24-
$this->router = $router;
25-
$this->translator = $translator;
18+
public function __construct(
19+
private readonly UrlGeneratorInterface $router,
20+
private readonly TranslatorInterface $translator
21+
) {
2622
}
2723

2824
/**

src/Pagination/SlidingPagination.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@ final class SlidingPagination extends AbstractPagination implements SlidingPagin
1616
{
1717
private ?string $route = null;
1818

19-
/** @var array<string, mixed> */
20-
private array $params;
21-
2219
private int $pageRange = 5;
2320

2421
private ?int $pageLimit = null;
@@ -34,9 +31,8 @@ final class SlidingPagination extends AbstractPagination implements SlidingPagin
3431
/**
3532
* @param array<string, mixed> $params
3633
*/
37-
public function __construct(array $params)
34+
public function __construct(private array $params)
3835
{
39-
$this->params = $params;
4036
}
4137

4238
public function setUsedRoute(?string $route): void

src/Subscriber/SlidingPaginationSubscriber.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,11 @@ final class SlidingPaginationSubscriber implements EventSubscriberInterface
1515
/** @var array<string, mixed> */
1616
private array $params = [];
1717

18-
/** @var array<string, mixed> */
19-
private array $options;
20-
2118
/**
2219
* @param array<string, mixed> $options
2320
*/
24-
public function __construct(array $options)
21+
public function __construct(private readonly array $options)
2522
{
26-
$this->options = $options;
2723
}
2824

2925
public function onKernelRequest(RequestEvent $event): void

src/Templating/PaginationHelper.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,10 @@
1616
*/
1717
final class PaginationHelper extends Helper
1818
{
19-
protected PhpEngine $templating;
20-
21-
protected Processor $processor;
22-
23-
public function __construct(Processor $processor, PhpEngine $templating)
24-
{
25-
$this->processor = $processor;
26-
$this->templating = $templating;
19+
public function __construct(
20+
private readonly Processor $processor,
21+
private readonly PhpEngine $templating
22+
) {
2723
}
2824

2925
/**

src/Twig/Extension/PaginationExtension.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Knp\Bundle\PaginatorBundle\Twig\Extension;
44

55
use Twig\Extension\AbstractExtension;
6+
use Twig\TwigFilter;
67
use Twig\TwigFunction;
78

89
final class PaginationExtension extends AbstractExtension
@@ -16,6 +17,7 @@ public function getFunctions(): array
1617
new TwigFunction('knp_pagination_rel_links', [PaginationRuntime::class, 'rel_links'], $options),
1718
new TwigFunction('knp_pagination_sortable', [PaginationRuntime::class, 'sortable'], $options),
1819
new TwigFunction('knp_pagination_filter', [PaginationRuntime::class, 'filter'], $options),
20+
new TwigFunction('knp_pagination_query', [PaginationRuntime::class, 'getQueryParams']),
1921
];
2022
}
2123
}

src/Twig/Extension/PaginationRuntime.php

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99

1010
final class PaginationRuntime implements RuntimeExtensionInterface
1111
{
12-
private Processor $processor;
13-
14-
public function __construct(Processor $processor)
15-
{
16-
$this->processor = $processor;
12+
public function __construct(
13+
private readonly Processor $processor,
14+
private readonly string $pageName = 'page',
15+
private readonly bool $skipFirstPageLink = false,
16+
) {
1717
}
1818

1919
/**
@@ -107,4 +107,22 @@ public function filter(
107107
$this->processor->filter($pagination, $fields, $options ?? [], $params ?? [])
108108
);
109109
}
110+
111+
/**
112+
* @param array<string, mixed> $query
113+
* @param int $page
114+
* @return array<string, mixed>
115+
*/
116+
public function getQueryParams(array $query, int $page): array
117+
{
118+
if ($page === 1 && $this->skipFirstPageLink) {
119+
if (isset($query[$this->pageName])) {
120+
unset($query[$this->pageName]);
121+
}
122+
123+
return $query;
124+
}
125+
126+
return array_merge($query, [$this->pageName => $page]);
127+
}
110128
}

0 commit comments

Comments
 (0)