Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
594 views
in Technique[技术] by (71.8m points)

routing - Mod-Rewrite or PHP router?

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&params=$1    [L,QSA]
// ..more custom routes, and then a default route
RewriteRule    ^([A-Za-z]+)/([A-Za-z]+)/(.*)?$    index.php?rt=$1/$2&params=$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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...