Skip to content
Nibiru docsv0.9.2

Quick Start

Build a minimal Products page in five minutes — controller, view, navigation entry.

Stable Reading time ~ 2 min Edit on GitHub

By the end of this page you’ll have a working /products page rendered by Smarty, fed by a controller, listed in the sidebar.

Terminal window
./nibiru -c products

This creates two files:

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

Open productsController.php and replace the body with:

<?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();
}
}

Two methods are conventional and always called by the dispatcher:

  • navigationAction() — populates the global navigation menu.
  • pageAction() — renders the page itself.

Anything matching ?_action=foo will additionally call a fooAction() method between them.

Open 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>

Variables passed to View::assign() show up as {$variable} in Smarty.

Edit application/settings/config/navigation/main.json and add:

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

The navigation is loaded by JsonNavigation and rendered by navigation.tpl.

If you have PHP’s built-in server handy:

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

Visit http://localhost:8080/products/. You should see your three products with the cosmic theme of your CSS.

In the same controller:

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

Router already understands /products/detail/42 and /products/marduk-gold-plating/42 (SEO URL form — id and slug get auto-populated in $_REQUEST).

Create application/view/templates/products/detail.tpl with whatever markup you like, and you have a two-page app.