这是用php自动加载html页面的可接受方法吗?

时间:2018-06-20 14:48:41

标签: php

如果您甚至可以将其称为简单MVC。无论如何,我正在使用控制器类来自动加载与$ _GET变量匹配的html文件。就像在浏览器中转到/?page3加载一个名为page3.php的文件一样。这是我一直在处理的脚本:

    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';
    }

1 个答案:

答案 0 :(得分:0)

简短内容:

    ob_start();
    include ('html/home.php');
    $fileContent = ob_get_contents();
    ob_end_clean();

它从文件中加载数据并捕获它,然后您可以回显“ $ fileContent”。