Mod-Rewrite or PHP router?(Mod-Rewrite 或 PHP 路由器?)
问题描述
我正在讨论使用以下两个选项之一路由我的请求:
I am debating routing my requests with one of the two options:
选项 1:使用 Mod-Rewrite 和漏斗编写的简单捕获路由 $_GET
路由到 index.php 以加载...
Option 1: simple capture route with Mod-Rewrite and funnel written $_GET
route to index.php for loading...
#default routing
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blog/([0-9]+)?$ index.php?rt=blog¶ms=$1 [L,QSA]
// ..more custom routes, and then a default route
RewriteRule ^([A-Za-z]+)/([A-Za-z]+)/(.*)?$ index.php?rt=$1/$2¶ms=$3 [L,QSA]
选项2:简单地将请求路由到Front Controller,并创建一个PHP路由类来处理路由...
Option 2: simply route requests to Front Controller, and create a PHP routing class to handle the routing...
#default routing
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?rt=$1 [L,QSA]
/* --- on front controller, process $_GET['rt'] --- */
归根结底,哪个会运行得更快、更容易保护、更容易维护?
at the end of the day, which will run faster, be easier to secure, and be easier to maintain?
还有其他想法吗?
注意:我没有运行已知的框架.我正在构建自己的 MVC 模式来学习它.
NOTE: I am not running a known framework. I am building my own MVC pattern to learn it.
推荐答案
通常在 MVC 框架中,这类事情通常最好由前端控制器(名为 index.php
或类似)处理.然后您使用 mod_rewrite 从所有 URL 中隐藏 index.php
,以便您的用户看到漂亮的干净路径.
Usually in MVC frameworks, this sort of thing is usually best handled by a front controller (named index.php
or the like). You use mod_rewrite to then hide index.php
from all of the URLs so your users see nice clean paths.
在 PHP 中处理比在 Apache 的重写指令中更容易处理.PHP 更灵活,更容易编写/理解.现在我想起来了,我不确定我是否曾经见过将 mod_rewrite 用作任何 Web 框架的唯一路由引擎.
It's also way easier to handle in PHP than in Apache's rewrite directives. PHP is much more flexible and easier to write/understand. I'm not sure I've ever seen mod_rewrite used as the sole routing engine for any web framework out there, now that I think of it.
你的第二段代码是你重写指令的方法.
Your second snip of code is the way to go for your rewrite directives.
这篇关于Mod-Rewrite 或 PHP 路由器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Mod-Rewrite 或 PHP 路由器?
基础教程推荐
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- PHP 守护进程/worker 环境 2022-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- 使用 PDO 转义列名 2021-01-01
- HTTP 与 FTP 上传 2021-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01