dezull / dbal-paginator-service-provider
一个基于 Silex 的 Doctrine DBAL 分页器提供程序,基于 knp pager 组件和 knp-paginator-bundle。
dev-master
2013-10-11 03:30 UTC
Requires
- php: >=5.3.0
- doctrine/dbal: ~2.2
- knplabs/knp-paginator-bundle: 2.3.*
This package is not auto-updated.
Last update: 2024-09-23 15:20:39 UTC
README
安装
添加到 composer.json
{
"require": {
"dezull/dbal-paginator-service-provider": "dev-master"
}
}
用法
- 当注册
TwigServiceProvider时,添加分页器的模板路径(创建自己的或直接使用 KnpPaginatorBundle 的)。
$app->register(new Silex\Provider\TwigServiceProvider(), array( 'twig.path' => array( /* Your other templates */ __DIR__.'/../vendor/knplabs/knp-paginator-bundle/Knp/Bundle/PaginatorBundle/Resources/views', ), ));
- 注册
DBALPaginatorServiceProvider。
$app->register(new Dezull\Silex\Provider\DBALPaginatorServiceProvider\DBALPaginatorServiceProvider(), array( /* The following assumes you use the template path as in step #1 */ 'dezull.dbal_paginator.template.pagination' => 'Pagination/twitter_bootstrap_pagination.html.twig', 'dezull.dbal_paginator.template.sortable' => 'Pagination/sortable_link.html.twig', ));
- 在你的控制器中
public function indexAction(Request $request, Application $app) { $page = (int) $request->query->get('page', 1); $sortKey = $request->query->get('sort', 's.id'); $direction = $request->query->get('direction', 'desc'); /* Doctrine DBAL QueryBuilder */ $qb = $app['db']->createQueryBuilder() ->select('s.*') ->from('sometable', 's') ->orderBy($sortKey, $direction); $pagination = $app['dezull.dbal_paginator']->paginate( $qb, $page, 20 /* per page limit */ ); return $app['twig']->render('Foo/index.html.twig', array( 'pagination' => $pagination, )); }
- 在模板中渲染分页
{{ dezull_dbal_pagination_render(pagination) }}
待办事项
- 移除对 KnpPaginatorBundle 的依赖