TinyRouter
TinyRouter is perhaps the smallest PHP router library on earth
Install / Use
/learn @jenstornell/TinyRouterREADME
TinyRouter
Version: 2.0
Probably the smallest PHP router library on earth.
Files
.htaccess
Add the .htaccess (code below).
Options -Indexes
RewriteEngine on
# RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php [L]
tinyrouter.php
Add the tinyrouter.php to a folder (code below).
<?php
class route
{
public static function __callStatic($method, $args)
{
if ($_SERVER['REQUEST_METHOD'] == strtoupper($method)) {
$presets = [
':all' => '.*',
':alpha' => '[a-zA-Z]+',
':any' => '[^/]+',
':num' => '\d+|-\d+',
];
$pattern = $args[0];
foreach ($presets as $shortcode => $regex) {
$pattern = strtr($pattern, [$shortcode => '(' . $regex . ')']);
}
$pattern = '~^' . $pattern . '$~';
$uri = '/';
$dir = dirname($_SERVER['SCRIPT_NAME']);
if (isset($_SERVER['REDIRECT_URL']) && $dir != '/') {
$uri = strtr($_SERVER['REDIRECT_URL'], [$dir => '']);
}
preg_match($pattern, $uri, $matches);
if (!$matches) return;
$output = $args[1]($matches);
if (isset($output)) die($output);
}
}
}
function route($pattern, $method)
{
route::get($pattern, $method);
}
Usage
include __DIR__ . '/tinyrouter.php';
route::post('form/:any', function($matches) {
// Will only run with POST requests. You can PUT or whatever.
});
route(':all', function($matches) {
// Will only run with GET requests
});
// When no match is found it will fall down here
header("HTTP/1.0 404 Not Found");
die('Error 404 - No route could be found');
Patterns
Predefined patterns
:allmatches everyting from current position until the end.*:alphamatches any alphabetically character[a-zA-Z]+:anymatches anything within slashes[^/]+:nummatches any number, even negative ones\d+|-\d+
The below will match for examlpe blog/2010/01/my-story.
route('blog/:num/:num/:any', function($matches) {
// Return something
});
Custom patterns
The below will match for examlpe assets/my/images/picture.jpg.
route('assets/(.*)\.(jpg|jpeg|png|gif|svg)$', function($matches) {
// Return something
});
Donate
Donate to DevoneraAB if you want.
Requirements
- PHP 7
- Rewrite module enabled
License
MIT
Related Skills
node-connect
339.3kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
83.9kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
339.3kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
commit-push-pr
83.9kCommit, push, and open a PR
