Reputation: 479
I installed knp paginator in symfony 5, it works fine. But when I try to setup the bootstrap template in config/package/paginator.yaml it does not work.
I found https://github.com/KnpLabs/KnpPaginatorBundle/issues/468 I should have to use controller instead of abstractController. Also I found https://ourcodeworld.com/articles/read/876/how-to-solve-knppaginator-exception-in-symfony-4-service-knp-paginator-not-found-even-though-it-exists-in-the-app-s-container that suggest injecting the paginator directly, then I did it:
class UsuariosController extends AbstractController
{
/**
* @Route("/list1", name="list")
*/
public function list(Request $request, PaginatorInterface $paginator)
{
// Retrieve the entity manager of Doctrine
$em = $this->getDoctrine()->getManager();
// Get some repository of data, in our case we have an Appointments entity
$usuariosRepository = $em->getRepository(Usuarios::class);
// Find all the data on the Appointments table, filter your query as you need
//->where('p.activo != :activo')
//->setParameter('activo', '1')
$allUsuariosQuery = $usuariosRepository->createQueryBuilder('p')
->getQuery();
// Paginate the results of the query
$usuarios = $paginator->paginate(
// Doctrine Query, not results
$allUsuariosQuery,
// Define the page parameter
$request->query->getInt('page', 1),
// Items per page
7
);
// Render the twig view
return $this->render('usuarios/index.html.twig', [
'usuarios' => $usuarios
]);
}
In config/package/paginator.yaml I have:
knp_paginator:
page_range: 15 # number of links showed 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)
default_options:
page_name: page # page query parameter name
sort_field_name: sort # sort field query parameter name
sort_direction_name: direction # sort direction query parameter name
distinct: true # ensure distinct results, useful when ORM queries are using GROUP BY statements
filter_field_name: filterField # filter field query parameter name
filter_value_name: filterValue # filter value query paameter name
template:
pagination: '@KnpPaginator/Pagination/twitter_bootstrap_v4_pagination.html.twig' # sliding pagination controls template
sortable: '@KnpPaginator/Pagination/sortable_link.html.twig' # sort link template
filtration: '@KnpPaginator/Pagination/filtration.html.twig'
Now it does not show bootstrap, it shows the default template: sliding.html.twig
Thank you.
Upvotes: 0
Views: 2885
Reputation: 479
SOLVED
http://knpbundles.com/KnpLabs/KnpPaginatorBundle
The knp_paginator service will be created lazily if the package symfony/proxy-manager-bridge is installed.
AND https://github.com/KnpLabs/KnpPaginatorBundle The knp_paginator service will be created lazily if the package symfony/proxy-manager-bridge is installed.
Then I had to:
composer require symfony/proxy-manager-bridge
now it works for me.
PD: knp paginator must be installed with composer require knplabs/knp-paginator-bundle
Upvotes: 1