mpm / routing
Mpm 路由器
v1.1.0
2023-05-06 14:58 UTC
Requires
- php: ^8.0
- mpm/http: ^1.0
This package is auto-updated.
Last update: 2024-09-28 13:04:04 UTC
README
简单而强大的 PHP 路由,适用于您的 Web 应用程序
目录
- 介绍
- 特性
- 入门
- 先决条件
- 安装
- 使用
- 示例
- 贡献
- 许可证
介绍
mpm-routing
是您在 PHP Web 应用程序中进行路由的首选解决方案。它简化了定义路由、匹配请求和处理路由错误的过程。
特性
- 简化路由: 使用 HTTP 方法(如 GET 和 POST)轻松定义路由,使路由设置变得简单。
- 灵活的路由匹配: 根据请求 URI 和 HTTP 方法匹配路由,允许动态和精确的路由。
- 自定义异常处理: 使用自定义异常处理轻松处理路由错误。优雅地检测和响应问题。
- 模块化和轻量级:
mpm-routing
设计为轻量级且易于集成到您的 PHP 项目中,确保最小化开销。 - 附加路由: 通过包含附加路由文件来组织您的路由,使代码库整洁且易于维护。
- 列出路由: 以编程方式检索所有已注册路由的列表,帮助您了解应用程序的路由结构。
- 社区支持: 加入使用
mpm-routing
的活跃开发者社区,并从持续更新和改进中受益。 - 开源: 此项目是开源的,并受 MIT 许可证的约束,因此您可以有信心将其用于您的项目。
- 响应式开发: 享受定期更新和社区贡献带来的响应式开发,确保满足您的路由需求。
- 易于集成: 无缝地将
mpm-routing
集成到现有 PHP 项目中,或在新的项目中使用,无需复杂设置。
选择 mpm-routing
为您的 PHP Web 应用程序提供强大且易于使用的路由解决方案。
入门
先决条件
- php >= 8.0
- mpm\http >= 1.1
安装
composer require mpm\routing
使用
1. 使用 HTTP 方法(如 GET 和 POST)定义基本路由。
Specify the route URI and the associated controller or handler function.
```
// Define a GET route
Route::get('/home', 'HomeController@index');
// Define a POST route
Route::post('/submit', 'FormController@process');
```
2. 处理动态路由
Match dynamic routes using route parameters enclosed in curly braces `{}`. These parameters capture values from the URL for further processing.
```<?php
// Match a dynamic route with a parameter
Route::get('/user/{id}', 'UserController@show');
// Access the captured parameter in the handler function
public function show($id) {
// Use $id to fetch and display user information
}
```
示例
1. 定义路由
// Define a GET route Route::get('/home', 'HomeController@index'); // Define a POST route Route::post('/submit', 'FormController@process');
2. 处理动态路由
Match dynamic routes using route parameters enclosed in curly braces `{}`. These parameters capture values from the URL for further processing.
```
// Match a dynamic route with a parameter
Route::get('/user/{id}', 'UserController@show');
// Access the captured parameter in the handler function
public function show($id) {
// Use $id to fetch and display user information
}
```
3. 包含附加路由
Organize your routes by including additional route files. This helps keep your codebase clean and maintainable.
```
// Include routes from an additional file
Router::includes('path/to/extra-routes.php');
```
4. 列出已注册的路由
Retrieve a list of all registered routes programmatically to understand your application's routing structure.
```markdown
// Get a list of registered routes
$routes = Router::routes();
```
贡献
我们欢迎社区对 mpm-routing
模块进行增强和改进的贡献。无论您是想报告错误、提出新功能还是提交代码更改,您的贡献都是有价值的。要为此项目做出贡献,请遵循以下指南
- 分支存储库: 首先从 GitHub 账户分支此存储库。
- 创建分支: 为您的贡献创建一个新的分支。使用与您的修复或功能相关的描述性分支名称。
- 进行更改: 对代码库进行所需的更改或改进。确保您的代码符合项目编码标准。
- 编写测试: 如果您的贡献涉及代码更改,请编写相关的测试,以确保功能按预期工作。
- 提交更改: 使用清晰简洁的提交信息提交更改。
- 推送到您的分支: 将更改推送到您在 GitHub 上分支的存储库。
- 创建拉取请求 (PR): 对此存储库的主要分支创建拉取请求。提供关于您的更改的详细描述,为什么它们是必要的,以及任何相关的问题。
- 审查和协作: 参与您 PR 的讨论。在审查过程中解决任何反馈或提出的问题。
- 持续集成:确保您的更改通过自动化测试,且不会引入任何新问题。
- 合并:一旦您的PR被批准,它将被合并到主分支。恭喜您的贡献!
许可证
本项目遵循MIT许可证 - 详细信息请参阅LICENSE文件。