denniscarrazeiro/php-router-module 问题修复 & 功能扩展

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

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

denniscarrazeiro/php-router-module

Composer 安装命令:

composer require denniscarrazeiro/php-router-module

包简介

A library that proposes handle http routes module. Consider that Module goes be use in PHP projects.

README 文档

README

Maintainer PHP from Packagist Latest Version Software License

This PHP Router class provides a robust and easy-to-use routing solution for your web applications, offering many features to ensure a single, consistent routing instance throughout your project. It enables you to define routes for various HTTP methods (GET, POST, PUT, DELETE, PATCH, OPTIONS, HEAD), incorporating support for middleware to execute logic before your main route callbacks. The router dynamically handles URL parameters, provides static access to current request details (URI, method, and parameters), and includes built-in mechanisms for handling 404 Not Found and 405 Method Not Allowed errors, making it a comprehensive tool for managing your application's request flow.

Instalation

bash ./scripts/composer_install.sh

Composer is a dependency manager for the PHP programming language. Therefore, after running the command above, Composer will install all the necessary dependencies to ensure the project functions under the best possible conditions.

Unit Tests

bash ./scripts/phpunit_tests.sh

PHPUnit is a programmer-oriented testing framework for PHP, designed to facilitate the creation and execution of unit tests. Consequently, after setting up your test suite and running the appropriate command, PHPUnit will execute your tests and provide detailed feedback, ensuring your codebase maintains a high level of quality and reliability.

PHP Built-in Web Server

bash ./scripts/start_php_server.sh

The PHP built-in web server is a command-line tool that provides a quick and convenient way to test PHP applications locally. Consequently, after navigating to your project's root directory and running the appropriate command, the PHP built-in web server will serve your files over HTTP, allowing you to access and test your application directly in a web browser without the need for a full-fledged web server like Apache or Nginx.

Bruno

You can import Bruno collection located in 'http' folder to test the requests.

Bruno is an open-source, Git-friendly, and offline-first API client designed for exploring and testing APIs. Consequently, after setting up your API collections as plain text files within your project's version control system, Bruno will allow you to send requests, analyze responses, and manage your API environments, ensuring seamless collaboration and a streamlined workflow for API development and testing.

Server configuration before use

Remember: You can use the built-in web server run the command start_php_server.sh to execute the example/index.php

Apache

The .htaccess f*ile is a powerful, directory-level configuration file used on Apache web servers. For your PHP Router class, it plays a crucial role by enabling URL rewriting, which is essential for a clean and efficient routing system. Enable mod_rewrite in your apache server and add the following .htaccess in the root of your php project:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php?__ROUTER__=/$1 [L,QSA]
</IfModule>

Nginx

Nginx uses its own configuration syntax for URL rewriting. You typically configure this in your server's nginx.conf file or a site-specific configuration file within sites-available (and symlinked to sites-enabled). For your Router class, an Nginx configuration would look something like this within your server block:

server {
    listen 80;
    server_name yourdomain.com;
    root /path/to/your/project/public; # Assuming index.php is in a 'public' directory

    index index.php;

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

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php8.x-fpm.sock; # Adjust for your PHP-FPM version
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param QUERY_STRING __ROUTER__=$uri&$query_string; # Pass __ROUTER__ as query param
    }
}

Examples

Usage Example 1

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

use DennisCarrazeiro\Php\Router\Module\Router\Router;
use DennisCarrazeiro\Php\Router\Module\Router\RouterDTO;

$router = new Router();
$router->get(
    RouterDTO::route('/home')
        ->callback(function(){
            echo "<h1>Hello World!</h1>";
        })
        ->middleware(function (){
            echo "<h2>Hello I am middleware!</h2>"
        })
        ->name('home')
);
$router->dispatch();

Usage Example 2

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

use DennisCarrazeiro\Php\Router\Module\Router\Router;
use DennisCarrazeiro\Php\Router\Module\Router\RouterDTO;

$router = new Router();
$router->post(
    RouterDTO::route('/user/{id}')
        ->callback(function($id){
            echo "<h1>User profile updated!</h1>";
        })
        ->middleware(function(){
            if(!isset($_POST['csrf']) || $_POST['csrf'] !== '123456789'){
                throw new \Exception("Csrf is required.");
            }
        })
        ->name('user.profile')
);
$router->dispatch();

Full example of use

For more examples see the Examples folder.

License

The MIT license. Please see License file for more information.

denniscarrazeiro/php-router-module 适用场景与选型建议

denniscarrazeiro/php-router-module 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 05 月 31 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「router」 「routes」 「Carrazeiro」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 denniscarrazeiro/php-router-module 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 denniscarrazeiro/php-router-module 我们能提供哪些服务?
定制开发 / 二次开发

基于 denniscarrazeiro/php-router-module 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-05-31