クイック スタート
5分で最小限の製品ページを構築する — コントローラー、ビュー、ナビゲーションエントリ。
このページの終わりまでに、Smartyによってレンダリングされ、コントローラーによって供給される /products ページがサイドバーにリスト表示されます。
1. コントローラーを生成する
Section titled “1. コントローラーを生成する”./nibiru -c productsこれにより2つのファイルが作成されます。
application/controller/productsController.phpapplication/view/templates/products.tpl2. コントローラーを接続する
Section titled “2. コントローラーを接続する”productsController.php を開き、内容を以下のように置き換えます:
<?phpnamespace 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() メソッドを追加で呼び出します。
3. ビューを書く
Section titled “3. ビューを書く”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 でレンダリングされます。
5. 実行する
Section titled “5. 実行する”PHPの組み込みサーバーがあれば便利です:
APPLICATION_ENV=development php -S localhost:8080 -t .http://localhost:8080/products/にアクセスしてください。ここでは、あなたのCSSの宇宙的なテーマを持つ3つの製品が表示されるはずです。
詳細ページを追加する
Section titled “詳細ページを追加する”同じコントローラー内で:
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 形式 — id と slug が自動的に $_REQUEST にセットされる)を理解しています。
application/view/templates/products/detail.tpl を作成し、好きなマークアップを使用して、2ページのアプリケーションが完成します。
次にどこへ?
Section titled “次にどこへ?”- アーキテクチャ (MMVC) — 全ての構成要素がどのように組み合わさっているか。
- モジュール — コントローラーとトレイトから実際のモジュールに進むタイミング。
- データベース & マイグレーション — ページを実際の
productsテーブルに接続する。