2014-10-29 26 views
0

以下代碼展示了一種方法,目前我正在通過index.php呈現我的頁面。問題是我不知道如何重新考慮這個,所以我可以在模板被包含之前傳遞頁面標題。從包含的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

短,你不能。通常的做法是分離邏輯和表示,因此所有的數據都是在向瀏覽器輸出之前定義的。 – Steve 2014-10-29 22:38:19

回答

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"); 
+0

我認爲這是一個好主意。未來還會節省一些編碼。 – HelpNeeder 2014-10-30 05:52:53