创建文件
首先,我们将创建index.php、router.php 和.htaccess 文件。
将所有请求重定向到.htaccess 中的index.php
1 2 3 4 | RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L,QSA]
|
登录后复制
防止直接访问router.php
如果用户直接输入访问router.php的url,此代码将显示404响应。
1 2 3 4 5 | <?php
if ( basename ( $_SERVER [ 'PHP_SELF' ]) == basename ( __FILE__ )) {
http_response_code(404);
die ();
}
|
登录后复制
在router.php中添加Page类
1 2 3 4 | class Page
{
protected static bool $Return_404 = true;
}
|
登录后复制
创建一个静态布尔变量用于返回 404 页面。我们将其默认设置为 true。
现在我们为404页面添加一个功能。
1 2 3 4 | protected static function Return_404(): void
{
( file_exists ( "./Pages/404.php" )) ? require_once "./Pages/404.php" : http_response_code(404);
}
|
登录后复制
在这里,我将 404 页面放入 Pages 文件夹中。您可以将其放入任何您想要的文件夹中。
我们还将添加“文件”功能。
1 2 3 4 5 6 7 8 | protected static function File(string $file ): string
{
if (! empty ( $file )) {
(str_contains( $file , "?" )) ? $file = strtok ( $file , '?' ) : $file ;
( $file [ strlen ( $file ) - 1] === "/" ) ? $file = rtrim( $file , "/" ) : $file ;
}
return $file ;
}
|
登录后复制
此函数将检查请求网址是否包含查询字符串或以“/”结尾并将其删除。
我使用“strok”字符串函数来获取“?”之前的字符串值。我知道“strok”不应该这样使用,但它确实有效,可以让我免于不必要地执行复杂的算法。我使用“rtrim”字符串函数来删除“/”(如果它包含在字符串的最后)。
在router.php中添加Routes类
1 2 3 4 5 6 7 | class Routes
{
public static array $Route = array (
"" => "Pages/home.php" ,
"/about" => "Pages/about.php" ,
);
}
|
登录后复制
在这里,我创建了一个静态数组来存储路由。
该数组包含“Requested url”=> “文件位置”。
我把所有页面文件放在Pages文件夹中。你可以把它们放在任何你想要的地方。
在router.php中添加Router类
Router 类将从我们上面创建的 Page 类扩展。
注意“”=>; “Pages/home.php”,用于主页。
1 2 3 4 | class Router extends Page
{
}
|
登录后复制
现在,我们将编写检查请求的文件是否存在于 Router 类中的函数。
1 2 3 4 5 6 7 8 9 10 11 12 13 | public static function Run(): void
{
$requested_file = self::File( $_SERVER [ "REQUEST_URI" ]);
foreach (Routes:: $Route as $request => $file ) {
if ( $requested_file === $request ) {
if ( file_exists ( $file )) {
self:: $Return_404 = false;
require $file ;
} else echo "Error" ;
}
}
if (self:: $Return_404 ) self::Return_404();
}
|
登录后复制
该函数首先检查请求的文件是否在 $Route 数组中。如果存在,则将静态布尔值 $Return_404 设置为 false,以便 404 页面不会显示并获取页面文件。如果不存在,将返回 404 页面。
如果存在但Pages文件夹中没有文件,该函数将回显“错误”。您可以在此处显示 404 页面而不是 echo“Error”。
最终的 router.php 文件将如下所示。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | <?php
if ( basename ( $_SERVER [ 'PHP_SELF' ]) == basename ( __FILE__ )) {
http_response_code(404);
die ();
}
class Page
{
protected static bool $Return_404 = true;
protected static function Return_404(): void
{
( file_exists ( "./Pages/404.php" )) ? require_once "./Pages/404.php" : http_response_code(404);
}
protected static function File(string $file ): string
{
if (! empty ( $file )) {
(str_contains( $file , "?" )) ? $file = strtok ( $file , '?' ) : $file ;
( $file [ strlen ( $file ) - 1] === "/" ) ? $file = rtrim( $file , "/" ) : $file ;
}
return $file ;
}
}
class Router extends Page
{
public static function Run(): void
{
$requested_file = self::File( $_SERVER [ "REQUEST_URI" ]);
foreach (Routes:: $Route as $request => $file ) {
if ( $requested_file === $request ) {
if ( file_exists ( $file )) {
self:: $Return_404 = false;
require $file ;
} else echo "Error" ;
}
}
if (self:: $Return_404 ) self::Return_404();
}
}
class Routes
{
public static array $Route = array (
"" => "Pages/home.php" ,
"/about" => "Pages/about.php" ,
);
}
|
登录后复制
索引.php
在index.php中,我们将使用Router类中的“Run”函数。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <!DOCTYPE html>
<html lang= "en" >
<?php
require_once "./router.php" ;
?>
<head>
<meta charset= "UTF-8" >
<meta name= "viewport" content= "width=device-width, initial-scale=1.0" >
<title>Router</title>
</head>
<body>
<?php
Router::Run();
?>
</body>
</html>
|
登录后复制
页面中的所有 HTML 代码都将位于“body”标签中。
创建页面
最后,在Pages文件夹中创建home.php、about.php和404.php。
home.php
about.php
404.php
检查代码是否与“xampp”或“PHP server”扩展一起工作。
如果您遇到错误,请随时告诉我。 XD
您还可以在以下位置检查 github 存储库:
时银
/
页面路由器
来自 php-router 存储库的手动路由器
以上是用 PHP 创建一个简单的页面路由器的详细内容。更多信息请关注PHP中文网其他相关文章!