Zend如何使用mod_rewrite在前端控制器中解析URL? [英] How does Zend parse URL in a front controller using mod_rewrite?

查看:53
本文介绍了Zend如何使用mod_rewrite在前端控制器中解析URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道Zend框架如何控制路线.我实际上是在尝试创建自己的控制器/路由类,编写了与Zend建议的.htaccess相同的代码,以重写为位于网站根目录中的index.php:

I can't figure out how the Zend Framework controls routes. I am actually trying to create my own controller/route class, wrote the same code as Zend reccomends for .htaccess to rewrite to an index.php located in the root of the website:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

我不明白的那一行是最后一条.它没有捕获组和$变量.在index.php中,我无法捕获$ _GET中的任何内容,后者为空.唯一可行的方法是:

The line I don't understand is the last one. It doesn't have capturing groups and $ variables. In index.php I can't catch anything in $_GET, which is empty. The only way it would work would be:

RewriteRule ^(.*)$ index.php?uri=$1 [NC,L]

然后解析$ _GET ['uri'],对我而言,其格式为"controller/action/param1/param1value".

and then parse $_GET['uri'], which for me it would be in the form "controller/action/param1/param1value".

但是最好解决这个问题,而不使用'uri'变量.在Zend项目中,这是可行的.有人可以解释一下Zend如何通过这种重写来解析URL吗?

But it would be nice to get around this and not use the 'uri' variable. In a Zend project, this just works. Can someone explain me how Zend parses the URL with this kind of rewrite ?

推荐答案

最后一条规则将文件实际上不存在的所有请求(请参阅上一条规则)转发到index.php,后者将加载Zend Framework并处理该请求.如果您跟踪Zend的功能,最终将发现您通过前端控制器进入Controller/Request/Http.php,该前端控制器使用多个$_SERVER变量来确定请求的内容.

The last rule forwards all requests where the file doesn't actually exist (see previous rule) to index.php which loads the Zend Framework and handles the request. If you trace through what Zend does with it, you'll find eventually that you get to Controller/Request/Http.php through the Front Controller which uses several of the $_SERVER variables to determine what the request was for.

我建议您编写自己的index.php并检查$_SERVER中的元素,并查看Controller/Request/Http.php中的代码.

I would recommend writing your own index.php and inspecting the elements in $_SERVER and reviewing the code in Controller/Request/Http.php.

    print_r($_SERVER);

这篇关于Zend如何使用mod_rewrite在前端控制器中解析URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆