【问题标题】:pagination in symfony 4 using KnpPaginatorBundle使用 KnpPaginatorBundle 在 symfony 4 中进行分页
【发布时间】:2019-01-26 00:21:45
【问题描述】:

尝试从命名空间“App\Knp\Bundle\PaginatorBundle”加载类“KnpPaginatorBundle”。您是否忘记了“Knp\Bundle\PaginatorBundle\KnpPaginatorBundle”的“use”声明?

【问题讨论】:

  • 图片链接?不用了,谢谢。用 2.1 和 4 标记问题?比较不寻常。错误消息本身会准确告诉您问题出在哪里以及如何解决。
  • 抱歉,我刚开始使用 symfony 一周,所以我使用了 use 声明,但如果您可以查看图像以查看我的控制器和其他页面,则没有任何改变
  • 点击随机发布的图片对您来说是个好主意吗?
  • 我猜问题出在 config/bundles.php 中,因为这是我能想到的唯一可以引用 KnpPaginatorBundle-class 的地方。如果不是这种情况,请在您使用 KnpPaginatorBundle 类的 src/ 文件夹中搜索并检查 use 语句是否在文件顶部的列表中。
  • 不是真的,但是当我们使用 KnpPaginatorBundle 时,我们使用相同的类,所以我试图显示我在 KnpPaginatorBundle 指令中使用相同的代码,以任何方式图像不是问题,问题是 paginatin 先生

标签: symfony symfony-forms symfony-2.1 symfony4


【解决方案1】:

只需添加KnpPaginatorBundle的类命名空间:

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]
];

【讨论】:

    【解决方案2】:

    你忘记在内核中添加你的 Bundle,正好在 config/bundles.php

    Knp\Bundle\PaginatorBundle\KnpPaginatorBundle::class => ['all' => true]
    

    【讨论】:

    • 在 Symfony4 中,新的 bundle 会自动添加到配置文件中!
    【解决方案3】:

    解决方案是创建一个新存档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
    

    在Controller之后在类中添加extends Controller

    <?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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-27
    • 2018-12-13
    • 2019-01-11
    • 1970-01-01
    • 2014-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多