在关注使用Slim / Twig和Eloquent 5.2构建PHP Authentication System的视频教程之后。
我已完成视频教程并拥有一个有效的系统。 但是我没有在教程的25th episode上为所有用户列表添加分页。
一切似乎都运转良好。但是当我按第2,3,4页等时......我仍然收到第1页数据。
这是我到目前为止的代码:
路由/用户/ 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');
视图/用户/ 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>
非常感谢任何帮助!
答案 0 :(得分:2)
嗯,这是一个月和几天。我也遇到了你的问题,我刚刚得到了解决方案。
我做的是,我打电话给Paginator
班,然后使用静态方法强制分页到我所在的任何页面。
Paginator::currentPageResolver(function() use ($current_page) {
return $current_page;
});
然后调用您想要分页的数据。这是我下面的全部代码。
<?php
use Illuminate\Pagination\Paginator;
$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');
我忘记了在哪里看到了答案,我发现它后会发布一个参考链接。