openclerk / routing
There is no license information available for the latest version (0.1.0) of this package.
A library for simple routing in PHP
0.1.0
2017-09-11 07:41 UTC
Requires
- openclerk/config: ^0.1
Requires (Dev)
- phpunit/phpunit: >=4.0
- soundasleep/component-tests: dev-master
This package is auto-updated.
Last update: 2024-11-14 09:59:19 UTC
README
A library for simple routing in PHP.
Using
This project uses openclerk/config for config management.
First configure the component with site-specific values:
Openclerk\Config::merge(array( "absolute_url" => "http://localhost/path/", ));
Add a simple router file:
<?php // router.php require(__DIR__ . "/../inc/global.php"); $path = require_get("path", "security/login/password"); try { \Openclerk\Router::process($path); } catch (\Openclerk\RouterException $e) { header("HTTP/1.0 404 Not Found"); echo htmlspecialchars($e->getMessage()); } ?>
Add a .htaccess
that translates paths to this router:
RewriteEngine on
# Forbid access to any child PHP scripts
RewriteRule ^([^\.]+)/([^\.]+).php$ - [F]
RewriteRule ^([^\.]+)$ router.php?path=$1 [L,QSA]
Define site routes:
// set up routes \Openclerk\Router::addRoutes(array( "security/login/password" => "security/login.php?type=password", "security/login/:key" => "security/login-:key.php?type=:key", // by default any unmatched routes will require <module>.php ));
Now you can use url_for()
and absolute_url_for()
:
<a href="<?php echo htmlspecialchars(url_for('security/login/password')); ?>">Login with password</a>
Renderable objects
You can also pass along an object with a render($args)
method, which will be called instead:
class MyApi { function render($args) { // $args = array('code' => ...) } } \Openclerk\Router::addRoutes(array( "api/currency/:code" => new MyApi(), ));
Tests
composer update --dev vendor/bin/phpunit
TODO
- Actual documentation
- Tests
- Publish on Packagist