[英]Pagination with Slim Framework and Laravel's Eloquent ORM
我使用 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开始,你可以做这样的事情
$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之外引导Pagination类。
考虑一下https://github.com/zofe/rapyd-framework它使用Slim,Twig,Eloquent,Symfony Forms ..但是用于分页的自定义小部件。 (我是作者)
composer.json
在require部分添加以下行:
"illuminate/pagination":"~5.0"
打开Ubuntu 14.04终端并转到项目目录。 运行composer update
命令。 它将下载您的分页库。
在模型文件中添加这两行
use Illuminate\\Pagination;
use Illuminate\\Pagination\\Paginator;
在您的控制器中放置此代码,您将获得分页数据。
$data = Student::paginate(5);
为了在视图文件中获取分页链接。
$students->links();
7- 不要忘记修改currentPageResolver
和currentPathResolver
。 您可以通过使用如下所示的简单中间件来实现此目的。
<?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.