如何解决用户浏览index.php的情况

发布于 2024-07-22 07:02:16 字数 672 浏览 9 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

摘星┃星的人 2024-07-29 07:02:17

您可以通过使用 Zend_Controller_Router_Route_Static(唷!),示例:

阅读上面链接的手册页,可以找到一些非常好的示例。

$route = new Zend_Controller_Router_Route_Static(
    'index.php',
    array('controller' => 'index', 'action' => 'index')
);
$router->addRoute('index', $route);

不能说我完全不同意你的做法。 也就是说,其他人很可能会指出 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.

$route = new Zend_Controller_Router_Route_Static(
    'index.php',
    array('controller' => 'index', 'action' => 'index')
);
$router->addRoute('index', $route);

Can't say I totally disagree with your approach. That said, others may well point out 5000 or so disadvantages. Good luck with that.

小兔几 2024-07-29 07:02:17

好吧,这实际上取决于您想如何解决这个问题。 如您所知,Zend Frameworks 构建在前端控制器模式上,其中未显式引用 /public 目录中文件的每个请求都将重定向到 index.php。 因此,您基本上可以通过多种方式解决此问题:

  1. 编辑 .htaccess 文件(或服务器配置指令)以将请求重写为所需的请求:
    • RewriteRule (.*index.php) /error/forbidden?req=$1 // 重写错误控制器禁止的操作。
    • RewriteRule index.php /index // 重写对主控制器的请求和操作
  2. 按照 karim79

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:

  1. Edit the .htaccess file (or server configuration directive) to rewrite the request to the desired request:
    • RewriteRule (.*index.php) /error/forbidden?req=$1 // Rewrite to the forbidden action of the error controller.
    • RewriteRule index.php /index // Rewrite the request to the main controller and action
  2. Add a static route in your bootstrapper as suggested by karim79.
独自唱情﹋歌 2024-07-29 07:02:17

使用mod_rewrite。 像这样的事情应该这样做:

RewriteRule ^index.php/(.*)$ /$1 [r=301,L]

Use mod_rewrite. Something like this should do it:

RewriteRule ^index.php/(.*)$ /$1 [r=301,L]
久而酒知 2024-07-29 07:02:17

我认为你不应该使用路线来做到这一点。

这是一个普遍问题,不应该通过这种方式解决。

您最好应该在 .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

ヅ她的身影、若隐若现 2024-07-29 07:02:17

我从来没有使用 Zend Framework 遇到过这个问题。 只是不要链接到index.php 文件。 就是这样。 当您向用户提供应用程序的地址时,只需告诉他们转到 http://base/url/

当用户输入 http://base/url/ 时,她的请求 URI 是base/url 并且您的 .htaccess 文件将请求路由到index.php,但请求IS base/url。 您不需要从请求中删除“index.php”。 因为它不在那里。

当您尝试生成链接和表单的 URL 等时,请使用内置的 url() 视图帮助程序来生成链接。 像这样:

// in some view script
<a href="<?php
  echo $this->url( array('controller'=>'targetController','action'=>'targetAction') );
         ?>" >click</a>

不用担心链接。 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:

// in some view script
<a href="<?php
  echo $this->url( array('controller'=>'targetController','action'=>'targetAction') );
         ?>" >click</a>

do not worry about the link. Zend will generate a URL for you.

命硬 2024-07-29 07:02:17

我的看法是,如果我有一个由 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文