rockberpro / rosa-router
Smart REST router for PHP
Installs: 6
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/rockberpro/rosa-router
Requires
- php: >=7.4
- ext-json: *
- monolog/monolog: ^2.10
- react/http: ^1.11
- symfony/dotenv: 3.4
- symfony/http-foundation: ^7.3
Requires (Dev)
- phpunit/phpunit: ^12.4
- symfony/var-dumper: ^7.3
- dev-main
- v1.17.7
- v1.17.6
- v1.17.5
- v1.17.4
- v1.17.3
- v1.17.2
- v1.17.1
- v1.17.0
- v1.16.5
- v1.16.4
- v1.16.3
- v1.16.2
- v1.16.1
- v1.16.0
- v1.15.1
- v1.15.0
- v1.14.9
- v1.14.8
- v1.14.7
- v1.14.6
- v1.14.5
- v1.14.4
- v1.14.3
- v1.14.2
- v1.14.1
- v1.14.0
- v1.13.3
- v1.13.2
- v1.13.1
- v1.13.0
- v1.12.1
- v1.12.0
- v1.11.1
- v1.11.0
- v1.10.4
- v1.10.3
- v1.10.2
- v1.10.1
- v1.10.0
- v1.5.7
- v1.5.6
- v1.5.5
- v1.5.4
- v1.5.3
- v1.5.2
- v1.5.1
- v1.5.0
- v1.4.9
- v1.4.8
- v1.4.7
- v1.4.6
- v1.4.5
- v1.4.4
- v1.4.3
- v1.4.2
- v1.4.1
- v1.4.0
- v1.3.7
- v1.3.6
- v1.3.5
- v1.3.4
- v1.3.3
- v1.3.2
- v1.3.1
- v1.3.0
- v1.2.2
- v1.2.1
- v1.2.0
- v1.0.8
- v0.1.7
- v0.1.6
- v0.1.5
- v0.1.4
- v0.1.3
- v0.1.2
- v0.1.1
- v0.1.0
This package is not auto-updated.
Last update: 2025-11-01 02:39:59 UTC
README
Introduction
ROSA-Router is a lightweight and efficient REST API engine built using PHP. It is designed to handle HTTP requests and route them to the appropriate controllers or functions based on the defined API endpoints. With a focus on simplicity and performance, ROSA-Router enables developers to quickly create and deploy RESTful web services.
Key Features
- Easy Routing System: Define routes for your REST API with simple configurations.
- Request Method Handling: Supports various HTTP methods such as
GET,POST,PUT,PATCHandDELETE - Error Handling: Built-in error handling mechanisms to gracefully manage exceptions and invalid requests.
- Lightweight and Fast: Optimized for performance, making it ideal for projects that require fast and efficient REST APIs.
How It Works
ROSA-Router listens for HTTP requests and maps them to the correct route handler based on the request's method and URI. It supports both static and dynamic routes and is fully customizable to fit different project needs.
Setup example
- index.php
<?php use Rockberpro\RestRouter\Bootstrap; require_once "vendor/autoload.php"; require_once "routes/api.php"; Bootstrap::execute(Bootstrap::MODE_STATELESS);
- In case your index.php entrypoint is already in use, tweak it as shown below:
<?php use Rockberpro\RestRouter\Bootstrap; use Rockberpro\RestRouter\Core\Server; require_once "vendor/autoload.php"; if (Server::getInstance()->isApiEndpoint()) { require_once "routes/api.php"; Bootstrap::execute(Bootstrap::MODE_STATELESS); }
- server.php
// To run the server: php server.php <?php use Rockberpro\RestRouter\Utils\DotEnv; use Rockberpro\RestRouter\Bootstrap; use React\Socket\SocketServer; use React\Http\HttpServer; require_once "vendor/autoload.php"; require_once "routes/api.php"; Bootstrap::setup(); $port = DotEnv::get('API_SERVER_PORT'); $address = DotEnv::get('API_SERVER_ADDRESS'); $server = new HttpServer(Bootstrap::execute(Bootstrap::MODE_STATEFUL)); $server->on('error', function (Throwable $e) { print("Request error: " . $e->getMessage().PHP_EOL); }); $socket = new SocketServer("{$address}:{$port}"); $server->listen($socket); print("Server running at http://{$address}:{$port}".PHP_EOL);
Usage examples
/ ** GET route * / Route::get('/post/{post}/comment/{comment}', [ PostController::class, 'get' ]); / ** GET route * / Route::get('/user/{id}', [ UserController::class, 'get' ]); / ** POST route * / Route::post('/user', [ UserController::class, 'post' ]); / ** PUT route * / Route::put('/user/', [ UserController::class, 'put' ]); / ** PATCH route * / Route::patch('/user/', [ UserController::class, 'patch' ]); / ** DELETE route * / Route::delete('/user/{id}', [ UserController::class, 'delete' ]);
Grouped Routes
Route::prefix('v1')->group(function() { Route::get('/example/{id}', [ V1ExampleController::class, 'get' ]); Route::post('/example', [ V1ExampleController::class, 'post' ]); }); Route::prefix('v2')->group(function() { Route::get('/example/{id}', [ V2ExampleController::class, 'get' ]); Route::post('/example', [ V2ExampleController::class, 'post' ]); });
Nested Routes
Route::prefix('multilevel')->group(function() { Route::prefix('1')->group(function() { Route::get('/example/{id}', [ V2ExampleController::class, 'get' ]); Route::post('/example', [ V2ExampleController::class, 'post' ]); Route::prefix('2')->group(function() { Route::get('/example/{id}', [ V2ExampleController::class, 'get' ]); Route::post('/example', [ V2ExampleController::class, 'post' ]); }); }); });
Namespaces
Route::prefix('v1') ->namespace('Rockberpro\\RestRouter\\Controllers') ->group(function() { Route::get('/example1', 'V1ExampleController@example'); } ); Route::prefix('v2') ->namespace('Rockberpro\\RestRouter\\Controllers') ->group(function() { Route::get('/example2', 'V2ExampleController@example'); } );
Middleware
Route::prefix('v1') ->middleware(AuthMiddleware::class) ->namespace('Rockberpro\\RestRouter\\Controllers') ->group(function() { Route::get('/hello', 'HelloWorldController@hello'); } ); Route::middleware(AuthMiddleware::class) ->namespace('Rockberpro\\RestRouter\\Controllers') ->get('/hello', 'HelloWorldController@hello');
Controllers
Route::controller(HelloWorldController::class)->group(function() { Route::get('/hello', 'hello'); });