lgrdev / simplerouter
A very simple php router
1.0.1
2023-11-01 05:55 UTC
Requires
- php: >=8.1.0
Requires (Dev)
- phpunit/phpunit: ^10.4
This package is auto-updated.
Last update: 2024-09-30 01:54:45 UTC
README
A very simple router for GET, POST, PUT, DELETE methods
Install
$ composer require lgrdev/simplerouter
Usage
$myrouter = new SimpleRouter(); // add route to home page $myrouter->addGet('/', function () { echo 'Hello, I\'m the home page'; } ); // route with a parameter $myrouter->addGet('/book/{id}', function ($id) { echo 'Hello, book #' . $id; } ); // route with a parameter id with format control $myrouter->addGet('/book/{id:[0-9a-f]+}', function ($id) { echo 'Hello, book #' . $id; } ); // route with a parameter and an optional parameter id2 $myrouter->addGet('/user/{id1}/{?id2}', function ($id1, $id2 = null) { echo 'Hello User ' . $id; } ); // add a route for the method DELETE $myrouter->addDelete('/book/{id:[0-9a-f]+}', function ($id) { echo 'Delete book #' . $id; } ); // add a POST route $myrouter->addPost('/book', function ($id) { echo 'Post a new book #'; } ); // display page $myrouter->run($_REQUEST['REQUEST_METHOD'], $_SERVER['REQUEST_URI']);