定制 rockberpro/rosa-router 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

rockberpro/rosa-router

最新稳定版本:v2.4.0

Composer 安装命令:

composer require rockberpro/rosa-router

包简介

Smart REST router for PHP

README 文档

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, PATCH and DELETE
  • 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\RosaRouter\Bootstrap;

require_once "vendor/autoload.php";

// Bootstrap::setup('path/to/.env');
// Bootstrap::setup('path/to/.ini');
Bootstrap::setup();
$server = Server::init();
if ($server->isApiEndpoint()) {
    $server->loadRoutes('./routes/api.php');
    $server->execute(Server::MODE_STATELESS);
}
  • server.php
// To run the server: php server.php
<?php

use Rockberpro\RosaRouter\Utils\DotEnv;
use Rockberpro\RosaRouter\Bootstrap;
use React\Socket\SocketServer;
use React\Http\HttpServer;

require_once "vendor/autoload.php";

// Bootstrap::setup('path/to/.env');
// Bootstrap::setup('path/to/.ini');
Bootstrap::setup();
$port = DotEnv::get('API_SERVER_PORT');
$address = DotEnv::get('API_SERVER_ADDRESS');

$server = Server::init();
$server->loadRoutes('./routes/api.php');
$server = new HttpServer(
    $server->execute(Server::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');
});

统计信息

  • 总下载量: 100
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-09-26

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固