php伪静态:php简单的伪静态url机制实现
曾几何时,我们公司准备开发一套新的建站系统,决定将以前的框架给ko掉,重新开发一套新的框架来适应新的系统功能。领导们不希望使用外面已有的框架,号称要开发有自己特色的框架(不懂开发的领导害死人)。于是我们又投入到了新的开发当中。
由于我们的系统支持伪静态,以前的系统是直接使用服务器apache或iis自带的rewrite文件定义规则,而框架中没有任何路由机制,于是这次框架准备使用新的策略,由php实现路由机制。于是我开始了功能实现的探索之路。
开发之前,我先了解‘路由机制’要做什么,它主要做两件事。
1.路由机制就是把某一个特定形式的url结构中提炼出来系统对应的参数。举个例子,如:http://www.cxybl.com/article/1 其中:/article/1 -> ?_m=article&id=1。
2.其次,是将拥有对应参数的url转换成特定形式的url结构,是上面的过程的逆向过程。由于路由机制隔离了url结构和参数的转换关系,使的日后结构的变化不会影响下面代码的执行。
通过上面的了解,可以得出要编写一个路由机制要一下几步:
1.编写服务器apache或iis自带的rewrite文件,将url结构导入index.php。
2.一个路由规则配置文件。
3.一个路由解析器,用来解析规则,匹配和转换url。
于是,我们一一实现其中的每一个部分。
1.rewrite文件编写,以apache为例:
rewriteengine on
rewriterule ^index\.php$ - [l]
rewritecond %{request_filename} !-f
rewritecond %{request_filename} !-d
rewriterule (.+) index.php/$1 [l]
上面的代码就是将url结构导入index.php中,具体的rewrite细节就不赘述了。
2.在php中设置一个路由规则配置文件routes.php,我简单的使用了一个hash数组编写规则:
/**
*路由配置文件编写说明:
* 路由配置在一个array数组中,一条记录代表一个规则
* 其中数组key的数据代表匹配的路径格式:使用特定的字符串标识 如:'/{id}'
* 字符串中可以包含特定的变量,所有变量使用大括号{}包裹起来
* 数组value里是一个array数组,是对key中路径中变量进行特定处理
* 变量写在数组的key中,规范写在数组的value里,如:array('id'=>'/\d+/','_m'=>'frontpage','_a'=>'index')
* 规范分成两类:
* 1.格式判断:比如 '/{id}'=> array('id'=>'/\d+/','_m'=>'frontpage','_a'=>'index') 为例,其中'id'=>'/\d+/'就是一个格式判断,
* 表示id变量只能是数字,格式判断后面只能使用正则表达式,由于php没有正则类,所以我指定 '/xxx/'和'#xxx#'格式的字符串为正则表达式
* 2.默认参数:比如 '/{id}'=> array('id'=>'/\d+/','_m'=>'frontpage','_a'=>'index') 为例,其中'_m'=>'frontpage'就是一个默认参数,
* 因为前面的路径没有_m和_a信息,所以后面会使用默认参数作为_m和_a的值
*
* 所以对于规则'/{id}'=> array('id'=>'/\d+/','_m'=>'frontpage','_a'=>'index')。我传入 /3 系统会转换成 index.php?_m=frontpage&_a=index&id=3
*
* 规则匹配是按照$routes数组的顺序逐一匹配,一旦匹配上了就不往下匹配了。所以一些特定的匹配规则要放在前面,通用的放在后面。
* 否则可能导致不执行特定的匹配规则了
*/
$routes= array(
'/' => array('_m'=>'wp_frontpage','_a'=>'index'),
'/{id}'=> array('id'=>'/\d+/','_m'=>'wp_frontpage','_a'=>'index'),
'/{_m}/{_a}/{id}'=> array('id'=>'/\d+/'),
'/{_m}/{_a}'=> array()
);
3.路由机制中最复杂也是最重要的一部分,就是解析器。
解析器有两个类组成(名字可能起的不佳)。
一个是route,作为整个解析器对外的接口,用于解析规则,匹配和转换url,然而它只是一个代理,实际操作不是直接由它直接做的。
一个是routepattern,每个routepattern实例对应规则数组中的一条记录,一个route实例包含多个routepattern,而route中的所有操作都会调用内部所有routepattern实例操作,并进行整合。
class route
{
private static $instance = null;
private $routepatterns=array();
private function __construct()
{
$routes = array();
include root."/routes.php";
foreach($routes as $key=>$value){
$this->routepatterns[]=new routepattern($key,$value);
}
if(!isset($_server['path_info'])) return false;
$urlpath= $_server['path_info'];
$ismatch=$this->match_url($urlpath); 本文链接http://www.cxybl.com/html/wlbc/Php/20120607/28511.html

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The message "Your organization has asked you to change your PIN" will appear on the login screen. This happens when the PIN expiration limit is reached on a computer using organization-based account settings, where they have control over personal devices. However, if you set up Windows using a personal account, the error message should ideally not appear. Although this is not always the case. Most users who encounter errors report using their personal accounts. Why does my organization ask me to change my PIN on Windows 11? It's possible that your account is associated with an organization, and your primary approach should be to verify this. Contacting your domain administrator can help! Additionally, misconfigured local policy settings or incorrect registry keys can cause errors. Right now

Windows 11 brings fresh and elegant design to the forefront; the modern interface allows you to personalize and change the finest details, such as window borders. In this guide, we'll discuss step-by-step instructions to help you create an environment that reflects your style in the Windows operating system. How to change window border settings? Press + to open the Settings app. WindowsI go to Personalization and click Color Settings. Color Change Window Borders Settings Window 11" Width="643" Height="500" > Find the Show accent color on title bar and window borders option, and toggle the switch next to it. To display accent colors on the Start menu and taskbar To display the theme color on the Start menu and taskbar, turn on Show theme on the Start menu and taskbar

By default, the title bar color on Windows 11 depends on the dark/light theme you choose. However, you can change it to any color you want. In this guide, we'll discuss step-by-step instructions for three ways to change it and personalize your desktop experience to make it visually appealing. Is it possible to change the title bar color of active and inactive windows? Yes, you can change the title bar color of active windows using the Settings app, or you can change the title bar color of inactive windows using Registry Editor. To learn these steps, go to the next section. How to change title bar color in Windows 11? 1. Using the Settings app press + to open the settings window. WindowsI go to "Personalization" and then

index.html represents the home page file of the web page and is the default page of the website. When a user visits a website, the index.html page is usually loaded first. HTML (HypertextMarkupLanguage) is a markup language used to create web pages, and index.html is also an HTML file. It contains the structure and content of a web page, as well as tags and elements used for formatting and layout. Here is an example index.html code: <

Do you see "A problem occurred" along with the "OOBELANGUAGE" statement on the Windows Installer page? The installation of Windows sometimes stops due to such errors. OOBE means out-of-the-box experience. As the error message indicates, this is an issue related to OOBE language selection. There is nothing to worry about, you can solve this problem with nifty registry editing from the OOBE screen itself. Quick Fix – 1. Click the “Retry” button at the bottom of the OOBE app. This will continue the process without further hiccups. 2. Use the power button to force shut down the system. After the system restarts, OOBE should continue. 3. Disconnect the system from the Internet. Complete all aspects of OOBE in offline mode

Taskbar thumbnails can be fun, but they can also be distracting or annoying. Considering how often you hover over this area, you may have inadvertently closed important windows a few times. Another disadvantage is that it uses more system resources, so if you've been looking for a way to be more resource efficient, we'll show you how to disable it. However, if your hardware specs can handle it and you like the preview, you can enable it. How to enable taskbar thumbnail preview in Windows 11? 1. Using the Settings app tap the key and click Settings. Windows click System and select About. Click Advanced system settings. Navigate to the Advanced tab and select Settings under Performance. Select "Visual Effects"

We all have different preferences when it comes to display scaling on Windows 11. Some people like big icons, some like small icons. However, we all agree that having the right scaling is important. Poor font scaling or over-scaling of images can be a real productivity killer when working, so you need to know how to customize it to get the most out of your system's capabilities. Advantages of Custom Zoom: This is a useful feature for people who have difficulty reading text on the screen. It helps you see more on the screen at one time. You can create custom extension profiles that apply only to certain monitors and applications. Can help improve the performance of low-end hardware. It gives you more control over what's on your screen. How to use Windows 11

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety
