makscraft / pagination
Symfony应用程序的分页管理器。
1.0.0
2024-07-30 13:59 UTC
Requires
- php: >=7.0
README
Symfony应用程序的分页管理器。
生成SQL查询的链接和参数列表。
安装
composer require makscraft/pagination
在控制器或仓库中使用示例
use Makscraft\Pagination;
$total = ... ; //count your all needed items
$limit = 20; //items per page
$pagination = new Pagination($total, $limit);
$repository -> findBy([...], [...], $pagination -> getLimit(), $pagination -> getOffset());
//Returns array of links for html template.
$data = $pagination -> setPath('/catalog') -> getDisplayData();
$data中的结果链接将类似于: '/catalog?page=3'。
如果您想传递带有附加GET参数的路径,可以这样做。
$pagination -> setPath('/catalog?sort=name&direction=asc');
对于Twig模板
使用包模板或将其复制到项目的模板目录。在控制器中将分页对象传递到模板中。
return $this -> render('mypage.html.twig', [
...
'pagination' => $pagination
]);
要使用来自供应商目录的模板,请将路径添加到twig配置文件中。
# config/packages/twig.yaml
paths:
'vendor/makscraft/pagination/templates': 'pagination'
接下来,将分页模板包含到您的twig文件中。
{% include '@pagination/pagination.html.twig' %}
您还可以将模板文件复制到模板目录中,并按需使用它。
模板文件位置: vendor/makscraft/pagination/templates/pagination.html.twig