wpnx/handler 问题修复 & 功能扩展

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

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

wpnx/handler

Composer 安装命令:

composer require wpnx/handler

包简介

A flexible PHP request handler for WordPress installations

README 文档

README

Tests Code Coverage Latest Stable Version License PHP Version

A modern, secure PHP request handler for WordPress installations. Provides intelligent request routing, static file serving, and comprehensive security features. Optimized for traditional hosting, AWS Lambda, and containerized environments.

Features

  • Simple API: Just three lines of code to get started
  • Intelligent Routing: Automatic handling of WordPress requests, static files, and directories
  • Security First: Built-in protection against common attacks
  • Multisite Support: Full support for WordPress Multisite installations
  • AWS Lambda Ready: Automatic environment detection and optimization
  • Extensible: Easy to add custom processing logic
  • Modern PHP: PHP 8.0+ with strict typing and Symfony HttpFoundation

Requirements

  • PHP 8.0 or higher
  • WordPress 5.0 or higher (recommended)

Installation

Install via Composer:

composer require wpnx/handler

Quick Start

Minimal Setup

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

use WpNx\Handler\Handler;

// Create handler and run
$result = (new Handler())->run();

// If a file path is returned, require it
if ($result) {
    require $result;
}

With Configuration

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

use WpNx\Handler\Handler;
use WpNx\Handler\Configuration;

$config = new Configuration([
    'web_root' => __DIR__,
    'multisite' => true,    // Enable multisite with defaults
    'lambda' => true,       // Force Lambda mode
]);

$result = (new Handler($config))->run();

if ($result) {
    require $result;
}

Configuration

Simple Configuration

$config = new Configuration([
    'multisite' => true,    // Use default multisite settings
    'lambda' => false,      // Disable Lambda mode even if detected
]);

Detailed Configuration

Option Type Description Default
web_root string Web root directory Current directory
wordpress_index string WordPress index file '/index.php'
wp_directory string WordPress directory '/wp'
index_files array Index files to look for ['index.php', 'index.html', 'index.htm']
Multisite
multisite bool/array Enable multisite false
multisite.enabled bool Enable multisite (detailed mode) false
multisite.pattern string URL match pattern '#^/[_0-9a-zA-Z-]+(/wp-.*)#'
multisite.replacement string URL replacement '/wp$1'
Lambda
lambda bool/array Lambda mode Auto-detect
lambda.enabled bool Force Lambda on/off Auto-detect
lambda.directories array Directories to create ['/tmp/uploads', '/tmp/cache', '/tmp/sessions']
Security
security.allow_directory_listing bool Allow directory listing false
security.check_symlinks bool Validate symlinks true
security.blocked_patterns array Blocked URL patterns See below

Default blocked patterns:

  • /\.git/
  • /\.env/
  • /\.htaccess/
  • /composer\.(json|lock)/
  • /wp-config\.php/
  • /readme\.(txt|html|md)/i

WordPress Multisite

Simple Mode

$config = new Configuration([
    'multisite' => true  // Use default settings
]);

Custom Pattern

$config = new Configuration([
    'multisite' => [
        'enabled' => true,
        'pattern' => '#^/sites/([^/]+)(/wp-.*)#',
        'replacement' => '/wp$2'
    ]
]);

AWS Lambda Support

Auto-Detection

The handler automatically detects Lambda environments and:

  • Creates necessary temp directories
  • Configures PHP settings for Lambda
  • Optimizes for Lambda constraints

Manual Control

// Force Lambda mode on (useful for testing)
$config = new Configuration(['lambda' => true]);

// Force Lambda mode off
$config = new Configuration(['lambda' => false]);

// Custom Lambda directories
$config = new Configuration([
    'lambda' => [
        'directories' => [
            '/tmp/uploads',
            '/tmp/my-app-cache',
        ]
    ]
]);

Extending the Handler

Custom Processors

Add custom request processing logic:

use WpNx\Handler\Processors\ProcessorInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class MaintenanceModeProcessor implements ProcessorInterface
{
    public function process(Request $request, Configuration $config): Request|Response|null
    {
        if ($this->isMaintenanceMode()) {
            return new Response('Site under maintenance', 503);
        }
        return null; // Continue to next processor
    }
}

// Add to handler
$handler = new Handler($config);
$handler->addProcessor(new MaintenanceModeProcessor(), priority: 1);
$result = $handler->run();

if ($result) {
    require $result;
}

Built-in Processors

The handler uses a chain of processors in this order:

  1. SecurityProcessor - Validates requests and blocks attacks
  2. TrailingSlashProcessor - Ensures directories have trailing slashes
  3. StaticFileProcessor - Serves static files with proper MIME types
  4. PhpFileProcessor - Handles direct PHP file requests
  5. DirectoryProcessor - Looks for index files in directories
  6. MultisiteProcessor - Handles multisite URL rewriting
  7. WordPressProcessor - Falls back to WordPress index.php

Examples

Basic WordPress Site

$result = (new Handler())->run();

if ($result) {
    require $result;
}

Multisite Network

$config = new Configuration([
    'multisite' => true,
    'web_root' => '/var/www/html'
]);

$result = (new Handler($config))->run();

if ($result) {
    require $result;
}

High-Security Setup

$config = new Configuration([
    'security' => [
        'blocked_patterns' => [
            '/\.git/',
            '/\.env/',
            '/vendor/',
            '/node_modules/',
            '/\.DS_Store$/',
            '/Thumbs\.db$/i',
        ]
    ]
]);

$result = (new Handler($config))->run();

if ($result) {
    require $result;
}

Lambda Deployment

$config = new Configuration([
    'lambda' => true,
    'multisite' => true
]);

$result = (new Handler($config))->run();

if ($result) {
    require $result;
}

Testing

# Run tests
vendor/bin/phpunit

# Run specific test
vendor/bin/phpunit tests/HandlerTest.php

# Check code style
vendor/bin/phpcs

# Fix code style
vendor/bin/phpcbf

Security

The handler includes comprehensive security measures:

  • Path Traversal Protection: Prevents ../ attacks
  • Symlink Validation: Ensures symlinks stay within web root
  • Blocked Patterns: Protects sensitive files
  • Null Byte Protection: Prevents null byte injection
  • URL Encoding Protection: Detects encoded attacks

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License.

wpnx/handler 适用场景与选型建议

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

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

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

围绕 wpnx/handler 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

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