Slim Framework をルーターとして、Twig を Template Engine として、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」の「2」ページがあるとします。
そして、各ページで最大30の結果が必要です。