A typical index.php file will look like this:
<?php require_once('fapp/FApp.php'); FApp::register_controllers_dir(__DIR__.'/controllers'); FApp::register_templates_dir(__DIR__.'/templates'); FApp::route('/([0-9A-Za-z]+)', 'HomeController:page'); FApp::route('/', 'HomeController:index'); FApp::route(null, 'HomeController:default'); FApp::start();
The first line (require_once) is quite obvious, FApp::register_controllers_dir() and FApp::register_templates_dir() register the controllers and templates directories, assuming in this example that these are located at the root level of the web site; then we have the routes definitions, that we'll explain later, and finally the FApp::start() command.
The Apache VHost definition will look like this:
<VirtualHost *:80> ServerName www.example.com DocumentRoot /var/www <Directory "/var/www/"> AllowOverride All Require all granted RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ index.php [QSA,L] </Directory> </VirtualHost>
Now let's have a look at the directory structure, under the web site root level:
/fapp/ FApp.php /controllers/ HomeController.php /templates/ index.html page.html index.php
You already know that routes are defined in the index.php files, we use the following syntax:
FApp::route('<route pattern>', '<controller class name>:<controller method name>');
The pattern is a classic posix regex pattern without the enclosing characters and modifiers, here are some examples:
FApp::route('/([0-9A-Za-z]+)', 'HomeController:page'); FApp::route('/([0-9A-Za-z]+)/([0-9A-Za-z]+)', 'HomeController:page'); FApp::route('/\?token=([0-9A-Za-z]+)', 'HomeController:handle_token'); // I know, this one is weird FApp::route('/page=([0-9A-Za-z]+)', 'HomeController:page'); FApp::route('/pages/([0-9A-Za-z]+)', 'HomeController:page');
Let's keep the latest one, the matching controller will look like:
<?php namespace Controllers; class HomeController { public function page () { $route_params = \FApp::route_params(); $req_pageid = $route_params[0]; $options = array(); $options['content'] = SomeFunctionCallReturningThePageContent($req_pageid); echo \FApp::render('page.html', $options); exit; } }
And the page.html template:
<html> <body> {{content}} </body> </html>
Of course this template is minimalist to be easily readable, so we can see that the content option passed to the FApp::render function is displayed inside the body section of the web page.