mark-veres / bedouin
A file-based PHP routing system.
Installs: 9
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/mark-veres/bedouin
README
bedouin
A file-based PHP routing system.
Two files:
cartograph.phpscript that generates the map filebedouin.phpscript that routes according to the map file
installation
composer require mark-veres/bedouin
example setup
# index.php require_once "bedouin.php"; $router = new \Bedouin\Router; $router->loadMap("map.json"); $route = $router->currentRoute(); if (isset($route->file)) include $route->file;
# gen_map.php require_once "./cartograph.php"; $cart = new \Bedouin\Cartograph; $cart->root_dir = __DIR__; $cart->route_folders = ["./routes", "./api"]; $cart->printMap("map.json");
usage
- copy the
bedouin.phpandcartograph.phpfiles in the desired directory - create the
routesandstaticdirectory - create an
index.phpfile will handle all requests - redirect all requests to
index.php - create another PHP file that will use the cartograph API to generate the map
directory structure
| file | url | method |
|---|---|---|
/routes/index.php |
/ |
all |
/routes/index.get.php |
/ |
get |
/routes/about.php |
/about |
all |
/posts/index.php |
/posts |
all |
/posts/[slug]/index.php |
/posts/test/posts/bla-bla |
all |
/posts/[slug]/new.post.php |
/posts/test/new |
post |
404 pages
- create a
404.phpfile in theroutesfolder - this file does not support custom HTTP methods
static files
- put all your static files in the
staticdirectory - access these files at the
/static/*url - file names are case- and extension-sensitive
accessing route parameters
- given a route
/posts/[slug] - and an url
/posts/test
$router = new \Bedouin\Router; // ... print_r($router->params); /* Array ( [slug] => test ) */
middleware
- create a file ending in
.mw.phpor.mw.get.php(or any HTTP method for that matter) - the middleware will be "bound" to the index handler of the directory it is placed in
Note
coming soon:
- map splits (performance optimization when dealing with many routes)
- templates
Tip
Redirecting all requests to bedouin.php on an Apache server.
RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ index.php
Tip
Redirecting all requests to bedouin.php with the PHP built-in server
php -S localhost:8080 bedouin.php
Tip
Regenerating the map by accessing a specific route.
# index.php require_once "bedouin.php"; if ($_SERVER["REQUEST_URI"] == "/your/custom/path") { require_once "./cartograph.php"; $cart = new \Bedouin\Cartograph; $cart->printMap("map.json"); } $router = new \Bedouin\Router; $router->loadMap("map.json"); $route = $router->currentRoute(); if (isset($route->file)) include $route->file;
