Documentation Getting started
Quick start
Install olala and build your first site in five minutes: layout, home page, module and pretty URLs.
In this guide we'll build a small site from scratch: a shared layout, a home page,
an “About” section and a blog with pretty URLs like /blog/hello-world.
All you need is PHP 8 and five minutes.
Prerequisites. PHP 8.0 or newer (with the intl extension if you're going to format dates),
a web server with URL rewriting (Apache with mod_rewrite or nginx) — or the PHP built-in server for development.
Installation
-
Put the framework in the site root
The
olalafolder must end up right in the site root (document root). The easiest way is to download the olala.tar.bz2 archive and unpack it:Terminalmkdir mysite && cd mysite wget https://olala.glenn.ru/olala.tar.bz2 tar -xjf olala.tar.bz2 && rm olala.tar.bz2Or clone the repository:
Terminalgit clone git@git.glenn.ru:v0yager/olala.git olala -
Run the initial setup
The
first_start.shscript copies starter files into the site root and creates the first controller.Terminalcd olala && sh first_start.sh && cd ..After that the root will contain:
index.php— the single entry point;config.php— settings and routes;.htaccess— sends every request toindex.php;site/modules/index/controllers/indexController.php— the home page controller;create— a link to theolala/console/createconsole utility.
-
Start the site
On an Apache server, just point the domain at the site folder. For local development the PHP built-in server is enough — it needs a tiny router file so that static files (CSS, images) are served as is:
dev-router.php<?php // dev-router.php — for development only if (is_file(__DIR__ . parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH))) { return false; // the server will serve an existing file itself } require __DIR__ . '/index.php';Terminalphp -S localhost:8000 dev-router.phpOpen
http://localhost:8000— you'll see “Olala installed”. The framework is up and running.
Step 1. A shared layout
A layout is the wrapper shared by every page: <head>, header, footer.
Whatever the controller returns is passed into the layout as the $content variable.
Create the layout file:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title><?= htmlspecialchars($title ?? 'My site') ?></title>
</head>
<body>
<header>
<a href="/">Home</a> · <a href="/about">About</a> · <a href="/blog">Blog</a>
</header>
<!-- the action's result goes here -->
<main><?= $content ?></main>
<footer>© <?= date('Y') ?></footer>
</body>
</html>What $content is. It's the block of content returned by the controller action. The layout doesn't care how it was produced:
- a rendered view —
return $this->render('index', [...]); - or just a string —
return 'Hello, world!';
Either way the framework puts the result into <?= $content ?>, while the header and footer stay shared by all pages.
And enable it in config.php:
view::$layout = 'default'; // folder name in site/layouts/Step 2. The home page
The / URL is handled by the index module, index controller and index action.
Replace the controller's contents so that it passes data to a template:
<?php
use olala\controller;
class IndexIndexController extends controller {
function indexAction() {
return $this->render('index', [
'title' => 'Home',
'features' => ['light', 'fast', 'MVC'],
]);
}
}The template is looked up in the module's views folder. It's an ordinary PHP file, and the array's variables are available in it directly:
<h1>Hi! This is my olala site</h1>
<ul>
<?php foreach ($features as $f): ?>
<li><?= htmlspecialchars($f) ?></li>
<?php endforeach ?>
</ul>The title variable passed to render() is also available in the layout — a handy way to set the page title from the controller.
Step 3. A new section with one command
Let's create the “About” section with the console utility — it makes the folders, the controller and the template:
./create module about
Done: the page already opens at /about. Edit the template
site/modules/about/views/index.php — and the section is filled in.
Step 4. A blog with pretty URLs
The blog needs a model with posts, a controller with two actions (list and post) and a route
that turns /blog/hello-world into a call to the right action.
Model
Models are plain classes. The olala autoloader turns a namespace into a file path,
so the site\models\Post class will be found in site/models/Post.php with no configuration at all.
For this example the data lives right in an array; in a real project you'd query the database here.
<?php
namespace site\models;
class Post {
const POSTS = [
'hello-world' => ['title' => 'Hello, world', 'text' => 'The first blog post.'],
'why-olala' => ['title' => 'Why olala', 'text' => 'Because it is fast and simple.'],
];
public static function all(): array {
return self::POSTS;
}
public static function find(string $slug): ?array {
return self::POSTS[$slug] ?? null;
}
}Controller
./create module blog<?php
use olala\controller;
use site\models\Post;
class BlogIndexController extends controller {
// /blog — list of posts
function indexAction() {
return $this->render('index', [
'title' => 'Blog',
'posts' => Post::all(),
]);
}
// /blog/:slug — a single post (the route is set in config.php)
function showAction() {
$post = Post::find($_GET['slug'] ?? '');
if (!$post) {
http_response_code(404);
return 'Post not found';
}
return $this->render('show', [
'title' => $post['title'],
'post' => $post,
]);
}
}Templates
<h1>Blog</h1>
<?php foreach ($posts as $slug => $p): ?>
<p><a href="/blog/<?= $slug ?>"><?= htmlspecialchars($p['title']) ?></a></p>
<?php endforeach ?><h1><?= htmlspecialchars($post['title']) ?></h1>
<p><?= htmlspecialchars($post['text']) ?></p>
<p><a href="/blog">← All posts</a></p>Route
Without a route, /blog/hello-world would mean “module blog, controller helloworld”.
Let's add a custom rule — the part of the URL after the colon ends up in $_GET:
router::addCustomRoute('/blog/:slug', [
'module' => 'blog',
'controller' => 'index',
'action' => 'show',
]);Don't forget to add use olala\router; at the top of config.php. Open /blog — the post list works, and each post opens at its own URL.
What we've built
mysite/
├── index.php
├── config.php # default layout + /blog/:slug route
├── olala/ # the framework — leave it alone
└── site/
├── layouts/default/index.php
├── models/Post.php
└── modules/
├── index/ # /
├── about/ # /about
└── blog/ # /blog and /blog/:slugWhere to go next:
- Routing — all the URL parsing rules;
- Views and layouts — partials, multiple layouts;
- Models and database — connecting PostgreSQL or MySQL;
- Deployment — how to put your site on a server.