使用 KnpPaginatorBundle 在 symfony 4 中分页
pagination in symfony 4 using KnpPaginatorBundle
试图从命名空间“App\Knp\Bundle\PaginatorBundle
”加载class“KnpPaginatorBundle
”。您是否忘记了“Knp\Bundle\PaginatorBundle\KnpPaginatorBundle
”的“use
”语句?
只需添加 KnpPaginatorBundle
的 class 命名空间:
在config/bundles.php
中:
<?php
return [
Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true],
Symfony\Bundle\TwigBundle\TwigBundle::class => ['all' => true],
***************************************************************
***************************************************************
Knp\Bundle\PaginatorBundle\KnpPaginatorBundle::class => ['all' => true]
];
您忘记在内核中添加您的 Bundle,正好在 config/bundles.php
Knp\Bundle\PaginatorBundle\KnpPaginatorBundle::class => ['all' => true]
解决方案是创建一个新存档 paginator.yaml
//config/paginator.yaml
knp_paginator:
page_range: 5 # rango por defecto de paginas en los controles del paginador
default_options:
page_name: page # nombre del parámetro de la URL para la página
sort_field_name: sort # nombre del parámetro de la URL para la ordenación
sort_direction_name: direction # nombre del parámetro de la URL para la dirección(ASC o DESC)
distinct: true # Garantizar resultados distintos
template:
pagination: '@KnpPaginator/Pagination/sliding.html.twig' # plantilla controles
sortable: KnpPaginatorBundle:Pagination:sortable_link.html.twig # plantilla links ordenación
在控制器中添加 extends Controller
之后 class
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Routing\Annotation\Route;
use App\Entity\Usuario;
use App\Form\UsuarioType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
/**
* @Route("/admin/usuarios")
*/
class BackendController extends Controller
{
/**
* @Route("/", name="admin")
*/
public function index(Request $request)
{
$usuarios = $this->getDoctrine()
->getRepository(Usuario::class)
->findAll();
$paginator = $this->get('knp_paginator');
$pagination = $paginator->paginate(
$usuarios, $request->query->getInt('page', 1), 20);
return $this->render('backend/usuario/index.html.twig',
array('pagination' => $pagination));
}
最后在 archive.yml
中添加这个
<div class="navigation">
{{ knp_pagination_render(pagination) }}
</div>
参考:https://github.com/KnpLabs/KnpPaginatorBundle/issues/468#issuecomment-356580135
试图从命名空间“App\Knp\Bundle\PaginatorBundle
”加载class“KnpPaginatorBundle
”。您是否忘记了“Knp\Bundle\PaginatorBundle\KnpPaginatorBundle
”的“use
”语句?
只需添加 KnpPaginatorBundle
的 class 命名空间:
在config/bundles.php
中:
<?php
return [
Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true],
Symfony\Bundle\TwigBundle\TwigBundle::class => ['all' => true],
***************************************************************
***************************************************************
Knp\Bundle\PaginatorBundle\KnpPaginatorBundle::class => ['all' => true]
];
您忘记在内核中添加您的 Bundle,正好在 config/bundles.php
Knp\Bundle\PaginatorBundle\KnpPaginatorBundle::class => ['all' => true]
解决方案是创建一个新存档 paginator.yaml
//config/paginator.yaml
knp_paginator:
page_range: 5 # rango por defecto de paginas en los controles del paginador
default_options:
page_name: page # nombre del parámetro de la URL para la página
sort_field_name: sort # nombre del parámetro de la URL para la ordenación
sort_direction_name: direction # nombre del parámetro de la URL para la dirección(ASC o DESC)
distinct: true # Garantizar resultados distintos
template:
pagination: '@KnpPaginator/Pagination/sliding.html.twig' # plantilla controles
sortable: KnpPaginatorBundle:Pagination:sortable_link.html.twig # plantilla links ordenación
在控制器中添加 extends Controller
之后 class
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Routing\Annotation\Route;
use App\Entity\Usuario;
use App\Form\UsuarioType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
/**
* @Route("/admin/usuarios")
*/
class BackendController extends Controller
{
/**
* @Route("/", name="admin")
*/
public function index(Request $request)
{
$usuarios = $this->getDoctrine()
->getRepository(Usuario::class)
->findAll();
$paginator = $this->get('knp_paginator');
$pagination = $paginator->paginate(
$usuarios, $request->query->getInt('page', 1), 20);
return $this->render('backend/usuario/index.html.twig',
array('pagination' => $pagination));
}
最后在 archive.yml
<div class="navigation">
{{ knp_pagination_render(pagination) }}
</div>
参考:https://github.com/KnpLabs/KnpPaginatorBundle/issues/468#issuecomment-356580135