csulok0000/routing 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

csulok0000/routing

Composer 安装命令:

composer require csulok0000/routing

包简介

README 文档

README

Status: in development

⚠️ This project is currently under development and not recommended for production use.

Simple PHP Router

Author: Tibor Csik csulok0000@gmail.com

Install

Install Composer and run following command in your project's root directory:

$ composer require csulok0000/routing "dev-main"

Getting Started

use Csulok0000\Routing\Router;

$router = new Router();

$router->get('/', function () {
    echo "<h1>Home</h1>";
});

$router->get('/contents/{id}', function ($id) {
    echo "<h1>Content: $id</h1>";
});

try {
    $response = $route->dispatch($_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI']);
} catch (Exception $e) {

}

$response?->send();

Routes

use Csulok0000\Routing\Route;
use Csulok0000\Routing\Enums\Method;

// The HTTP method can be specified in several ways
new Route('GET', '/a/{b}', fn($b) => 'Hello ' . $b);
new Route(['GET'], '/a/{b}', fn($b) => 'Hello ' . $b);
new Route(Method::Get, '/a/{b}', fn($b) => 'Hello ' . $b);
new Route([Method::Get], '/a/{b}', fn($b) => 'Hello ' . $b);

// ...or multiple methods at once
new Route(['GET', 'POST'], '/a/{b}', fn($b) => 'Hello ' . $b);

There are also multiple ways to define the action:

new Route('GET', '/', fn($b) => 'Hello ' . $b);
new Route('GET', '/', [TestController::class, 'index']);
new Route('GET', '/', Closure::fromCallable([TestController::class, 'index']));

Named Routes

Routes can be given names, allowing us to reference them later — for example, when generating a URL.

new Route('GET', '/', fn($b) => 'Hello ' . $b, 'route1');
// or
(new Route('GET', '/', fn($b) => 'Hello ' . $b))->setName('route1');

Router class

Adding a route

...
$router->addRoute(new Route('GET', '/', fn() => ''));

Helper methods

The Router class includes several helper methods to simplify adding routes.

...
// For individual HTTP methods
$router->get('/', fn () => '');
$router->head('/', fn () => '');
$router->post('/', fn () => '');
$router->put('/', fn () => '');
$router->patch('/', fn () => '');
$router->delete('/', fn () => '');
$router->options('/', fn () => '');

// For all HTTP methods
$router->any('/', fn () => '');

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-04-13