[英]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.