1

我当前的项目通过 index.php 路由所有内容。然后我看看是什么$_SERVER['REQUEST_URI'],检查白名单,然后执行一些逻辑来包含相关的 html 模板。像这样:

<?php

include("header.html");

$requested_page = $_SERVER['REQUEST_URI'];

if ($requested_page in $whitelist){
    $page_title = $requested_page;
    include ($requested_page . "_controller.php");
    include ($requested_page . "_template.html");
}

include ("footer.html")

?>

我的问题是我想$page_title在标题模板中使用,像这样......

<title><?php echo $page_title?></title>

但显然它不会起作用,因为当包含标题时, $page_title 尚未设置。谁能建议如何解决这个问题?“输出缓冲”可能有帮助吗?如果是这样,怎么做?如果这种使用 php 生成页面的方式存在重大问题,请说出来!谢谢

编辑:我应该补充一点,条件逻辑在我的实际项目中要复杂得多,我在这里简化了很多。我不想一直重复我对页眉和页脚的包含,所以这就是不包含它们的原因。如果可能的话,我真的宁愿让他们远离所有的逻辑。

4

5 回答 5

3

您可以重新排序代码以实现此目的:

<?php 
$requested_page = $_SERVER['REQUEST_URI'];

if ($requested_page in $whitelist){
    $page_title = $requested_page;
    include("header.html");
    include ($requested_page . "_controller.php");
    include ($requested_page . "_template.html");
}else{
    //Make sure we include header if page not in white list
    include("header.html");
}

include ("footer.html")
?>

编辑:实际上考虑一下,您可能可以将逻辑和包括解耦:

<?php 
$requested_page = $_SERVER['REQUEST_URI'];
$includes = array();
$includes[] = 'header.html';
if ($requested_page in $whitelist){
    $page_title = $requested_page;
    $includes[] = $requested_page . "_controller.php";
    $includes[] = $requested_page . "_template.html";
}

$includes[] = "footer.html";
foreach($includes as $include){
    include($include);
}
?>
于 2013-01-31T22:29:36.720 回答
1

我为我的加载 MVC 库编写了这个来加载视图,但我认为这会变得很方便:

$target = "myfile.php";

$data = array(
  'page_title' => 'page title'
);

ob_start();
if(count($data) > 0) extract($data);
include_once($target);
$content = ob_get_clean();

echo $content;

并且您可以使用函数将$data键用作变量。extract()

于 2013-01-31T22:38:13.163 回答
0

简单的

<?php 
$requested_page = $_SERVER['REQUEST_URI'];

$page_title = "";

if ($requested_page in $whitelist) $page_title = $requested_page;

include("header.html");

if(!empty($page_title)) {
    include ($requested_page . "_controller.php");
    include ($requested_page . "_template.html");
}

include ("footer.html")
?>
于 2013-01-31T22:43:16.567 回答
0

关于什么..?

<?php 
$page_title = $requested_page = $_SERVER['REQUEST_URI'];
if (!in_array($requested_page, $whitelist)) {
    header('Location: http://example.com/your404');
    exit(0);
}

include 'header.html';
include "$requested_page_controller.php";
include "$requested_page_template.html";
include 'footer.html';
于 2013-02-01T01:19:10.583 回答
-1

尝试使用带有页面标题index.php$_POST或重定向回您的页面。$_GET

如果设置了这些,您可以从页面的开头阅读它们。

<?php

if (isset($_GET['pagetitle'])){
  echo "<html><head><title>".$_GET['pagetitle']."</title></head>";
}

include("header.html");

$requested_page = $_SERVER['REQUEST_URI'];

if ($requested_page in $whitelist){
    $page_title = $requested_page;
    include ($requested_page . "_controller.php");
    include ($requested_page . "_template.html");
    header( 'Location: http://www.yoursite.com/index.php?pagetitle='.
           urlencode('This is the page title'),'"' ) 
}

include ("footer.html")

?>
于 2013-01-31T22:23:39.093 回答