定制 henningcullin/php-routing 二次开发

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

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

henningcullin/php-routing

最新稳定版本:v0.1.0

Composer 安装命令:

composer require henningcullin/php-routing

包简介

A lightweight PHP routing library

README 文档

README

A minimal routing library for PHP, inspired by Rust Axum.

⚠️ Warning: This library is in early development (pre-1.0).
APIs and behavior may change without notice. Use at your own risk.

Features

  • Fast and lightweight – minimal dependencies, small footprint.
  • Flexible route matching – supports static paths, named parameters, optional segments, and wildcard routes.
  • HTTP method routing – GET, POST, PUT, PATCH, DELETE, and more.
  • Route nesting – prefix groups of routes cleanly.
  • Fallback and 404 handling – default or path-specific fallbacks.
  • Middleware support – wrap routes or all routes in a layer.
  • Request state propagation – attach custom state objects to requests.
  • Convenient response helpersjson(), text(), redirect(), and problem() responses.
  • Cookie and header management – structured response headers and cookies.
  • View integration – simple PHP views with layout support.
  • Helmet support – manage <head> tags programmatically.
  • Modern PHP 8+ syntax – typed properties, enums, union types, and match expressions.
  • Composer-ready – integrates easily into existing projects.

Installation

Install via Composer:

composer require henningcullin/php-routing

Setup

All requests should be routed through a single entry point (index.php or main.php). Use a rewrite rule in your web server:

Apache .htaccess example

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

Nginx example

location / {
   try_files $uri /index.php?$query_string;
}

Example Usage

require_once __DIR__ . '/vendor/autoload.php';

use Routing\Router;
use Routing\Response;
use Routing\StatusCode;

// Create a router instance
$router = Router::new();

// Define routes
$router
    ->route('/hello/:name', Routing\get(function ($params) {
        return Response::text("Hello, " . $params['name']);
    }))
    ->route('/json', Routing\get(function () {
        return Response::json(['message' => 'Hello, JSON']);
    }));

// Define a fallback
$router->fallback(function () {
    return Response::text("Page not found", StatusCode::NOT_FOUND);
});

// Listen for requests
$router->listen();

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-10-14