4

我使用 Slim Framework 作为路由器,使用 Twig 作为模板引擎,使用 Eloquent ORM 来处理数据库。

我为这些库创建了引导程序。

<?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);

和路线:

/**
 * 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();

我想要的是:如何对结果进行分页:

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

考虑到我有“两个”页面,例如“/”和“/page/1”。

在每个页面中,我想要最多 30 个结果。

4

3 回答 3

5

从Illumination 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));

});

更好的解决方案是包含 Illuminate\Pagination 包并使其与

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

但是我不知道如何在 laravel 之外引导分页类。

考虑看看https://github.com/zofe/rapyd-framework 它使用 Slim、Twig、Eloquent、Symfony Forms .. 但用于分页的自定义小部件。(我是作者)

于 2013-11-14T18:26:30.590 回答
3
  1. 打开composer.json
  2. 在 require 部分添加这一行:

    "illuminate/pagination":"~5.0"

  3. 打开 Ubuntu 14.04 终端并转到项目目录。运行composer update命令。它将下载您的分页库。

  4. 在模型文件中添加这两条线

    use Illuminate\Pagination;

    use Illuminate\Pagination\Paginator;

  5. 在您的控制器中放置此代码,您将获得分页数据。

    $data = Student::paginate(5);

  6. 为了在视图文件中获取分页链接。

    $students->links();

于 2017-05-24T08:30:18.080 回答
0

7-不要忘记修改currentPageResolvercurrentPathResolver。您可以通过使用如下所示的简单中间件来实现此目的。

<?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);
    }
}
于 2022-01-11T17:43:36.427 回答