lawondyss / router
Small PHP library for creating app routes. Using only at for your own risk, this is for my educational purposes.
Requires
- php: >=7.4
- nette/utils: 3.1.*
Requires (Dev)
- nette/http: 3.0.*
- tracy/tracy: 2.7.*
This package is auto-updated.
Last update: 2024-10-17 21:31:41 UTC
README
Simply PHP library to create REST API.
Requirements
Strictly requires PHP 7.4
Install
Via Composer: composer require lawondyss/router
Usage
Simply instantiate.
use Lawondyss\Sandy\Router\Router; require_once __DIR__ . '/vendor/autoload.php'; $router = new Router;
No static. Why not?
Defining routes
Add routes for HTTP method(s) and URL path (as mask). Callback can processing request and editing response.
// predefined HTTP method $router->get('/', function (Request $request, Response $response) { $response->body = 'Hello world'; }); // predefined HTTP method $router->post('/foo/bar', function(Request $request, Response $response) { $response->code = Response::S201_CREATED; }); // own definition of valid HTTP methods (PUT and PATCH) $router->add(Router::PUT | Router::PATCH, '/foo/bar', function (Request $request, Response $response) { $response->code = Response::S204_NO_CONTENT; $response->addHeader('X-Lipsum-Message', 'Lorem ipsum dolor sit amet'); });
Parameters in mask
For catching segments of URL use {
and }
.
Optional segments of URL is in between [
and ]
.
Definition of regex validation for catching segment is after :
.
All catching segments is in Request::$params
.
$router->get('/foo/{bar:\d+}[/{baz:[a-z]+}]', function(Request $request, Response $response) { $response->contentType = 'application/json'; $response->body = json_encode($request->params); });
Motivation
Mainly for my educational purposes.
License
The MIT License (MIT). Please see License File for more information.