首页 > 解决方案 > symfony 5 设置捆绑配置

问题描述

我在 symfony 5 中安装了 knp paginator,它工作正常。但是当我尝试在 config/package/paginator.yaml 中设置引导模板时,它不起作用。

我发现https://github.com/KnpLabs/KnpPaginatorBundle/issues/468 我应该使用控制器而不是 abstractController。我还发现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建议直接注入分页器,然后我做到了:

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

在 config/package/paginator.yaml 我有:

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' 

现在它不显示引导程序,它显示默认模板:sliding.html.twig

谢谢你。

标签: symfonytemplatesexternalbundles

解决方案


解决了

http://knpbundles.com/KnpLabs/KnpPaginatorBundle

如果安装了包 symfony/proxy-manager-bridge,knp_paginator 服务将被延迟创建。

https://github.com/KnpLabs/KnpPaginatorBundle 如果安装了包 symfony/proxy-manager-bridge,knp_paginator 服务将被延迟创建。

然后我不得不:

composer 需要 symfony/proxy-manager-bridge

现在它对我有用。

PD:knp 分页器必须与 composer require knplabs/knp-paginator-bundle 一起安装


推荐阅读