lukevear / tbone
An extremely fast micro PHP router
Installs: 26
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:package
Requires
- php: >=5.4.0
Requires (Dev)
- phpunit/phpunit: ~4.8
- satooshi/php-coveralls: 0.7.*
This package is not auto-updated.
Last update: 2020-08-16 11:36:36 UTC
README
A very simple, very fast PHP router.
- Exceptionally easy to use
- Supports OPTIONS, HEAD, GET, POST, PUT, PATCH and DELETE requests
- Simple event system for error handling
Note: TBone does not support route parameters (such as /customers/{id})
When to use TBone
TBone is designed to be used in in very simple, relatively static PHP applications where your application logic can sit within a set of closures. If you try to use TBone for anything beyond simple sites, you're going to have a bad time. If you're after something more impressive I would suggest nikic/FastRoute.
Installation
composer require lukevear/tbone
Activating the router
You may use TBone wherever you feel is appropiate in your project, however the most common usage would be in an index.php file in your document directory. If running from an index.php file, you should have a .htaccess file similar to the one below.
<IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^ index.php [L] </IfModule>
Basic Example
use TBone\TBone; $router = new TBone; // Add a GET route $router->get('/', function() { echo 'Welcome to my homepage'; }); // Add a POST route $router->post('/contact-us', function() { echo 'Thanks for your submission'; }); // Run the router $router->dispatch();
Event System
TBone's event system exists to provide a mechanism for you to handle routing related events. TBone supports ROUTE_DISPATCH_REQUESTED
(fired when dispatch() is called), ROUTE_NOT_FOUND
(fired when a route cannot be matched) and ROUTE_DISPATCHED
(fired when a route is matched and the callback has been run).
When an event is fired the specified callback will be run.
use TBone\TBone; use TBone\TBoneEvent; $router = new TBone; // Add a GET route $router->get('/', function() { echo 'Welcome to my homepage'; }); // Register our 404 page $router->addHandler(TBoneEvent::ROUTE_NOT_FOUND, function() { http_response_code(404); echo 'Sorry, that page doesn\'t exist!'; }); // Run the router $router->dispatch();