所以我刚刚开始学习 PHP,并且正在制作一个本地项目来测试它。
我正在使用内置的网络服务器 https://www.php.net/manual/en/features.commandline.webserver.php 使用命令php -S localhost:8000 router.php
我想导航到我的 test.php 文件,该文件位于我的 index.php 文件旁边http://localhost:8000/test
但只有当我使用时它才有效http://localhost:8000/test.php
在内置的网络服务器页面上,它提到了如何提供 .el 文件
所以我想出了
<?php
// router.php
$pathName = $_SERVER['REQUEST_URI'];
$pathName = str_replace("/", "\\", $pathName);
$file = __DIR__ . $pathName . ".php";
if(file_exists($file)){
include($file);
}else{
return false;
}
?>
但这似乎不对。
我该如何以正确的方式去做呢?有人能指出我正确的方向吗?在 stackoverflow 上找不到任何内容,但这可能只是因为我不知道要搜索什么。
编辑:我确实想要包含功能而不是读取文件,我只是想知道检查 .php 文件是否存在的方法是否正确。
慕尼黑8549860