tiloweb/pagination-bundle

此包的最新版本(v1.6.3)没有提供许可证信息。

/

v1.6.3 2023-06-12 08:47 UTC

This package is auto-updated.

Last update: 2024-09-12 12:00:34 UTC


README

此Bundle使使用Doctrine\Paginator方法进行最佳分页变得容易。

安装

步骤 1: 下载Bundle

打开命令行控制台,进入您的项目目录,并执行以下命令以下载此bundle的最新稳定版本

$ composer require tiloweb/pagination-bundle "dev-master"

此命令需要您全局安装Composer,如Composer文档中的安装章节所述。

步骤 2: 启用Bundle

然后,通过将其添加到项目中的app/AppKernel.php文件中注册的bundle列表中,启用此bundle

<?php
// app/AppKernel.php

// ...
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...

            new Tiloweb\PaginationBundle\TilowebPaginationBundle(),
        );

        // ...
    }

    // ...
}

步骤 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, $max = 10)
    {
        $dql = $this->createQueryBuilder('user');
        $dql->orderBy('user.lastname', 'DESC');

        $firstResult = ($page - 1) * $max;

        $query = $dql->getQuery();
        $query->setFirstResult($firstResult);
        $query->setMaxResults($max);

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

注意 $request->query->getInt('page', 1),您可以选择$_GET参数的名称,但默认为page

步骤 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, 'page') }}
            </td>
        </tr>
    </tfoot>
</table>

使用函数 {{ pagination(Paginator, get) }} 来渲染分页。paginator参数是您的Paginator对象,而get参数(默认值为page)是您希望分页监听的$_GET参数的名称。

步骤 6: 享受

<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>HOUDAYER</td>
            <td>Gaël</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 start">
                        <a href="/app_dev.php/user/?page=1" class="page-link disabled">
                            &lt;&lt;
                        </a>
                    </li>
                    <li class="page-item prev">
                        <a href="/app_dev.php/user/?page=2" class="page-link disabled" rel="prev">
                            &lt;
                        </a>
                    </li>
                    <li class="page-item">
                        <a href="/app_dev.php/user/?page=1" class="page-link">
                            1
                        </a>
                    </li>
                    <li class="page-item">
                        <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">
                        <a href="/app_dev.php/user/?page=4" class="page-link">
                            4
                        </a>
                    </li>
                    <li class="page-item next">
                        <a href="/app_dev.php/user/?page=4" class="page-link" rel="next">
                            &gt;
                        </a>
                    </li>
                    <li class="page-item end">
                        <a href="/app_dev.php/user/?page=4" class="page-link">
                            &gt;&gt;
                        </a>
                    </li>
                </ul>
            </td>
        </tr>
    </tfoot>
</table>

更改分页模板

您可以通过在config.yml中调用它来自定义twig分页模板。

tiloweb_pagination:
      template: 'Your/File.html.twig'