使用 Twig 和 Eloquent-5.2 进行分页

2022-01-22 00:00:00 php slim eloquent pagination twig

按照此视频教程了解如何使用 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&amp;d=identicon"> Admin</a> [Admin]
    </div>
    <div class="user">
        <a href="/pcb/public/u/Jamarcus22"><img src="http://www.gravatar.com/avatar/...?s=18&amp;d=identicon"> Jamarcus22 | Abbie O&#039;Hara</a>
    </div>
    <div class="user">
        <a href="/pcb/public/u/jWalker"><img src="http://www.gravatar.com/avatar/...?s=18&amp;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&amp;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&amp;d=identicon"> lCarter | Lilly Stokes</a>
    </div>
<ul class="pagination"><li class="disabled"><span>&laquo;</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">&raquo;</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.

相关文章