如何解决用户浏览index.php的情况
在 Zend 框架中,使用 MVC,如果用户显式浏览 http://base/url/index.php< /a> 而不是http://base/url,系统认为真正的基本url是http://base/url/index.php/ 并据此计算系统中的所有URL。
所以,如果我有一个控制器 XXX 和操作 YYY 链接将是
http://base/url/index.php/XXX/YYY 这是当然错了。
我目前正在通过在index.php添加一行来解决这个问题:
$_SERVER["REQUEST_URI"]=str_replace('index.php','',$_SERVER["REQUEST_URI"]);
我想知道ZF中是否有内置的方法来解决这个问题。
In Zend framework, using the MVC, if A user surf explicitly to http://base/url/index.php instead of just http://base/url, The system thinks the real base url is http://base/url/index.php/ and according to that calculates all the URLs in the system.
So, if I have a controller XXX and action YYY The link will be
http://base/url/index.php/XXX/YYY which is of course wrong.
I am currently solving this by adding a line at index.php:
$_SERVER["REQUEST_URI"]=str_replace('index.php','',$_SERVER["REQUEST_URI"]);
I am wondering if there is a built-in way in ZF to solve this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以通过使用
Zend_Controller_Router_Route_Static
(唷!),示例:阅读上面链接的手册页,可以找到一些非常好的示例。
不能说我完全不同意你的做法。 也就是说,其他人很可能会指出 5000 个左右的缺点。 祝你好运。
You can do it with ZF by using
Zend_Controller_Router_Route_Static
(phew!), example:Read the manual page linked above, there are some pretty good examples to be found.
Can't say I totally disagree with your approach. That said, others may well point out 5000 or so disadvantages. Good luck with that.
好吧,这实际上取决于您想如何解决这个问题。 如您所知,Zend Frameworks 构建在前端控制器模式上,其中未显式引用 /public 目录中文件的每个请求都将重定向到 index.php。 因此,您基本上可以通过多种方式解决此问题:
Well it really depends on how you want to solve this. As you know the Zend Frameworks build on the front controller pattern, where each request that does not explicitly reference a file in the /public directory is redirected to index.php. So you could basically solve this in a number of ways:
使用mod_rewrite。 像这样的事情应该这样做:
Use mod_rewrite. Something like this should do it:
我认为你不应该使用路线来做到这一点。
这是一个普遍问题,不应该通过这种方式解决。
您最好应该在 .htaccess 中执行此操作,这将为您提供更好的效果。 将用户重定向到您想要的位置的更简单的方法,例如错误页面或索引。
这是 mod_rewrite 的文档页面
I don't think you should use a route to do this.
It's kind of a generic problem which shouldn't be solved by this way.
You better should have to do it in your .htaccess, which will offer you a better & easier way to redirect the user to where you want, like to an error page, or to the index.
Here is the documentation page for the mod_rewrite
我从来没有使用 Zend Framework 遇到过这个问题。 只是不要链接到index.php 文件。 就是这样。 当您向用户提供应用程序的地址时,只需告诉他们转到 http://base/url/
当用户输入 http://base/url/ 时,她的请求 URI 是base/url 并且您的 .htaccess 文件将请求路由到index.php,但请求IS base/url。 您不需要从请求中删除“index.php”。 因为它不在那里。
当您尝试生成链接和表单的 URL 等时,请使用内置的 url() 视图帮助程序来生成链接。 像这样:
不用担心链接。 Zend 将为您生成一个 URL。
I've never faced this problem using Zend Framework. just do not link to index.php file. that's it. and when your are giving your application's address to users, just tell them to go to http://base/url/
when the user enters http://base/url/ her request URI is base/url and your .htaccess file routs the request to index.php, but the request IS base/url. you do not need to remove 'index.php' from the request. because it is not there.
when you are trying to generate URLs for links and forms and ..., use the built-in url() view helper to generate your links. like this:
do not worry about the link. Zend will generate a URL for you.
我的看法是,如果我有一个由 PHP 提供支持的网站,并且用户访问 http://site/index .aspx 然后我会发送 404。
尽管理论上 index.php 确实存在,但它在我的应用程序中不是有效的 URL,因此在这种情况下我也会发送 404。
The way I look at this is that if I have a website powered by PHP and a user goes to http://site/index.aspx then I would send a 404.
Even though index.php does exist in theory, it's not a valid URL in my application so I would send a 404 in this case too.