PHP MVC +路由器和重定向问题
我有一个奇怪的问题,我一生都无法弄清楚。首先我从头开始开发了一个PHP MVC框架。我遇到的问题是在 exit() 调用后得到标头重定向执行。
下面是路由过程的高级视图:
- REQUEST_URI 被剥离,请求的控制器、方法和任何参数都设置为稍后在脚本中使用的变量。
- router.php 检查以确保请求的控制器是否确实存在,如果不存在,则返回 false,但如果一切检查正常,则返回 true。
- 如果返回 true,则执行请求的控制器、方法和任何参数。
- 如果返回 false,则路由器将发送重定向到自定义 404 页面。这使用了 PHP 的 header() 函数,并且 header("Location: *") 之后有一个 exit();
第4步就是出现问题的地方。请求的控制器存在,因此它被执行,并且请求的视图存在,因此它正确加载所有内容,但即使在成功加载视图后调用 exit(),它也会执行重定向。
路由器是否有任何原因会继续运行并触发重定向?
I have an odd issue that I can not for the life of me figure out. First of all I have developed a PHP MVC framework from scratch. The problem I am having is that I am getting a header redirect execution after an exit() call.
Here is a high level view of the routing process:
- REQUEST_URI is stripped apart and the requested controller, method, and any arguments are set to variables to be used later in the script.
- The router.php checks to make sure if the requested controller actually exists, if it doesn't then it returns false but if everything checks out then it returns true
- If it returns true, it executes the requested controller, method, and any arguments.
- If it returns false then the router will send a redirect to a custom 404 page. This uses PHP's header() function and there is an exit() after the header("Location: *");
Step 4 is where the problem occurs. The requested controller exists so it executed and the requested view exists so it loads everything correctly but then it executes the redirect even though an exit() is called after successfully loading the view.
Is there any reason why the router would continue on and fire the redirect?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不会猜测(根据 @Tomasz :),但即使您
exit()
浏览器也会尊重它获得的任何 http 标头,包括位置。事实上,在类似的事情中,我会在发生任何错误时发送重定向标头,然后执行
exit
以确保在重定向之后控制器中不会继续执行。I'm not going to guess (as per @Tomasz :), but even if you
exit()
the browser is going to respect any http headers it gets, including a Location.In fact in something similar I do, on any errors I send a redirect header and then do an
exit
to make sure execution doesn't continue in the controller after the redirect.