### Install PagerfantaBundle with Composer
Source: https://github.com/babdev/pagerfantabundle/blob/4.x/docs/installation.md
Use this Composer command to add the PagerfantaBundle to your project dependencies. This installs the core Pagerfanta package; additional adapters may be required.
```bash
composer require babdev/pagerfanta-bundle
```
--------------------------------
### Registering Views with XML Configuration
Source: https://github.com/babdev/pagerfantabundle/blob/4.x/docs/adding-views.md
Configure views using XML in your Symfony application. This example demonstrates registering 'DefaultView' and 'SemanticUiView' with their aliases.
```xml
```
--------------------------------
### Controller Setup for Pagerfanta
Source: https://github.com/babdev/pagerfantabundle/blob/4.x/docs/rendering-pagerfantas.md
This PHP code demonstrates how to create a Pagerfanta instance with a Doctrine ORM adapter in a Symfony controller and pass it to a Twig template.
```php
createBlogListQueryBuilder();
$pagerfanta = new Pagerfanta(
new QueryAdapter($queryBuilder)
);
return $this->render(
'blog/list.html.twig',
[
'pager' => $pagerfanta,
],
);
}
}
```
--------------------------------
### Include Default View CSS
Source: https://github.com/babdev/pagerfantabundle/blob/4.x/docs/views.md
Include the basic CSS for the default Pagerfanta view using the asset() function. This is useful for quick setup.
```twig
```
--------------------------------
### Registering Views with YAML Configuration
Source: https://github.com/babdev/pagerfantabundle/blob/4.x/docs/adding-views.md
Configure views using YAML in your Symfony application. This example shows how to register 'DefaultView' and 'SemanticUiView' with their respective aliases.
```yaml
services:
# Use in Twig by calling {{ pagerfanta(pager, 'default') }}
pagerfanta.view.default:
class: Pagerfanta\View\DefaultView
tags:
- { name: pagerfanta.view, alias: default }
# Use in Twig by calling {{ pagerfanta(pager, 'pagerfanta.view.semantic_ui') }}
pagerfanta.view.semantic_ui:
class: Pagerfanta\View\SemanticUiView
tags:
- { name: pagerfanta.view }
```
--------------------------------
### Registering Views with PHP Configuration
Source: https://github.com/babdev/pagerfantabundle/blob/4.x/docs/adding-views.md
Configure views using PHP in your Symfony application. This example shows how to register 'DefaultView' and 'SemanticUiView' with their respective aliases.
```php
services()
// Use in Twig by calling {{ pagerfanta(pager, 'default') }}
->set('pagerfanta.view.default', DefaultView::class)
->tag('pagerfanta.view', ['alias' => 'default'])
// Use in Twig by calling {{ pagerfanta(pager, 'pagerfanta.view.semantic_ui') }}
->set('pagerfanta.view.semantic_ui', SemanticUiView::class)
->tag('pagerfanta.view')
;
};
```
--------------------------------
### Pagerfanta JSON Serialization Example
Source: https://github.com/babdev/pagerfantabundle/blob/4.x/docs/serializer.md
Illustrates the typical JSON structure produced when serializing a Pagerfanta instance. The 'items' array content depends on your serializer configuration.
```json
{
"items": [
{
"id": 1
},
{
"id": 2
},
{
"id": 3
}
],
"pagination": {
"current_page": 1,
"has_previous_page": false,
"has_next_page": true,
"per_page": 10,
"total_items": 35,
"total_pages": 4
}
}
```
--------------------------------
### Build JSON Response in Controller
Source: https://github.com/babdev/pagerfantabundle/blob/4.x/docs/serializer.md
Example of creating a JSON response for a paginated list of blog posts using Symfony Serializer. Ensure the Pagerfanta instance is injected or created within the controller.
```php
createBlogListQueryBuilder();
$pagerfanta = new Pagerfanta(new QueryAdapter($queryBuilder));
return $this->json($pagerfanta);
}
}
```
--------------------------------
### Set Default Twig Template
Source: https://github.com/babdev/pagerfantabundle/blob/4.x/docs/configuring-the-bundle.md
Specify the default Twig template for Twig views by configuring the `default_twig_template` option. This example shows how to use a custom template.
```yaml
babdev_pagerfanta:
default_view: twig
default_twig_template: '@App/Pagerfanta/default.html.twig'
```
--------------------------------
### Configure Exception Strategies
Source: https://github.com/babdev/pagerfantabundle/blob/4.x/docs/configuring-the-bundle.md
Customize how Pagerfanta exceptions are handled. Set `out_of_range_page` to 'custom' to disable 404 responses for invalid max per page exceptions, and `not_valid_current_page` to 'to_http_not_found' to keep the default 404 behavior for invalid current page exceptions.
```yaml
babdev_pagerfanta:
exceptions_strategy:
out_of_range_page: custom # Disables converting `Pagerfanta\Exception\NotValidMaxPerPageException` to a 404 response
not_valid_current_page: to_http_not_found # Default behavior converting `Pagerfanta\Exception\NotValidCurrentPageException` to a 404 response
```
--------------------------------
### Set Default View
Source: https://github.com/babdev/pagerfantabundle/blob/4.x/docs/configuring-the-bundle.md
Configure the default view for your application by setting the `default_view` node in the Pagerfanta configuration file.
```yaml
babdev_pagerfanta:
default_view: my_view
```
--------------------------------
### Injecting and Using ViewFactory
Source: https://github.com/babdev/pagerfantabundle/blob/4.x/docs/retrieving-views.md
Inject the `ViewFactoryInterface` and `RouteGeneratorFactoryInterface` into a service to render Pagerfanta views programmatically. This approach is suitable when you need to control pagination rendering outside of a templating engine.
```php
viewFactory->get($view)->render($pagerfanta, $this->routeGeneratorFactory->create($options), $options);
}
}
```
--------------------------------
### Default Pagerfanta Configuration
Source: https://github.com/babdev/pagerfantabundle/blob/4.x/docs/default-configuration.md
Configure the default Pagerfanta view and Twig template. Set the exception strategy for out-of-range or invalid current pages.
```yaml
babdev_pagerfanta:
# The default Pagerfanta view to use in your application
default_view: default
# The default Twig template to use when using the Twig Pagerfanta view
default_twig_template: '@BabDevPagerfanta/default.html.twig'
exceptions_strategy:
# The exception strategy if requesting a page outside the available pages in a paginated list; valid options are "custom" or "to_http_not_found"
out_of_range_page: to_http_not_found
# The exception strategy if the current page is not an allowed value in a paginated list; valid options are "custom" or "to_http_not_found"
not_valid_current_page: to_http_not_found
```
--------------------------------
### Omit First Page Link (Shorter Syntax)
Source: https://github.com/babdev/pagerfantabundle/blob/4.x/docs/rendering-pagerfantas.md
This is a shorter syntax for omitting the first page link by relying on the default template.
```twig
{{ pagerfanta(pager, {'omitFirstPage': true }) }}
```
--------------------------------
### Register PagerfantaBundle in Symfony
Source: https://github.com/babdev/pagerfantabundle/blob/4.x/docs/installation.md
If Symfony Flex does not automatically register the bundle, add this entry to your `config/bundles.php` file to enable it.
```php
['all' => true],
];
```
--------------------------------
### Define Reusable View with Low Proximity and Spanish Messages
Source: https://github.com/babdev/pagerfantabundle/blob/4.x/docs/reusing-options.md
This snippet defines a reusable Pagerfanta view named 'low_proximity_and_spanish_messages'. It uses the OptionableView to decorate the default view, applying a proximity of 2 and custom Spanish messages for 'previous' and 'next' navigation. This configuration can be used in Twig templates by calling `{{ pagerfanta(pager, 'low_proximity_and_spanish_messages') }}`.
```yaml
services:
# Use in Twig by calling {{ pagerfanta(pager, 'low_proximity_and_spanish_messages') }}
pagerfanta.view.low_proximity_and_spanish_messages:
class: Pagerfanta\View\OptionableView
arguments:
- @pagerfanta.view.default
- { proximity: 2, prev_message: Anterior, next_message: Siguiente }
tags:
- { name: pagerfanta.view, alias: low_proximity_and_spanish_messages }
```
--------------------------------
### Basic Pagerfanta Rendering in Twig
Source: https://github.com/babdev/pagerfantabundle/blob/4.x/docs/rendering-pagerfantas.md
Use this Twig code to render the default Pagerfanta pagination controls. It automatically generates routes for the current page using the 'page' variable.
```twig
{{ pagerfanta(pager) }}
```
--------------------------------
### Define Reusable View with High Proximity
Source: https://github.com/babdev/pagerfantabundle/blob/4.x/docs/reusing-options.md
This snippet defines a reusable Pagerfanta view named 'high_proximity'. It utilizes the OptionableView to configure the default view with a proximity of 5. This reusable view can be invoked in Twig templates using `{{ pagerfanta(pager, 'high_proximity') }}`.
```yaml
services:
# Use in Twig by calling {{ pagerfanta(pager, 'high_proximity') }}
pagerfanta.view.high_proximity:
class: Pagerfanta\View\OptionableView
arguments:
- @pagerfanta.view.default
- { proximity: 5 }
tags:
- { name: pagerfanta.view, alias: high_proximity }
```
--------------------------------
### Custom Page Parameter Name
Source: https://github.com/babdev/pagerfantabundle/blob/4.x/docs/rendering-pagerfantas.md
If your routing uses a different parameter name for pagination, specify it using the `pageParameter` option. The parameter name must be wrapped in brackets.
```twig
{{ pagerfanta(pager, 'default', {'pageParameter': '[other_page]'}) }}
```
--------------------------------
### Omit First Page Link
Source: https://github.com/babdev/pagerfantabundle/blob/4.x/docs/rendering-pagerfantas.md
To prevent the URL from showing '?page=1', set the `omitFirstPage` option to `true` when calling the `pagerfanta` Twig function.
```twig
{{ pagerfanta(pager, 'default', {'omitFirstPage': true}) }}
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.