定制 giftcards/mod-rewrite 二次开发

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

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

giftcards/mod-rewrite

Composer 安装命令:

composer require giftcards/mod-rewrite

包简介

A PHP implementation of mod rewrite

README 文档

README

Purpose

The mod rewrite library is meant to be a php implementation of mod rewrite. it can read the same directives given to the apache rewrite engine module and use them to rewrite given urls the same way as apache would.

Support

RewriteRule, RewriteCond, RewriteEngine are supported with the caveat that certain server variables are not implemented yet in the replacements system as well as a bunch of the more advanced rewriting flags for the RewriteRule directive arent implemented. Adding support for these features as well as the other directives is pretty straightforward through extension it just hasn't been done yet. At the moment only the server variables QUERY_STRING, REQUEST_URI, REQUEST_FILENAME, HTTP_HOST are supported. Feel free to PR support for more!

Usage

Basic

if you just want to use the stock rewriter with a file you just need to use the 2 builders to build the rewriter and the file compiler

<?php

use Giftcards\ModRewrite\RewriterBuilder;
use Giftcards\ModRewriter\Compiler\CompilerBuilder;
use Symfony\Component\HttpFoundation\Request;

$rewriter = RewriterBuilder::create()->build();
$compiler = CompilerBuilder::create()->build();

$request = Request::createFromGlobals();
$file = __DIR__.'/rewrite_rules';

$result = $rewriter->rewrite(
    rawurldecode($request->getPathInfo()),
    $request,
    $compiler->compile(file_get_contents($file))
);

the $result is an instance of Giftcards\ModRewrite\Result you can get the rewritten url from getUrl() this will be populated whether it was matched and rewritten by a rule or not. You can get the rule that was matched from getMatchedRule(). the rule contains the rewrite directive as well as the condition directives. If no rule was matched getMatchedRule() will return null.

Note: make sure your urls are decoded before passing them to the rewriter since it could cause odd things if it's not decoded

Processor

The processor class can be used to process a submitted request along with a matched result and spit out a result. After processing the request according to the rules given it will either return a clone of the given request with the query and uri changed according to the matched rule, or a Symfony\Component\HttpFoundation\Response if the rule says to return some sort of status code or redirect (R|redirect, F|forbidden, G|gone etc). You can use these as they are in your application to either the forward a request or return a response

###Formatters###

Formatters are the clases that do all the replacing of values in the TestString and CondPattern in the RewriteCond as well as the Substitution in the RewriteRule in the default rewriter setup. In the Formatters dir there are a bunch there

  • ApacheServerVariableFormatter - this class takes the string and does the replacement for the server variables like HTTP_HOST and REQUEST_URI
  • LastPassingConditionFormatter - this class is the one that replaces all the backreferences from the regex of the last passing condition
  • PathInfoFormatter - if you put a dash as the value in any of the 3 formatted values it will replace it with the url that was passed to be rewritten
  • RewriteFormatter - this class is the one that replaces all the backreferences form the regex from the rule that matched

by default all of these are added to a chain formatter and run in the order they were added you can add as many formatters as you want they have to implement Giftcards\ModRewrite\Formatter\FormaterInterface

###Condition Predicate Checkers###

the condition predicate checkers are the classes that after a rule matches are used to check the conditions attached to the rule. in the Condition there are a bunch already defined

  • ExistencePredicateChecker - this is the one that does checks for the -f and -d flags to see if the file or dir mentioned exists
  • FormattingPredicateChecker - this checker gets passed another checker to actually do the checking with as well as a formatter which it uses to format the test string as well as the condition pattern before passing it to the real checker. this class allows for condition backreferences rewrite backreferences etc to be used in these values
  • NotPredicateChecker - this class allows for the ! char to be used in a condition if a ! is there it strips it then passes it to the real checker to do its thing then returns the opposite of the result
  • RegexPredicateChecker - this treats the condition pattern as a regex and does the check on the test stirng that way

all predicate checkers have a supports method. this allows them to be added to a chain checker which will check to see if any checkers in the chain support the condition pattern (predicate) the chain will throw an exception if no supporting checker is found. the chain allows you to set a default checker to run instead if you dont want an exception thrown. in the default setup the existence checker is added to the chain, the regex checker is set as the default. the chain then passed into the not checker which is passed into the formatting checker.

giftcards/mod-rewrite 适用场景与选型建议

giftcards/mod-rewrite 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5.46k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2015 年 02 月 25 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 giftcards/mod-rewrite 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 3
  • Watchers: 7
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-02-25