The alkimisti/simplerouter
package is a simple router for PSR-4 PHP applications.
In order to make composer to autoload application classes, we should add autoload
key and under psr-4
we set correlation between the namespace and actual folder name where our application files are stored.
We set App
namespace to correspond to App
folder.
{ "require": { ... "autoload": { "psr-4": {"App\\": "App/"} } }
The package can be installed via Composer:
composer require alkimisti/simplerouter:dev-main
You may download it from Gihub: https://github.com/Alkimisti/simplerouter
Inside index.php
we should add the following lines:
$router = new Router(); $router->resolve();
Example:
<?php require('vendor/autoload.php'); use alkimisti\simplerouter\Router; $router = new Router(); $router->resolve();
The contents of .htaccess
:
RewriteEngine on RewriteCond $1 !^(index\.php|assets) RewriteRule ^(.*)$ /index.php/$1 [L]
In the line 2, instead of assets
, you may add other folder names that need direct access, dividing them with |
.
The application should have following structure:

The router will deal only with App\Controllers
, where we will store our controller classes.
If the URL is /articles/show/5
, the router will instantiate the ArticlesController
class, invoke the show()
method, and pass 5
as its first parameter.
Controller classes should be concrete, while the methods should be public and non-static.
If the route is not found, the router will emit 404 error.
If the second URL segment is missing, the default method name will be index()
.
If first and second URL segments are missing, the default name for controller will be HomeController
, while the default name for the method will be index()
.
Therefore, for the route for home page, we should create HomeController
with a method named index()
.
<?php namespace App\Controllers; class HomeController { public function index() { // } }