dev-master
9999999-devA small, lightweight php-router for creating clean, RESTful urls. Extremely easy to setup and configure.
MIT
The Requires
- php >=5.6
The Development Requires
by Erlend Ellingsen
Wallogit.com
2017 © Pedro Peláez
A small, lightweight php-router for creating clean, RESTful urls. Extremely easy to setup and configure.
, (*1)
An extremely small and lightweight PHP-router for clean, RESTful urls. Extremely easy to setup and requires no configuration. Just plain dead simple routing., (*3)
Supports parameters, different HTTP-methods and wildcard urls., (*4)
As the new maintainer I tried to implement a better more robust architecture without going away from the initial goal of the router which was to work off of if based control structures., (*5)
This may change in the future as the project evolves. Below is the reworked example of how I envision you using the router in its new, unit tested, form., (*6)
composer require erlendellingsen/flex-router
URL-rewrite must be enabled on the target server. This guide consists of an example on how to set it up on Apache-servers. If you do have IIS, Nginx or other examples, then please contribute!, (*7)
Configuration, (*8)
mod_rewrite must be enabled.AllowOverride all
.htaccess-file, (*9)
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^?]*)$ index.php?path=$1 [NC,L,QSA]
You somehow have to enable rewriting of urls in your web server/directory to point all requests to index.php?path=x.
If you have somewhat idea how, but need assistance then don't hesitate to reach out., (*10)
If you do have an example for other web servers then please consider getting in touch. (Open an issue, a fork, or just email me). Contributions are very much appreciated ☺️, (*11)
The example beneath demonstrates how to setup flexRouter., (*12)
<?php
// Load the namespaces
use FlexRouter\FlexRouter;
use FlexRouter\Utilities\FlexResolver;
// Load in composer's autoloader
require_once 'vendor/autoload.php';
// Create a new router instance
$router = new FlexRouter();
/* Register your routes
Register Method | Request Method | Route | Route Id */
$router->registerRoute('GET', '/', 'homepage'); // Can pass single method
$router->registerRoute(['GET', 'POST'], '/test', 'test'); // Or an array of methods
$router->registerRoute('GET', '/test/:id/post', 'param'); // You can create URL params
$router->registerRoute('GET', '/asdf*', 'wildcard'); // Or Wildcard Params
// Create your resolver
$requestMethod = $_SERVER['REQUEST_METHOD']; // Can be attained from either globals or request object
$requestUri = $_SERVER['REQUEST_URI']; // Can be attained from either globals or request object
$resolver = new FlexResolver($requestMethod, $requestUri, $router);
// Start resolving your routes in whatever way you would like,
if ($resolver->resolve('homepage')) {
echo 'The homepage route was found';
return;
}
if ($resolver->resolve('test')) {
echo 'The nested test route was found';
return;
}
if ($resolver->resolve('param')) {
echo "The param test route was found\r\n";
echo $resolver->access()->params('url', 'id');
return;
}
if ($resolver->resolve('wildcard')) {
echo 'The wildcard test route was found';
return;
}
// This is the 404 catch all
$resolver->notFound(function () {
echo 'Nothing found';
return;
});
Want to contribute? Lovely! Contributions are very much appreciated. There are a ton of things to do in this project, but I don't have the time do to it. (At least not all)., (*13)
Areas where contribution is deeply needed, (*14)
And probably much much more., (*15)
MIT Copyright Erlend Ellingsen 2017. See LICENSE., (*16)
A small, lightweight php-router for creating clean, RESTful urls. Extremely easy to setup and configure.
MIT