用PHP正道路由页面请求 [英] Routing page requests with PHP the right way

查看:174
本文介绍了用PHP正道路由页面请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要知道的术语和表演网址导航正确的最佳做法?这样,类似于你如何计算器的路线,当你问通过URL一个问题:

I need to know the term and best practices of performing site navigation the "right"? way, similar to how stackoverflow routes you when you ask a question via the url:

http://stackoverflow.com/questions/ask

"http://stackoverflow.com/questions/ask"

在哪里与我的PHP编程知识我可能会code将其像这样:

Where as with my knowledge of PHP programming I would probably code it like so:

http://stackoverflow.com/index.php?p=questions&act=ask

"http://stackoverflow.com/index.php?p=questions&act=ask"

希望你明白我的意思。我想知道的页面导航和请求/响应处理的这种方法来看,如果可能的最佳实践,限制或其他任何我需要记住使用此标准/方法设计Web应用程序时。我甚至不知道这是不是所有的PHP或ASP或Ruby或者你有什么一些网站后台codeD完成,所以我已经填充标签与我的猜测。

Hopefully you understand what I mean. I would like to know the term for this method of page navigation and request/response handling, and if possible the best practices, limitations, or anything else I need to keep in mind when designing a web application using this standard / method. I also don't even know if this is all done with PHP or some web backend coded in ASP or Ruby or what have you, so I have populated the tags with my guesses.

推荐答案

这是最MVC框架中使用的模式是一个前端控制器调用一台路由器上。前端控制器是一个典型的的index.php 在Web根。接着,不进行现有的文件(如JS,CSS和图片资源)的所有请求需要被发送到该控制器。在Apache中,你可以做到这一点 mod_rewrite的

The pattern that most MVC frameworks use is a front controller that calls on a router. The front controller is typically an index.php in your web root. Next, all requests that aren't for existing files (like js, css, and image assets) need to be sent to this controller. In apache, you can do this with mod_rewrite:

RewriteRule ^index\.php$ - [F]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /index.php [L,QSA]

不过,在Apache的2.5推荐的方法是用<一个href=\"http://httpd.apache.org/docs/trunk/mod/mod_dir.html#fallbackresource\">FallbackResource:

FallbackResource index.php

IIS有类似的功能如果这是你使用的是什么

IIS has similar functionality if that's what you're using.

的index.php ,您可以访问最初 $请求的URL _ SERVER ['REQUEST_URI'] 。你应该包括你的路由器(这应该是外部的Web根目录),并与请求的URI调用它。例如:

In index.php, you can access the URL originally requested with $_SERVER['REQUEST_URI']. You should include your router (which should be outside of the web root) and invoke it with the request URI. Example:

require '../router.php';

$router = new Router();
$router->process($_SERVER['HTTP_METHOD'], $_SERVER['REQUEST_URI'], $_GET, $_POST);

那么你的路由器可以找到相应的控制器将请求路由到。了解更多关于 MVC框架和研究的一些 范例更好地了解其他人如何实现它。

Then your router can find the appropriate controller to route the request to. Read more on the MVC framework, and study some examples to better understand how others have implemented it.

这篇关于用PHP正道路由页面请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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