Pagination with Twig and Eloquent-5.2(使用 Twig 和 Eloquent-5.2 进行分页)
问题描述
按照此视频教程了解如何使用 Slim/构建 PHP 身份验证系统Twig and Eloquent 5.2.
After following this video tutorial on building a PHP Authentication System with Slim/Twig and Eloquent 5.2.
我已经完成了视频教程,并且有了一个工作系统.但我未能在 教程第 25 集.
I have finished the video tutorial and have a working system. But I am failing to add pagination to the all user list on the 25th episode of the tutorial.
似乎一切正常.但是当我按第 2、3、4 页等时...我仍然得到第 1 页的数据.
这是我目前的代码:
routes/user/all.php
$app->get('/users', $authenticated(), function() use ($app) {
$users = $app->user->where('active', true)->paginate(5);
$users->setPath($app->urlFor('user.all'));
$app->render('user/all.php', [
'users' => $users
]);
})->name('user.all');
查看/user/all.php
{% extends 'templates/default.php' %}
{% block title %}All Users{% endblock %}
{% block content %}
<h4>All Users</h4>
{% if users is empty %}
<p>No registered users</p>
{% else %}
{% for user in users %}
<div class="user">
<a href="{{ urlFor('user.profile', {username: user.username}) }}"><img src="{{ user.getAvatarUrl({size: 18}) }}"> {{ user.username }}{% if user.getFullName %} | {{ user.getFullName }}{% endif %}</a>
{% if user.isAdmin() %}
[Admin]
{% endif %}
</div>
{% endfor %}
{{ users.render()|raw }}
{% endif %}
{% endblock %}
composer.json
{
"autoload": {
"psr-4": {
"A146\": "app/A146"
}
},
"require": {
"slim/slim": "~2.0",
"slim/views": "0.1.*",
"twig/twig": "~1.21",
"phpmailer/phpmailer": "~5.2",
"hassankhan/config": "0.8.*",
"illuminate/database": "5.*",
"illuminate/pagination": "5.*",
"alexgarrett/violin": "2.*",
"ircmaxell/random-lib": "~1.1",
"fzaninotto/faker": "*"
}
}
这是我得到的输出:
<div class="row">
<h4>All Users</h4>
<div class="user">
<a href="/pcb/public/u/Admin"><img src="http://www.gravatar.com/avatar/...?s=18&d=identicon"> Admin</a> [Admin]
</div>
<div class="user">
<a href="/pcb/public/u/Jamarcus22"><img src="http://www.gravatar.com/avatar/...?s=18&d=identicon"> Jamarcus22 | Abbie O'Hara</a>
</div>
<div class="user">
<a href="/pcb/public/u/jWalker"><img src="http://www.gravatar.com/avatar/...?s=18&d=identicon"> jWalker | Salvador Douglas</a>
</div>
<div class="user">
<a href="/pcb/public/u/Karelle00"><img src="http://www.gravatar.com/avatar/...?s=18&d=identicon"> Karelle00 | Harmon Ryan</a>
</div>
<div class="user">
<a href="/pcb/public/u/lCarter"><img src="http://www.gravatar.com/avatar/...?s=18&d=identicon"> lCarter | Lilly Stokes</a>
</div>
<ul class="pagination"><li class="disabled"><span>«</span></li> <li class="active"><span>1</span></li><li><a href="/pcb/public/users?page=2">2</a></li><li><a href="/pcb/public/users?page=3">3</a></li><li><a href="/pcb/public/users?page=4">4</a></li><li><a href="/pcb/public/users?page=5">5</a></li><li><a href="/pcb/public/users?page=6">6</a></li><li><a href="/pcb/public/users?page=7">7</a></li> <li><a href="/pcb/public/users?page=2" rel="next">»</a></li></ul>
</div>
非常感谢任何帮助!
推荐答案
嗯,已经一个月零几天了.我也遇到了你的问题,我刚刚得到了解决方案.
Well, it's been a month and a few days. I also encountered your problem, I just got my solution for it.
我所做的是,我调用了 Paginator
类,然后使用静态方法强制分页到我所在的任何页面.
What I did was, I called the Paginator
class then used a static method to force the pagination to whatever page I am in.
Paginator::currentPageResolver(function() use ($current_page) {
return $current_page;
});
然后调用你想要分页的数据.下面是我的全部代码.
Then call the data that you wanted to be paginated. Here's my whole code below.
<?php
use IlluminatePaginationPaginator;
$app->get('/controller/clients', $controllerAuthenticated(), function() use ($app) {
$request = $app->request;
$current_page = $request->get('page');
Paginator::currentPageResolver(function() use ($current_page) {
return $current_page;
});
$clients = $app->client->where(function($query) use ($client_name, $country_id) {
if ($client_name != '') {
$query->where('client_name', 'LIKE', '%'. $client_name .'%');
}
if ($country_id != '') {
$query->where('country_id', $country_id);
}
})
->orderBy('client_name', 'ASC')
->paginate(1);
$clients->setPath($app->urlFor('controller.client.all'));
$app->render('/controller/client/all.twig', [
'clients' => $clients,
'countries' => $countries,
'request' => $request
]);
})->name('controller.client.all');
我忘记了我在哪里看到的答案,一旦我找到它,我会发布一个参考链接.
I forgot where I saw the answer though, I'll post a link to a reference once I found it.
这篇关于使用 Twig 和 Eloquent-5.2 进行分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 Twig 和 Eloquent-5.2 进行分页
基础教程推荐
- Libpuzzle 索引数百万张图片? 2022-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 在多维数组中查找最大值 2021-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01