Skip to content

Lawondyss/Router

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Router

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.

About

Simply PHP library to create REST API.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages