Stars: 395
Forks: 135
Pull Requests: 12
Issues: 57
Watchers: 31
Last Updated: 2022-10-12 18:05:35
This is a simple and small single class PHP router that can handel the whole url routing for your project.
License: MIT License
Languages: PHP, Dockerfile
Hey! This is a simple and small single class PHP router that can handle the whole URL routing for your project. It utilizes RegExp and PHP's anonymous functions to create a lightweight and fast routing system. The router supports dynamic path parameters, special 404 and 405 routes as well as verification of request methods like GET, POST, PUT, DELETE, etc. The codebase is very small and very easy to understand. So you can use it as a boilerplate for a more complex router.
Take a look at the index.php file. As you can see the Route::add()
method is used to add new routes to your project.
The first argument takes the path segment. You can also use RegExp in there to parse out variables.
All matching variables will be pushed to the handler method defined in the second argument.
The third argument will match the request method. The default method is 'get'.
// Require the class
include 'src\Steampixel\Route.php';
// Use this namespace
use Steampixel\Route;
// Add the first route
Route::add('/user/([0-9]*)/edit', function($id) {
echo 'Edit user with id '.$id.'<br>';
}, 'get');
// Run the router
Route::run('/');
You will find a more complex example with a build in navigation in the index.php file.
Just run composer require steampixel/simple-php-router
Than add the autoloader to your project like this:
// Autoload files using composer
require_once __DIR__ . '/vendor/autoload.php';
// Use this namespace
use Steampixel\Route;
// Add your first route
Route::add('/', function() {
echo 'Welcome :-)';
});
// Run the router
Route::run('/');
If your script lives in a subfolder (e.g. /api/v1) set this basepath in your run method:
Route::run('/api/v1');
Do not forget to edit the basepath in .htaccess too if you are on Apache2.
You don't have to use echo
to output your content. You can also use the return
statement. Everything that gets returned is echoed automatically.
// Add your first route
Route::add('/', function() {
return 'Welcome :-)';
});
Since PHP 7.4 you can also use arrow functions to output your content. So you can easily use variables from outside and you can write shorter code.
Please be aware that an Arrow function must always return a value. Therefore you cannot use echo
directly in here.
You can find an example in index.php. However, this is deactivated, as it only works from PHP 7.4.
Route::add('/arrow/([a-z-0-9-]*)', fn($foo) => 'This is a working arrow function example. Parameter: '.$foo );
This is useful, for example, to automatically generate test routes or help pages.
$routes = Route::getAll();
foreach($routes as $route) {
echo $route['expression'].' ('.$route['method'].')';
}
On top of that you could use a library like https://github.com/hoaproject/Regex to generate working example links for the different expressions.
The second, third and fourth parameters of Route::run('/', false, false, false);
are set to false by default.
Using this parameters you can switch on and off several options:
run()
method and in your .htaccess file.a2enmod apache2
AllowOverride All
option is set in the Apache2 configuration like in this example:<VirtualHost *:80>
ServerName mysite.com
DocumentRoot /var/www/html/mysite.com
<Directory "/var/www/html/mysite.com">
AllowOverride All
</Directory>
</VirtualHost>
This is a simple router. So there is no templating at all. But it works perfectly together with simplePHPComponents and simplePHPPortals. There is a complete boilerplate project including these dependencies and this router called simplePHPPages. You can use it for you next project.
Please be aware that for this router you need a basic understanding of PHP. Many problems stem from people lacking basic programming knowledge. You should therefore have the following skills:
Please note that we are happy to help you if you have problems with this router. Unfortunately, we don't have a lot of time, so we can't help you learn PHP basics.
I have created a little Docker test setup.
Build the image: docker build -t simplephprouter docker/image-php-7.2
Spin up a container
docker run -d -p 80:80 -v $(pwd):/var/www/html --name simplephprouter simplephprouter
docker run -d -p 80:80 -v %cd%:/var/www/html --name simplephprouter simplephprouter
Open your browser and navigate to http://localhost
With IIS now fully supporting PHP, this example can be run using the included web.config. The web.config has a rewrite rule, similar to the .htaccess rewrite rule, but specifically for IIS. The rewrite rule will send all incoming requests to index.php in your root. The rest is done by the simple php router.
This setup tutorial assumes you have the knowledge to create sites in IIS and set up bindings for http/https and custom DNS. If you need more information, this article will help you with that part.
This project is licensed under the MIT License. See LICENSE for further information.