-
Notifications
You must be signed in to change notification settings - Fork 3
Wildcards
Stein Janssen edited this page Feb 24, 2016
·
1 revision
The following options exist
- a: (alfabetic chars only)
- n: (numbers only)
- an: (alfanumeric chars only)
- w: (alfanumeric, dash and underscore only)
- ?: (optional parameters) - must be last part of the url
- *: (lazy loading) - must be last part of the url
How to use
// In this case the id may be a number
$router->add('/user/{n:id}', 'GET', 'App\Controllers\UserController::show');
// In this case the id may only contain alfabetic chars or numbers (or both)
$router->add('/user/{an:id}', 'GET', 'App\Controllers\UserController::show');
// Now we want everything behind docs/ in the page variable
// For example when we go to the url /docs/user/edit we will receive user/edit in the page variable
$router->add('/docs/{*:page}', 'GET', function($page) {
// do something with $page
});
// Optional parameter example
$router->add('/hello/{a:name}/{?:lastname}', 'GET', function($name, $lastname = null) {
// check if lastname is provided
// if ($lastname) {...}
})
Simple PHP Router
Getting started - Usage - Wildcard options - Changes