简体   繁体   English

使用 Slim 框架和 Laravel 的 Eloquent ORM 进行分页

[英]Pagination with Slim Framework and Laravel's Eloquent ORM

I'm using Slim Framework as router, Twig as Template Engine and Eloquent ORM to handle the database.我使用 Slim Framework 作为路由器,使用 Twig 作为模板引擎,使用 Eloquent ORM 来处理数据库。

I created the bootstrap for these libraries.我为这些库创建了引导程序。

<?php

require_once 'vendor/autoload.php';

/**
 * Laravel Eloquent ORM
 */
use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Events\Dispatcher;
use Illuminate\Container\Container;

$database_capsule = new Capsule;

$database_capsule->addConnection([
    'driver'    => 'mysql',
    'host'      => 'localhost',
    'database'  => 'database',
    'username'  => 'username',
    'password'  => 'password',
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
]);

$database_capsule->setEventDispatcher(new Dispatcher(new Container));

$database_capsule->setAsGlobal();

$database_capsule->bootEloquent();

/**
 * Twig Template Engine
 */
Twig_Autoloader::register();

$twig_loader = new Twig_Loader_Filesystem('template');

$twig_engine = new Twig_Environment($twig_loader);

And the routes:和路线:

/**
 * Slim Framework
 */
$application = new \Slim\Slim();

$application->get('/', function () use ($twig_engine) {

    $foods = Capsule::table('foods')->get();

    $index = $twig_engine->loadTemplate('index.html');

    echo $index->render(array('foods' => $foods));

});

$application->get('/page/:number', function ($number) use ($twig_engine) {

    $foods = Capsule::table('foods')->get();

    $index = $twig_engine->loadTemplate('index.html');

    echo $index->render(array('foods' => $foods));

});

$application->run();

What I want is: How can I paginate the results of:我想要的是:如何对结果进行分页:

$foods = Capsule::table('foods')->get();

Considering that I have "two" pages, the "/" and the "/page/1" for example.考虑到我有“两个”页面,例如“/”和“/page/1”。

And in each page I want in max 30 results.在每个页面中,我想要最多 30 个结果。

starting from illuminate 4 you can do someting like this 从照亮4开始,你可以做这样的事情

$application->get('/page/:number', function ($number) use ($twig_engine) {

    $foods = Capsule::table('foods')->skip(30*$number)->take(30)->get();
    $index = $twig_engine->loadTemplate('index.html');

    echo $index->render(array('foods' => $foods));

});

a better solution is to include Illuminate\\Pagination package and make it work with 更好的解决方案是包括Illuminate \\ Pagination包并使其可以使用

Capsule::table('foods')->paginate(5) 

however I don't know how to bootstrap Pagination class outside laravel. 但是我不知道如何在laravel之外引导Pagination类。

Consider taking a look at https://github.com/zofe/rapyd-framework It uses Slim, Twig, Eloquent, Symfony Forms.. but custom widgets for pagination. 考虑一下https://github.com/zofe/rapyd-framework它使用Slim,Twig,Eloquent,Symfony Forms ..但是用于分页的自定义小部件。 (I'm the author) (我是作者)

  1. Open composer.json 打开composer.json
  2. Add this line in require section: 在require部分添加以下行:

    "illuminate/pagination":"~5.0"

  3. Open your Ubuntu 14.04 terminal and go to your project's directory. 打开Ubuntu 14.04终端并转到项目目录。 Run composer update command. 运行composer update命令。 It will download your pagination library. 它将下载您的分页库。

  4. Add these both Lines in your model file 在模型文件中添加这两行

    use Illuminate\\Pagination;

    use Illuminate\\Pagination\\Paginator;

  5. In your controller place this code you will get paginated data. 在您的控制器中放置此代码,您将获得分页数据。

    $data = Student::paginate(5);

  6. In order to get paginated links in view file. 为了在视图文件中获取分页链接。

    $students->links();

7- Don't forget to modify currentPageResolver and currentPathResolver . 7- 不要忘记修改currentPageResolvercurrentPathResolver You can achieve this by using a simple middleware like the following.您可以通过使用如下所示的简单中间件来实现此目的。

<?php

namespace Mismar\Api\Middlewares;

use Illuminate\Pagination\Paginator;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\RequestHandlerInterface as RequestHandler;
use Slim\Psr7\Response;

class PaginatorMiddleware
{
    /**
     * Example middleware invokable class
     *
     * @param  ServerRequest  $request PSR-7 request
     * @param  RequestHandler $handler PSR-15 request handler
     *
     * @return Response
     */
    public function __invoke(Request $request, RequestHandler $handler): Response
    {
        Paginator::currentPathResolver(function () use ($request) {
            return $request->getUri()->getPath();
        });
        Paginator::currentPageResolver(function ($pageName = 'page') use ($request) {
            return $request->getQueryParams()[$pageName] ?? 1;
        });

        return $handler->handle($request);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM