【问题标题】:Pagination with Slim Framework and Laravel's Eloquent ORM使用 Slim 框架和 Laravel 的 Eloquent ORM 进行分页
【发布时间】:2013-10-06 22:00:48
【问题描述】:

我使用 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 个结果。

【问题讨论】:

    标签: php laravel-4 twig eloquent slim


    【解决方案1】:

    从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.. 以及用于分页的自定义小部件。 (我是作者)

    【讨论】:

    • 嘿,我在我的 Slim 框架应用程序中遇到以下异常,请告诉我运行此应用程序所需的依赖项(放入我的 composer.json)。 Type: ReflectionException Code: -1 Message: Class paginator does not exist File: C:\xampp\htdocs\workspace\shopping-demo\vendor\illuminate\container\Illuminate\Container\Container.php Line: 501
    • 没有解决办法,基本上“分页”需要laravel路由器。我建议你离开 Slim 使用 Laravel 路由器(基本上是 symfony 路由器):在这篇文章中你可以找到一种引导它的方法:gufran.me/post/…
    【解决方案2】:
    1. 打开composer.json
    2. 在需要部分添加这一行:

      "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-&gt;links();

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 2016-02-04
        • 2017-07-21
        • 2019-05-21
        • 1970-01-01
        • 2017-03-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多