Mod-Rewrite or PHP router?(Mod-Rewrite 还是 PHP 路由器?)
问题描述
I am debating routing my requests with one of the two options:
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]
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?
any other ideas?
NOTE: I am not running a known framework. I am building my own MVC pattern to learn it.
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.
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 路由器?
基础教程推荐
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- 在多维数组中查找最大值 2021-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01