从包含的PHP文件传递数据的策略

时间:2014-10-29 22:02:11

标签: php oop

以下代码介绍了我目前通过index.php呈现网页的方式。问题是,我不确定如何重新考虑这一点,所以我可以在模板被包含之前传递页面标题。

我可以用其他方式做到这一点?这只是我的索引页面,请询问是否需要更多代码。

include($cms->GetTheme() . "/head.php");这应该在包含之前获取标题信息,但我不确定如何从后面包含的页面传递数据。

include('config/config.inc.php');

$cms = new cms();

if(($_SERVER['REQUEST_METHOD'] === 'GET' || $_SERVER['REQUEST_METHOD'] === 'POST') && !empty($_GET['page'])) {
    include($cms->GetTheme() . "/head.php");
    $cms->IncludeModule($_GET['page']); <- actual page being included
    include($cms->GetTheme() . "/foot.php");
} // end (GET || POST) && GET
else { // just index.php
    include($cms->GetTheme() . "/head.php");
    foreach($cms->GetModuleList() as $module) {
        echo " <a href=\"index.php?page=$module\"> $module </a><br />";
    }
    include($cms->GetTheme() . "/foot.php");
} // end ELSE

包含示例页面。 The $this->SetTitle($module_name);我会用来设置页面标题。

<?php
    $module_name        = 'Log out user';
    $module_directory   = 'admin';
    $this->SetTitle($module_name); // setting page title

    if(count(get_required_files()) < 2) {
        header('Location: index.php');
    }
    else {
        if(isset($_SESSION['user'])) {
            $this->DestroyUser();

            echo "You have been logged out! Please navigate to the <a href=\"index.php?page=login\">Login Page</a>.";
        }
        else {
            header('Location: index.php?page=login'); 
        }
    }
?>

1 个答案:

答案 0 :(得分:2)

到处都有回声。通过存储输出来尝试并限制您执行此操作的位置,而不是立即将其全部打印出来。

例如,在您的模块中,您可以执行$this->content = "You have been logged out..."

然后你可以改变执行顺序:

$cms->IncludeModule($_GET['page']);
include($cms->GetTheme() . "/head.php");
echo $cms->content;    
include($cms->GetTheme() . "/foot.php");