https://github.com/jenstornell/tinyrouter
TinyRouter is perhaps the smallest PHP router library on earth
https://github.com/jenstornell/tinyrouter
php php-router php-router-standalone php-routing router routing
Last synced: about 1 month ago
JSON representation
TinyRouter is perhaps the smallest PHP router library on earth
- Host: GitHub
- URL: https://github.com/jenstornell/tinyrouter
- Owner: jenstornell
- License: mit
- Created: 2018-10-01T05:45:45.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2020-05-09T12:58:07.000Z (about 5 years ago)
- Last Synced: 2025-04-12T02:04:02.463Z (about 1 month ago)
- Topics: php, php-router, php-router-standalone, php-routing, router, routing
- Language: PHP
- Homepage:
- Size: 19.5 KB
- Stars: 7
- Watchers: 2
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: readme.md
- Changelog: changelog.md
- License: license
Awesome Lists containing this project
README
# TinyRouter
*Version: 2.0*
Probably the smallest PHP router library on earth.
## Files
### `.htaccess`
Add the `.htaccess` (code below).
```htaccess
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
'.*',
':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
```php
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
- `:all` matches everyting from current position until the end `.*`
- `:alpha` matches any alphabetically character `[a-zA-Z]+`
- `:any` matches anything within slashes `[^/]+`
- `:num` matches any number, even negative ones `\d+|-\d+`The below will match for examlpe `blog/2010/01/my-story`.
```php
route('blog/:num/:num/:any', function($matches) {
// Return something
});
```### Custom patterns
The below will match for examlpe `assets/my/images/picture.jpg`.
```php
route('assets/(.*)\.(jpg|jpeg|png|gif|svg)$', function($matches) {
// Return something
});
```## Donate
Donate to [DevoneraAB](https://www.paypal.me/DevoneraAB) if you want.
## Requirements
- PHP 7
- Rewrite module enabled## License
MIT