Little gallery of small examples

The smallest example


/* File: index.php */
<?php
# Load Phraw
require_once('phraw/phraw.php');
$phraw = new Phraw();

# Routing
if ($phraw->route('')) {
    echo 
'Hello world!';
}
?>

Templating and a default 404 error page

With Smarty, the suggested template engine:


/* File: index.php */
<?php
# Load Phraw
require_once('phraw/phraw.php');
$phraw = new Phraw();

# Load the Smarty extension
require_once('phraw/extensions/smarty.php');
$smarty = new SmartyTemplateEngine();

# Routing
if ($phraw->route('')) {
    
$smarty->display('hello.html');
} else {
    
$smarty->display_error();
}
?>

With RainTPL, the faster but simpler template engine:


/* File: index.php */
<?php
# Load Phraw
require_once('phraw/phraw.php');
$phraw = new Phraw();

# Load the Smarty extension
require_once('phraw/extensions/raintpl.php');
$raintpl = new RaintplTemplateEngine();

# Routing
if ($phraw->route('')) {
    
$raintpl->draw('hello');
} else {
    
$raintpl->display_error();
}
?>

Route a bulk of static pages


/* File: index.php */
<?php
# Load Phraw
require_once('phraw/phraw.php');
$phraw = new Phraw();

# Load the Smarty extension
require_once('phraw/extensions/smarty.php');
$smarty = new SmartyTemplateEngine();

# Static pages
$static_pages = array(
    
'' => 'home.html',
    
'hello' => 'hello.html',
    
'more' => 'about/more.html'
);

# Routing
if ($phraw->bulk_route($static_pages$page)) {
    
$smarty->display($page);
} else {
    
$smarty->display_error();
}
?>

Templating with a dynamic view function


/* File: index.php */
<?php
# Load Phraw
require_once('phraw/phraw.php');
$phraw = new Phraw();

# Load the Smarty extension
require_once('phraw/extensions/smarty.php');
$smarty = new SmartyTemplateEngine();

# View function
function hello_view() {
    
# ... your dynamic code here
    
$smarty->display('hello.html');
}

# Routing
if ($phraw->route('')) {
    
hello_view();
} else {
    
$smarty->display_error();
}
?>

The view function can be placed outside the index.php file. It is suggest to place it inside the resources/ directory.

Dynamic and static pages


/* File: index.php */
<?php
# Load Phraw
require_once('phraw/phraw.php');
$phraw = new Phraw();

# Load the Smarty extension
require_once('phraw/extensions/smarty.php');
$smarty = new SmartyTemplateEngine();

# View function
function home_view() {
    
# ... your dynamic code here
    
$smarty->display('home.php');
}

# Routing
if ($phraw->route('')) {
    
home_view();
} else if (
$phraw->route('hello\/world')) { # Regular expression matching for the URI "/hello/world/"
    
$smarty->display('hello.html');
} else {
    
$smarty->display_error();
}
?>

It is possible to use shortcut functions for similar routing page types. See more on the documentation.

Get parameters from URLs


/* File: index.php */
<?php
# Load Phraw
require_once('phraw/phraw.php');
$phraw = new Phraw();

# Load the Smarty extension
require_once('phraw/extensions/smarty.php');
$smarty = new SmartyTemplateEngine();

# View function
function news_view() {
    
$news_id $phraw->request['news_id'];
    
# ... your dynamic code here
    
$smarty->assign('news'$news);
    
$smarty->display('news.php');
}

# Routing
if ($phraw->route('')) {
    
$smarty->display('home.php');
} else if (
$phraw->route('news\/(?P<news_id>\d+)')) { # Regular expression
    
news_view();
} else {
    
$smarty->display_error();
}
?>

Sessions

Create a directory, writable by PHP, for session files (in the example it is '/home/user/tmp').


/* File: index.php */
<?php
# Load Phraw
require_once('phraw/phraw.php');
$phraw = new Phraw();

# Sessions
require_once('lib/phraw/extensions/sessions_files.php');
$phraw->session_start('SessionFilesHandler''/home/user/tmp');

# Set a session variable
$_SESSION['foo'] = 'bar';

# ...

# Use the session variable
echo $_SESSION['foo'];
?>