This is a simple PHP Router Class. It requires no dependencies. Fork it and have fun with it.
It's a standalone single file PHP class to use on your projects. It requires no dependencies or no framework.
You only need two files:
- 1
Router.php
- 2
.htaccess
The .htaccess
redirects all requests to index.php
.
You can run everything from the index.php file, see the file for usage.
Create a router instance
require "Router.php"
$router = new Router();
Add routes with add
method:
$router->add('GET', '/', function(){
// Code goes here
});
You can directly call specific methods:
Only supports get
, post
, put
, patch
, delete
right now
$router->get('/', function(){
// Code goes here
});
$router->post('/create', function(){
// Code goes here
});
Finish routing with listen()
method
$router->listen();
You can set URI params of any type just by using ":"
before URI name
$router->get('/user/:id', function($params){
// Code goes here
echo $params['id'];
});
require 'Router.php';
$router->get('/', function(){
// Code goes here
});
$router->post('/create', function(){
// Code goes here
});
$router->get('/user/:id', function($params){
// Code goes here
echo $params['id'];
});
$router->listen();