jsc-php/routes
Composer 安装命令:
composer require jsc-php/routes
包简介
Maps a http request to a class method using attributes.
关键字:
README 文档
README
This package provides a router for handling HTTP requests in PHP applications. It allows you to define routes and associate them with specific controller actions, making it easier to manage and organize your application's routing logic.
Installation
$ composer require jsc-php/routes
Features
- Attribute-based routing – Define routes using modern PHP attributes
- Access control - Built-in support for protected/public routes
- Priority Based Routing – Define routes with priority levels for better control
- Memcached support – Optional route caching for improved performance
- URI parameter extraction – Automatically extract and pass parameters to your methods
- Automatic discovery – Scans directories to find and register routes
- Method filtering – Restrict routes to specific HTTP methods (GET, POST, etc.)
Requirements
- PHP >= 8.5
- ext-zlib
- ext-http
- ext-uri
- ext-memcached
Usage
Create a 'RouterConfig' instance and pass if to the Router Constructor
$router_config = new \JscPhp\Routes\RouterConfig();
$router_config->addDirectory('/path/to/controllers');
$router = new Router($router_config)
$router->go();
Note
If you want to use Memcached for caching, ensure Memcached is installed and configured properly
$router_config->addMemcachedServer(host: 'localhost', port: 11211);
Host is optional if using localhost Port number is optional if using default port
You can also use setUseMemcached(true); This will add a default memcached server using host 'localhost' and port '11211'
For testing, you can also use setUseMemcached(false) after using addMemcachedServer to disable using Memcached
Route Attribute
Add a \JscPhp\Routes\Attr\Route attribute ot the controller method you want to handle the request
use JscPhp\Routes\Attributes\Route;
class Controller {
#[Route('/post')]
function post() {...}
}
You can define multiple routes by adding addition Route attributes to the same method
use JscPhp\Routes\Attributes\Route;
...
#[Route('/post')]
#[Route('/read')]
function getPost() {...}
Parameters can be defined it the route path by wrapping them in curly braces.
use JscPhp\Routes\Attributes\Route;
...
#[Route('/post/{id}/{page}')]
public function post($id, $page) {...}
Adding a question mark to the parameter name makes it optional.
#[Route('/post/{id?}')]
Adding a pipe <|> to the parameter is optional but can limit the type of values that can be accepted for that parameter.
| Type | Symbol | Description | Example |
|---|---|---|---|
| Integer | i |
Matches only integers | {id|i} |
| Alpha | a |
Matches only alphabetic characters | {name|a} |
| Decimal/Float | d or f |
Matches decimal numbers | {price|d} |
use JscPhp\Routes\Attributes\Route;
#[Route('/hello/{id}')] - Matches /hello/abc123
#[Route('/hello/{id|i}')] - Matches /hello/123 but not /hello/abc123
For custom types, you can use also use a regex expression after the pipe. Do not include parentheses.
#[Route('/hello/{id|\d{3}}')] - Matches /hello/123 but not /hello/1234
Access Attribute
Adding an \JscPhp\Routes\Attr\Access attribute flags the route as protected. To find a matching route, in the
getRoute() or go()
function, you need to set the search_private parameter to true.
const('IS_LOGGED_IN', isset($_SESSION, 'uid');
$router->go(search_private: IS_LOGGED_IN);
jsc-php/routes 适用场景与选型建议
jsc-php/routes 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 44 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 01 月 27 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「routes」 「attributes」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 jsc-php/routes 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jsc-php/routes 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 jsc-php/routes 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Generates a Blade directive exporting all of your named Laravel routes. Also provides a nice route() helper function in JavaScript.
Block routes by IP
Provides caching methods which can be easily used for caching routes.
A library that allows you to easily use the PHP-VCR library in your PHPUnit tests.
This Package provides some usefully console features like the attribute syntax for arguments and options, validation, auto ask and casting.
The last validation library you will ever need!
统计信息
- 总下载量: 44
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 22
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: GPL-3.0-or-later
- 更新时间: 2026-01-27