这是一种可以接受的用php自动加载html页面的方法吗?
在一个简单的MVC上工作,如果你可以这么说的话。无论如何,我使用一个控制器类来自动加载与$\u GET变量匹配的html文件。如要加载名为page3.php的文件,请转到浏览器中的/?page3。以下是我一直在编写的脚本:这是一种可以接受的用php自动加载html页面的方法吗?,php,Php,在一个简单的MVC上工作,如果你可以这么说的话。无论如何,我使用一个控制器类来自动加载与$\u GET变量匹配的html文件。如要加载名为page3.php的文件,请转到浏览器中的/?page3。以下是我一直在编写的脚本: public function __construct(){ // This makes configs and many // global variables available include $_SERVER['DOCUMENT_RO
public function __construct(){
// This makes configs and many
// global variables available
include $_SERVER['DOCUMENT_ROOT'] . '/config.php';
// Homepage will be included if there is no $_GET
if($_GET){
// Check the name of each $_GET in the users URL
foreach ($_GET as $key => $value) {
/* Check if there is a file
* named after the input $_GET.
* Also, make sure there's no hanky panky with the input */
if(preg_match ("/^[a-zA-Z\s]+$/", $key) && file_exists('html/' . $key . '.php')){
// Include the file named after the input $_GET
include 'html/' . $key . '.php';
}
// Include homepage if there is no matching file
else {
include 'html/home.php';
}
// Break loop to stop multiple files from loading
break;
}
}
// Include homepage if there is no $_GET set
else {
include 'html/home.php';
}
简而言之:
ob_start();
include ('html/home.php');
$fileContent = ob_get_contents();
ob_end_clean();
它从文件中加载数据并捕获它,然后您可以只回显“$fileContent”。仅供参考,该正则表达式不会匹配
“page3”
,因为您只是检查字母和空格。啊,是的,忘了,只是以page3为例。页面名称将只包含字母。只包含空格是因为我不完全理解正则表达式,所以使用了一个示例请检查:因为你想像我假设的那样重定向到此页面?你没有问题吗?你只是想分享一下?:-)问题是索引文件有一个类的自动加载程序(spl_autoload_register()),所以我希望能够为每个页面和类创建新文件。因此,如果我完全重定向到html页面,我需要重新初始化自动加载程序。想法?这有什么好处?我已经用它来加载PHP文件,而且据我记忆所及,这个文件中仍然有可用的PHP值。我必须承认这是很久以前的事了,但它仍然可以正常工作。