PHP路由的基础 [英] The basics of php routing
问题描述
我正在寻找有关如何进行非常基本的php路由的教程或解释.
I'm looking for a tutorial or explaination on how to do very basic php routing.
例如,当我访问诸如mywebsite.com/users之类的链接时,我想运行route类的get方法来提供数据,就像laravel一样.
For example when I visit a link like: mywebsite.com/users I want to run the get method of a route class to provide the data, in the same way laravel does it.
Route::get('users', function()
{
return 'Users!';
});
有人可以解释如何执行此操作或向我提供更多信息吗?
Can somebody explain how to do this or provide me with some more information?
推荐答案
在最常见的配置中,PHP依赖Web服务器进行路由.这是通过将请求路径映射到文件来完成的:如果您请求www.example.org/test.php,则Web服务器实际上将在预定义目录中查找名为test.php的文件.
In its most common configuration, PHP relies on the web server to do the routing. This is done by mapping the request path to a file: If you request www.example.org/test.php, the web server will actually look for a file named test.php in a pre-defined directory.
有一个方便使用的功能可满足我们的需求:许多Web服务器还允许您调用www.example.org/test.php/hello,它会仍然执行test.php. PHP使通过$_SERVER['PATH_INFO']
变量可访问请求路径中的其他内容.在这种情况下,它将包含"/hello".
There is a feature that comes in handy for our purpose: Many web servers also allow you to call www.example.org/test.php/hello and it will still execute test.php. PHP makes the additional stuff in the requested path accessible via the $_SERVER['PATH_INFO']
variable. In this case it would contain "/hello".
使用它,我们可以构建一个非常简单的路由器,如下所示:
Using this, we can build a very simple router like this:
<?php
// First, let's define our list of routes.
// We could put this in a different file and include it in order to separate
// logic and configuration.
$routes = array(
'/' => 'Welcome! This is the main page.',
'/hello' => 'Hello, World!',
'/users' => 'Users!'
);
// This is our router.
function router($routes)
{
// Iterate through a given list of routes.
foreach ($routes as $path => $content) {
if ($path == $_SERVER['PATH_INFO']) {
// If the path matches, display its contents and stop the router.
echo $content;
return;
}
}
// This can only be reached if none of the routes matched the path.
echo 'Sorry! Page not found';
}
// Execute the router with our list of routes.
router($routes);
?>
为简单起见,我没有将路由器定为一类.但是从现在开始,这也不应该成为问题.
For the sake of simplicity, I did not make the router a class. But from here on, that shouldn't be a problem either.
假设我们将这个文件命名为index.php.现在,我们可以调用www.example.org/index.php/hello来获得一个不错的"Hello,World!".信息.或www.example.org/index.php/进入主页.
Let's assume we named this file index.php. We can now call www.example.org/index.php/hello to get a nice "Hello, World!" message. Or www.example.org/index.php/ to get the main page.
该URL中的"index.php"仍然很丑陋,但是我们可以使用URL重写来解决此问题.在Apache HTTPD中,您可以将.htaccess
文件放在具有以下内容的相同目录中:
The "index.php" in that URL is still ugly, but we can fix this by using URL rewriting. In Apache HTTPD you would put a .htaccess
file in the same directory with the following content:
RewriteEngine on
RewriteRule ^(.*)$ index.php/$1
您在那里!您自己的路由器,逻辑代码少于10行(不计算注释和路由列表).
And there you are! Your very own router with under 10 lines of logic code (not counting comments and the routes list).
这篇关于PHP路由的基础的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!