tangoman / pagination-bundle
此包的最新版本(1.4.2)没有可用的许可证信息。
Symfony Pagination Twig 扩展包
1.4.2
2018-04-08 20:56 UTC
Requires
- php: >=5.3.3
- symfony/framework-bundle: ~2.3|~3.0
- twig/twig: ~1.12|~2
README
此包允许您轻松并优化地使用 Doctrine\Paginator 方法来分页请求,并使结果针对 SEO 进行优化。分页始终使用 GET $page
来控制您所在的页面,因此您无需担心路由。有两个不同的响应式模板可供选择,分别是 default.thml.twig
和 smart.html.twig
。
安装
步骤 1:下载包
打开命令行,进入项目目录,然后执行以下命令以下载此包的最新稳定版本
$ composer require tangoman/pagination-bundle
此命令要求您全局安装 Composer,具体请参阅 Composer 文档中的安装章节。
步骤 2:启用包
然后,通过将其添加到项目 app/AppKernel.php
文件中注册的包列表中,启用此包
<?php // app/AppKernel.php // ... class AppKernel extends Kernel { // ... public function registerBundles() { $bundles = array( // ... new TangoMan\PaginationBundle\TangoManPaginationBundle(), ); // ... } }
步骤 3:配置您的仓库
<?php // Bundle/Entity/Repository/User.php use Doctrine\ORM\EntityRepository; use Doctrine\ORM\Tools\Pagination\Paginator; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; class User extends EntityRepository { public function findByPage($page = 1, $limit = 10) { if(!is_numeric($page)) { throw new \InvalidArgumentException( '$page must be an integer ('.gettype($page).' : '.$page.')' ); } if(!is_numeric($limit)) { throw new \InvalidArgumentException( '$limit must be an integer ('.gettype($limit).' : '.$limit.')' ); } $dql = $this->createQueryBuilder('user'); $dql->orderBy('user.lastname', 'DESC'); $firstResult = ($page - 1) * $limit; $query = $dql->getQuery(); $query->setFirstResult($firstResult); $query->setMaxResults($limit); $paginator = new Paginator($query); if(($paginator->count() <= $firstResult) && $page != 1) { throw new NotFoundHttpException('Page not found'); } return $paginator; } }
步骤 4:在控制器中发起请求
<?php // Bundle/Controller/DefaultController.php namespace Bundle\Controller; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\HttpFoundation\Request; class DefaultController extends Controller { /** * @Route("/user/", name="app_list_user") */ public function listUserAction(Request $request) { $db = $this->getDoctrine()->getManager(); $listUser = $db->getRepository('AppBundle:User')->findByPage( $request->query->getInt('page', 1), 5 ); return $this->render('listUser.html.twig', array( 'listUser' => $listUser )); } }
步骤 5:集成到 Twig 中
<table class="table"> <thead> <tr> <th>Lastname</th> <th>Firstname</th> </tr> </thead> <tbody> {% for user in listUser %} <tr> <td>{{ user.lastname | upper }}</td> <td>{{ user.firstname | capitalize }}</td> </tr> {% else %} <tr> <td colspan="2" class="text-center"> <em>No Users</em> </td> </tr> {% endfor %} </tbody> <tfoot> <tr> <td colspan="2"> {{ pagination(listUser) }} </td> </tr> </tfoot> </table>
如果您想使用“智能”分页,请添加以下参数
{{ pagination(listUser, 'smart') }}
您可以使用以下代码显示每页结果限制选择器
{{ pagination(listUser, 'select') }}
您也可以使用您自己的分页模板
{{ pagination(listUser, '@AppBundle/pagination/custom.html.twig') }}
步骤 6:集成 rel="canonical" rel="next" 和 rel="prev" 标记
在您的视图内部
<head> {{ pagination(listUser, 'meta') }} </head>
步骤 7:享受
<table class="table"> <thead> <tr> <th>Lastname</th> <th>Firstname</th> </tr> </thead> <tbody> <tr> <td>HENRY</td> <td>Thibault</td> </tr> <tr> <td>LAZZAROTTO</td> <td>Fabrice</td> </tr> <tr> <td>MORIN</td> <td>Matthias</td> </tr> <tr> <td>MAHÉ</td> <td>Alexandre</td> </tr> <tr> <td>GRÉAUX</td> <td>Tony</td> </tr> <tr> <td>CICHOWLAS</td> <td>Cédric</td> </tr> </tbody> <tfoot> <tr> <td colspan="2"> <ul class="pagination"> <li class="page-item"> <a href="/app_dev.php/user/?page=1" class="page-link"> << </a> </li> <li class="page-item"> <a href="/app_dev.php/user/?page=2" class="page-link"> < </a> </li> <li class="page-item hidden-xs"> <a href="/app_dev.php/user/?page=1" class="page-link"> 1 </a> </li> <li class="page-item hidden-xs"> <a href="/app_dev.php/user/?page=2" class="page-link"> 2 </a> </li> <li class="page-item active"> <a href="/app_dev.php/user/?page=3" class="page-link"> 3 </a> </li> <li class="page-item hidden-xs"> <a href="/app_dev.php/user/?page=4" class="page-link"> 4 </a> </li> <li class="page-item"> <a href="/app_dev.php/user/?page=4" class="page-link"> > </a> </li> <li class="page-item"> <a href="/app_dev.php/user/?page=4" class="page-link"> >> </a> </li> </ul> </td> </tr> </tfoot> </table>
版权 (c) Thibault Henry
如果您喜欢 TangoMan Pagination Bundle,请给它加星标!
并在 GitHub 上关注我:TangoMan75 ... 并查看我的其他酷项目。