简单的MVC mod-rewrite [英] Simple MVC mod-rewrite
问题描述
我不确定如何对模块化MVC结构进行mod-rewrite.我想发生的是URL捕获:
I'm not sure how to do a mod-rewrite for a modular MVC structure. What I want to happen is the URL captures:
http://domainname.com/index.php?model={model}&view={view}¶meters={parameters}
注意:参数将按特定顺序排列,并以竖线分隔(除非有更好的方法):
参数= param1 | param2 | param3
http://domainname.com/{model}/{view}/{parameters}
http://domainname.com/index.php?model={model}&view={view}¶meters={parameters}
NOTE: parameters will be in a specific order and separated by pipes (unless there is a better way):
parameters=param1|param2|param3
http://domainname.com/{model}/{view}/{parameters}
示例:
http://domainname.com/faq/edit/13
另一个例子:
http://domainname.com/faq/index/{sort}/{page}/{search}
http://domainname.com/faq/index/asc/3/How+to
http://domainname.com/faq/index/{sort}/{page}/{search}
http://domainname.com/faq/index/asc/3/How+to
基本上,模型和视图之后的所有内容都可以并且可以作为参数;根据需要.对于每个视图,我都会知道该区域允许的可能参数以及以什么顺序排列.
Essentially anything after the model and view will and can be parameters; as many as needed. For each view I will know the possible parameters that area allowable and in what order.
谢谢.
-
使用下面的代码,这就是我所拥有的:
Using the code below this is what I have:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)/(.*) index.php?model=$1&view=$2¶meters=$3 [L,NS]
URL:http://localhost:8888/testing/faq/index/asc/5/How+to
PHP $ _GET变量:
URL: http://localhost:8888/testing/faq/index/asc/5/How+to
PHP $_GET variables:
Array
(
[model] => faq/index/asc
[view] => 5
[parameters] => How to
)
应该是:
Array
(
[model] => faq
[view] => index
[parameters] => asc/5/How to
)
请帮助
推荐答案
您可以使用PHP函数来做到这一点.快速,没有默认值或错误处理:
You could use a PHP function to do that. Quickly, with no default value or error handling:
list( $controller, $function, $params ) = explode( '/', $uri, 3 );
$params = explode( '/', $uri );
然后在.htaccess中,将所有不存在的查询重定向到您的PHP查询处理文件:
And in the .htaccess, redirect any non-existing query to your PHP query-handling file:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ mvc.php [L,NS]
请注意,尽管要过滤输入内容,但不要包含任何文件等.
Beware to filter your input though, not to include any file, etc.
这篇关于简单的MVC mod-rewrite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!