The discussions here are for Phraw, a micro framework for the PHP programming language.
You are not logged in.
Pages: 1
In the "develop" trunk there is a more stabilized and powerful routing feature.
Attention: this is a partial backward incompatible change. $phraw->request is now $phraw->uri_values. I chose to break compatibility because there are a lot of changes and new are coming. This is all alpha code, so don't use it on production.
Actually the route() method can handle full regular expressions, better regular expressions on parentheses than the previous alpha code and equal comparison.
Some people will discover that the regular expressions on parentheses algorithm is very similar to the Django one: yes it is
It is now possible to add custom route() algorithms from functions, classes and objects. This feature open new possibilities for different routing practices and complex behaviors like lookup pages on a database, CMSs, object-driven frameworks etc..
The default algorithm used is regular expressions.
Example of a regular expression for the URI "/":
<?php
# ...
if ($phraw->route('^$', 'rexp')) {
# ...
}
# ...
?>
Example of a regular expression for the URI "/foo/bar/" and "/foo/bar":
<?php
# ...
if ($phraw->route('^foo\/bar\/?$')) {
# ...
}
# ...
?>
Example of a regular expression on parentheses for the URI "/foo/bar/" that prints the value "bar":
<?php
# ...
if ($phraw->route('^foo/(?P<name>\w+)/$', 'prexp')) {
echo $phraw->uri_values['name']
}
# ...
?>
Example of a equal comparison for the URI "/foo/bar/":
<?php
# ...
if ($phraw->route('foo/bar/', 'equal')) {
# ...
}
# ...
?>
A custom route with a simple function:
<?php
# ...
function myroute(&$uri, &$uri_values) {
# ...
}
if ($phraw->route('foo/bar/', 'myroute')) {
# ...
}
# ...
?>
Offline
Today I pushed the full documentation for routing on the develop branch and a new method: tree_route().
tree_route() simplifies a lot the creation of hierarchical URI structures saving many lines of code.
This tool is very simple to use and similar to other frameworks. The benefits are many because it is fully configurable: it's easy to serialize, easy to modify after the creation, very easy to learn because there are not keywords or functions to remember, easy to extend because all the parameters are configurable and the matching algorithm can also be customizable.
With a simple array it is possible to create a structure, use default parameters and customize the behavior.
First a simple example:
<?php
#...
$tree_pages = array(
'' => array(null, 'index.html'),
'about/' => array(null, 'about.html'),
'contacts/' => array(null, 'contacts.html'),
'documentation/' => array(
array(
'' => array(null, 'documentation/index.html'),
'setup/' => array(null, 'setup.html'),
'guide/' => array(null, 'guide.html'),
'reference/' => array(null, 'reference.html'),
'faq/' => array(null, 'faq.html'),
)
)
);
if ($phraw->tree_route($static_pages, $values, 'equal')) { # Tree routing
$smarty->display($values[0]);
}
#...
?>
The keys are the URI to match; the values are arrays where the first value is for descendants, the other values are stored in the $values variable.
It is possible to use default values for parents and global values for the whole tree, in this example:
<?php
# ...
$tree_pages = array(
'' => array(null, 'page' => 'index.html', 'section' => 'main'),
'about/' => array(null, 'page' => 'about.html', 'section' => 'main'),
'contacts/' => array(null, 'page' => 'contacts.html', 'section' => 'contact'),
'other/' => array(null),
'documentation/' => array(
array(
'' => array(null, 'page' => 'documentation/index.html'),
'setup/' => array(null, 'page' => 'setup.html'),
'guide/' => array(null, 'page' => 'guide.html'),
'reference/' => array(null, 'page' => 'reference.html'),
'faq/' => array(null, 'page' => 'faq.html'),
'other/' => array(null)
), 'page' => 'documentation/default.html', 'section' => 'documentation'
)
);
$values = array('page' => 'default.html');
if ($phraw->tree_route($static_pages, $values, 'equal')) { # Tree routing
$smarty->assign('section' => $values['section']);
$smarty->display($values['page']);
}
# ...
?>
The "other/" URI will use the default page from the "$values".
The "documentation/other/" URI will use the default page from the "documentation/" parent.
All the "documentation/" descendants takes the "documentation" value for the "section" parameter.
Just a note: "$values" can be also an object attribute.
The tree structure is an array, so it can be easily serialized on a file (XML, JSON and so on).
Offline
Pages: 1