URI Router

All the routing is now done via the settings in the router.conf, the idea was "to decoupling of [the] serve logic from Apache – to be server-agnostic".

The whole idea is to take URI path, _GET/_PUT/_SERVER data and other meta-data, then process rules in this file sequentially, which results in dispatched handler to process query, and all arguments extracted from URI for further usage by those handlers (e.g. you can extract parts of URI to _GET vars, etc.).

1. 'Language' guide

Every line of code consists of regex which matched against URI, and actions, all separated by whitespace. there are no possibility to include whitespace in regex or action. Regex will be matched against URI, on success all actions executed, on failure – we go for next regex. Lines without regex is continuations for same regex, and will be executed sequentially if regex matched.

Every single action can succeed or fail. if any one fails – all variable assignments made by THIS line (even before failed action) is discarded, and we go to next line.

Two control actions exist (all action line must succeed for them to act!)

_next! — jump to next regex (skip next action lines with empty regex)
_ok! — search terminated with success

If no _ok! is executed ever – search fails and 404 is emitted.

That's all on control flow.

1.1. Main regex

PCRE regex syntax — usual php preg_* regex syntax apply, including delimiters and options after trailing delimiter. Convenience macros (defined by 'define' line, or supplied by WackoWiki) expanded before matching. Used as {macro} – to be referenced as $1-vars then, or {var=macro} – for inline assignment. Macros cannot be used in ~-regexes.


$0..$9 – fields matched by main regex. $0 is complete match, $1 and later – corresponding (...) parts
$a..$j – as $0..$9 but set by sub-matched (by ~ operator) patterns
others – local variables

Predefined vars:

_tls1 or 0, tls session
_uri – parsed URI (it is matched against main regex, but can be changed by assignment)
_rewrite1 or 0, mod_rewrite active

Usage of undefined variables considered a failure (if not masked by @ in VALUE expansion, see later)

Similar format used for all actions (not all fields apply for every action, and just ignored):


Value is a string, with expanded variables. expansions:

$0 .. $9 / $a .. $j — see above
@$... format can be used to mask undefined variable error
$$ – replaced by $
$@ – replaced by @


FUNCTIONs can be used: tolower | toupper | int
var?=$2 — assign if not set
var! == var=1
dbg=$1,$2,$3Ut::dbg printer ;)

Pattern matching:

var/regex/i — sets $a..$j on success
varhashid:[1-9] — hashid expansion, into $a...


FUNCTIONS can be used: int — both args converted to int before comparison
var==12 var!=12 var:int<12 var>12 var<=12 var>=12


var? — isset
var- — unset

2. route.conf

//define	{method}	name|name|name		// predefined by wackowiki
define		{hashid}	[0-9a-zA-Z]+
define		{i}			[0-9]+
define		{h}			[0-9a-fA-F]+
define		{a}			[0-9a-zA-Z]+
define		{w}			[\w]+
define		{}			[^/]*
define		{*}			.*?
define		{**}		.*

//`^{hashid}$`		$1~hashid:2 Gone=$a Gtwo=$b all=$0+${Gone} _ok! _tls!=0 _method~/g(e)t/i BIN:tolower=$b Pln=${_line}
//				desc=$0 term:tolower=MyMethod term!= Gmethod=show _ok!
//				Op!~/^diff$/i &next!
//				$1~hashid:2 Gone=$a Gtwo=$b Gmethod:tolower=${Op} _ok! // test

3. Rewrite

Mode rewrite is now set in the Settings class at runtime, regardless what you set in the config, this follows again the server-agnostic idea.

// if .htaccess tell us actual info on mod_rewrite status - use it
if (getenv('HTTP_MOD_ENV') === 'on')
	$this->rewrite_mode = (getenv('HTTP_MOD_REWRITE') === 'on');

3.1. The .htaccess file

# No user serviceable parts inside
# If you want to fix anything by tuning htaccess - you're possibly on the wrong path

<IfModule mod_env.c>

<IfModule mod_rewrite.c>
	<IfModule mod_env.c>
	RewriteEngine on
	RewriteRule ^ index.php [QSA,L]

<IfModule !mod_rewrite.c>
<FilesMatch \.php$>
	# Apache 2.4
	<IfModule mod_authz_core.c>
		Require all denied
	# Apache 2.2
	<IfModule !mod_authz_core.c>
		Order Allow,Deny
		Deny from all

<FilesMatch "^(admin|index)\.php$">
	# Apache 2.4
	<IfModule mod_authz_core.c>
		Require all granted
	# Apache 2.2
	<IfModule !mod_authz_core.c>
		Order Allow,Deny
		Allow from All
		Deny from None

3.2. Example

  • httpd.conf LoadModule rewrite_module modules/mod_rewrite.so
  • config,php 'base_url' => 'http://localhost/wacko/',
  • page tag: RecentlyCommented

3.2.1. ON

 LoadModule rewrite_module modules/mod_rewrite.so

result: http://localhost/wacko/RecentlyCommented

3.2.2. OFF

 #LoadModule rewrite_module modules/mod_rewrite.so

result: http://localhost/wacko/?page=RecentlyCommented

It works without index.php/ in base_url, the URI router appends ?page= if mode_rewrite is OFF.