php 学习笔记:MVC 入口设计与伪静态
原创笔记,转载须注明来源和链接 php开发web程序,MVC模式的入口设计非常关键,常用的著名开源程序如wordpress、discuz都有特别的入口设计,除了要做controller 、action ,路由设计也是被重视的,因为这关系到url的美观和搜索友好度。 wordpress的入口和伪
原创笔记,转载须注明来源和链接
php开发web程序,MVC模式的入口设计非常关键,常用的著名开源程序如wordpress、discuz都有特别的入口设计,除了要做controller 、action ,路由设计也是被重视的,因为这关系到url的美观和搜索友好度。
wordpress的入口和伪静态
以wordpress 为例,http://it.liuhuafang.com/code/2982? ,我们很容易分析到code是分类的alias,2982是文章的id;而http://edu.xiguagg.com/2013-xigua-collage/ 西瓜学院的链接也是一个文章,采用了文章的alias。
网友打开http://it.liuhuafang.com/code/2982 、http://edu.xiguagg.com/2013-xigua-collage/,wordpress的入口文件index.php就要调用入口分析和控制的类,因为url是指向唯一页面的,具有唯一性,否则浏览器也没法运行,所以,数据库设计时候就必要将参与url rewrite 和路由的字段值进行筛选。wordpress选取了category、post_id、post_name等等几个可参与路由的字段,通过设置permalink 固定连接来固化url显示的类型。
wordpress 有rewrite函数来处理多种可能设置的permalink类型的前端请求判断机制,以便能够通过各种Url中提取到关键信息字段值,比如http://edu.xiguagg.com/2013-xigua-collage/ ,wordpress要从permalink设置中的post_name字段中进行搜索2013-xigua-collage,然后返回post_id,这样就可以通过post_id调用出文章内容页面需要展示的各类相关信息。
discuz的入口设计和伪静态
discuz是多入口文件模式设计,他们把portal、forum、group、home相对独立地进行入口控制和引导,这带来了伪静态的复杂性,所以官方默认伪静态规则设为http://www.xxx.com/forum-7-1.html这样的参数裸露式的url,而没有去像wordpress那样去做permalink池的关联设计。当然,这跟产品的需求不同,wordpress多用于个人博客,访问请求量通常很少;而discuz用于讨论区,存在很多并发请求,访问量规模一旦提升,对于成本来说是一个大挑战,所以,discuz选择了牺牲个性化的这种方式。
当然,很多站长开始喜欢简洁网址,比如http://bbs.jiabao.org/baixing/1 ,这是域名+分类别名+帖子id的目录化伪静态结构,如果网站信息量很大,这种结构让网站内部结构扁平一些,会搜索友好一些,但是,这是违背discuz路由系统设计的思想的。
你打开家宝社区的链接,你会发现,这是一个帖子,而事实上,你社区首页版块分类点击版块竟然到了这里,这是什么原因呢?
因为追求扁平化目录,bbs.jiabao.org/baixing/2 这样的url可能是某版块的分页,也可能是某个帖子链接,这样子入口选择无法判别路由了,如何解决这个问题呢?
我在想:一方面,将帖子url改成 bbs.jiabao.org/baixing/2.html? 与版块的rewrite规则进行区别,如果就是非要bbs.jiabao.org/baixing/222 这样的帖子url,要么开始把其实thread id 通过sql 弄个很大的位数,让版块fid永远不太可能和帖子tid 的值冲突;要么,就要改动程序设计,将版块和帖子的重写规则进行唯一性绑定,解决入口无法识别出是主题列表分页url还是帖子url。
论坛主题列表页 | {fid}, {page} |
论坛主题内容页 | {tid}, {page}, {prevpage} |
终极的解决方案应该是这样:将主题链接和主题内容的rewrite前段入口和路由控制统一,解决版块首页的clean url ,分页加上page等辅助参数,这样bbs.jiabao.org/baixing/2 从浏览器提交到服务器,htaccess 识别引导到动态Php入口文件,并指向帖子页面。所以,{fid}/t{tid}/{page} 、{fid}/{page} 帖子页面和主题链接页面必须有个带辅助参数,否则fid tid进行严格唯一性去重,对fid tid进行搜索式匹配,当然,这相对会损耗一些性能。
原文地址:php 学习笔记:MVC 入口设计与伪静态, 感谢原作者分享。

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



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
