コンテンツにスキップ
Nibiru docsv0.9.2

クイック スタート

5分で最小限の製品ページを構築する — コントローラー、ビュー、ナビゲーションエントリ。

Stable Reading time ~ 1 min Edit on GitHub

このページの終わりまでに、Smartyによってレンダリングされ、コントローラーによって供給される /products ページがサイドバーにリスト表示されます。

Terminal window
./nibiru -c products

これにより2つのファイルが作成されます。

application/controller/productsController.php
application/view/templates/products.tpl

productsController.php を開き、内容を以下のように置き換えます:

<?php
namespace Nibiru;
use Nibiru\Adapter\Controller;
class productsController extends Controller
{
public function pageAction()
{
View::assign([
'title' => 'Products — Nibiru',
'products' => [
['id' => 1, 'name' => 'Marduk Gold Plating', 'price' => 99.0],
['id' => 2, 'name' => 'Tiamat Hull Sealant', 'price' => 49.0],
['id' => 3, 'name' => 'Anu Stardust Polish', 'price' => 19.5],
],
'css' => Config::getInstance()->getConfig()[View::NIBIRU_SETTINGS]['smarty.css'],
'js' => Config::getInstance()->getConfig()[View::NIBIRU_SETTINGS]['smarty.js'],
]);
}
public function navigationAction()
{
JsonNavigation::getInstance()->loadJsonNavigationArray();
}
}

2つのメソッドは従来通りで、ディスパッチャによって常に呼び出されます

  • navigationAction() — グローバルナビゲーションメニューを設定します。
  • pageAction() — ページ自体を描画します。

?_action=foo に一致するものは、それらの間に fooAction() メソッドを追加で呼び出します。

application/view/templates/products.tplを開きます:

{include 'shared/header.tpl'}
<body>
{include file="navigation.tpl"}
<main class="container">
<h1>{$title}</h1>
<table class="table">
<thead>
<tr><th>#</th><th>Name</th><th>Price</th></tr>
</thead>
<tbody>
{foreach $products as $p}
<tr>
<td>{$p.id}</td>
<td><a href="/products/detail/{$p.id}">{$p.name|escape}</a></td>
<td>{$p.price|string_format:"%.2f"} €</td>
</tr>
{/foreach}
</tbody>
</table>
</main>
{include 'shared/footer.tpl'}
</body>

View::assign()に渡された変数は、Smartyで{$variable}として表示されます。

4. ナビゲーションエントリを追加する

Section titled “4. ナビゲーションエントリを追加する”

application/settings/config/navigation/main.json を編集し、以下を追加してください:

{
"label": "Products",
"href": "/products",
"icon": "shopping-bag"
}

ナビゲーションは JsonNavigation で読み込まれ、navigation.tpl でレンダリングされます。

PHPの組み込みサーバーがあれば便利です:

Terminal window
APPLICATION_ENV=development php -S localhost:8080 -t .

http://localhost:8080/products/にアクセスしてください。ここでは、あなたのCSSの宇宙的なテーマを持つ3つの製品が表示されるはずです。

同じコントローラー内で:

public function detailAction()
{
$id = (int) ($_REQUEST['id'] ?? 0);
View::assign([
'title' => "Product #$id",
'id' => $id,
]);
}

Router はすでに /products/detail/42/products/marduk-gold-plating/42(SEO URL 形式 — idslug が自動的に $_REQUEST にセットされる)を理解しています。

application/view/templates/products/detail.tpl を作成し、好きなマークアップを使用して、2ページのアプリケーションが完成します。