在PHP中包含标题时,如何添加内容?

在PHP中包含标题时,如何添加内容?,php,javascript,Php,Javascript,我正在建立我的第一个PHP网站之一,只是玩它。我将使用PHP includes,所以我只需更改一次我的元数据。目前,我拥有从doctype到结尾的所有内容 一位朋友说我不应该在header.php中包含,而是应该在每个页面中包含它,这样我就可以添加特定于页面的内容。这是一个好方法吗 目前,对于像标题这样的东西,我正在做 <title><?php echo $page_title; ?> 然后在每一页的顶部,我在做 <?php $page_title = 'Bl

我正在建立我的第一个PHP网站之一,只是玩它。我将使用PHP includes,所以我只需更改一次我的元数据。目前,我拥有从doctype到
结尾的所有内容

一位朋友说我不应该在header.php中包含
,而是应该在每个页面中包含它,这样我就可以添加特定于页面的内容。这是一个好方法吗

目前,对于像标题这样的东西,我正在做

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

然后在每一页的顶部,我在做

<?php $page_title = 'Blah'; ?> 


如果我是这样做的,我将如何添加特定于页面的Javascript文件?

我不知道,您是如何构建页面的。但在一个简单的站点上,可以使用如下关联数组:

$pages = array(
    'home' => array(
        'title' => 'Home',
        'metaDescription' => 'This is the home of my website',
        'metaKeywords' => 'A,B,C',
        'scripts' => '<script src="src/js/com.jquery/1.7/jquery-1.7.min.js"></script>',
        'content' => 'home.phtml'
    ),
    'page_1' => …
);
<head>
    […]
    <title><?php print $pages['home']['title']; ?></title>
    […]
    <?php print $pages['home']['scripts']; ?>
    […]
</head>
<body>
    <?php require '/pathToIncludes/' . $pages['home']['content]; ?> 
</body>
<?php 
    include("start.php");
    $page->title = "My special title";
    include("header.php");
    // The rest of my content
?>
<?php

$page = array(
    'title' => 'Title',
    'js' => array(
        'jquery.min.js',
    ),
    'copyright' => 'Copyright 2012',
);

if ( $needed ) {
    $page[ 'js' ][] = 'some-other.js';
}

include( 'header.php' );
include( 'content.php' );
include( 'footer.php' );


// header.php
<html>
    <head>
        <title><?= $page[ 'title' ]; ?></title>

        <? foreach ( $page[ 'js' ] as $js ) { ?>
            <script src="<?= $js"></script>
        <? } ?>

    </head>
$pages=数组(
'主'=>阵列(
“标题”=>“主页”,
'metaDescription'=>'这是我网站的主页',
“元关键字”=>“A、B、C”,
'脚本'=>'',
'content'=>'home.phtml'
),
“第1页”=>…
);
你可以这样回忆:

$pages = array(
    'home' => array(
        'title' => 'Home',
        'metaDescription' => 'This is the home of my website',
        'metaKeywords' => 'A,B,C',
        'scripts' => '<script src="src/js/com.jquery/1.7/jquery-1.7.min.js"></script>',
        'content' => 'home.phtml'
    ),
    'page_1' => …
);
<head>
    […]
    <title><?php print $pages['home']['title']; ?></title>
    […]
    <?php print $pages['home']['scripts']; ?>
    […]
</head>
<body>
    <?php require '/pathToIncludes/' . $pages['home']['content]; ?> 
</body>
<?php 
    include("start.php");
    $page->title = "My special title";
    include("header.php");
    // The rest of my content
?>
<?php

$page = array(
    'title' => 'Title',
    'js' => array(
        'jquery.min.js',
    ),
    'copyright' => 'Copyright 2012',
);

if ( $needed ) {
    $page[ 'js' ][] = 'some-other.js';
}

include( 'header.php' );
include( 'content.php' );
include( 'footer.php' );


// header.php
<html>
    <head>
        <title><?= $page[ 'title' ]; ?></title>

        <? foreach ( $page[ 'js' ] as $js ) { ?>
            <script src="<?= $js"></script>
        <? } ?>

    </head>

[…]
[…]
[…]

下面是我如何解决这个问题的:

  • start.php
    连接到我的数据库,包括我的类,设置我的所有默认元值 进入
    $页面
    对象并启动
    $\u会话
  • header.php
    根据
    $page
  • top.php
    包括
    start.php
    header.php
对于大多数文件,默认值都可以。我只包括
top.php
。对于需要特殊值的特殊情况,我的页面如下所示:

$pages = array(
    'home' => array(
        'title' => 'Home',
        'metaDescription' => 'This is the home of my website',
        'metaKeywords' => 'A,B,C',
        'scripts' => '<script src="src/js/com.jquery/1.7/jquery-1.7.min.js"></script>',
        'content' => 'home.phtml'
    ),
    'page_1' => …
);
<head>
    […]
    <title><?php print $pages['home']['title']; ?></title>
    […]
    <?php print $pages['home']['scripts']; ?>
    […]
</head>
<body>
    <?php require '/pathToIncludes/' . $pages['home']['content]; ?> 
</body>
<?php 
    include("start.php");
    $page->title = "My special title";
    include("header.php");
    // The rest of my content
?>
<?php

$page = array(
    'title' => 'Title',
    'js' => array(
        'jquery.min.js',
    ),
    'copyright' => 'Copyright 2012',
);

if ( $needed ) {
    $page[ 'js' ][] = 'some-other.js';
}

include( 'header.php' );
include( 'content.php' );
include( 'footer.php' );


// header.php
<html>
    <head>
        <title><?= $page[ 'title' ]; ?></title>

        <? foreach ( $page[ 'js' ] as $js ) { ?>
            <script src="<?= $js"></script>
        <? } ?>

    </head>

这还有一个额外的优点,即页面可以访问$\u会话和其他自定义类,并使用该信息修改标题,而不会出现“header ready sent”错误。例如,您可以验证一些用户输入并使用
location('header:foo.php')重定向它们或根据其登录状态更改标题


对于特定于页面的javascript包含,我将创建一个URL数组,以包含
$page->javascript
,然后放置如下内容:
$page->javascript[]=”http://example.com/js/myScript.js";介于
start.php
header.php

之间,这里发生的是您试图将逻辑注入到模板中,这并不一定是错误的,但它会产生更混乱、更难维护的代码。这个问题不仅与你的
标签有关,而且会随着你的页面变得越来越动态和复杂(动态导航、页面特定布局等)而不断出现


MVC方法完美地解决了这个问题。MVC由模型组成,这些模型与您的数据源(MySQL、Redis等)通信并处理您的逻辑;视图,呈现HTML;控制器,它们是模型和视图之间的粘合剂。用户发出的每个请求最终都会路由到控制器,然后路由到控制器中的操作方法(例如:
/users/login
可能映射到
用户
控制器和
登录
操作)


您可以在控制器中的action方法中设置页面标题(或任何其他元信息),控制器知道请求是什么,但在呈现视图之前会被调用:

$request->setTitle(“主页”);
然后在您的视图中,只需渲染它:


如果您刚刚开始学习PHP,那么现在是学习MVC的好时机,因为它会让您养成一些好习惯,这些习惯不仅会影响您的PHP开发,还会影响任何其他开发


我建议您查看它的简单性和优秀的文档。我使用CodeIgniter有一段时间了,但结果更符合我的需要。

我认为您应该先做一些逻辑,然后再渲染它。大概是这样的:

$pages = array(
    'home' => array(
        'title' => 'Home',
        'metaDescription' => 'This is the home of my website',
        'metaKeywords' => 'A,B,C',
        'scripts' => '<script src="src/js/com.jquery/1.7/jquery-1.7.min.js"></script>',
        'content' => 'home.phtml'
    ),
    'page_1' => …
);
<head>
    […]
    <title><?php print $pages['home']['title']; ?></title>
    […]
    <?php print $pages['home']['scripts']; ?>
    […]
</head>
<body>
    <?php require '/pathToIncludes/' . $pages['home']['content]; ?> 
</body>
<?php 
    include("start.php");
    $page->title = "My special title";
    include("header.php");
    // The rest of my content
?>
<?php

$page = array(
    'title' => 'Title',
    'js' => array(
        'jquery.min.js',
    ),
    'copyright' => 'Copyright 2012',
);

if ( $needed ) {
    $page[ 'js' ][] = 'some-other.js';
}

include( 'header.php' );
include( 'content.php' );
include( 'footer.php' );


// header.php
<html>
    <head>
        <title><?= $page[ 'title' ]; ?></title>

        <? foreach ( $page[ 'js' ] as $js ) { ?>
            <script src="<?= $js"></script>
        <? } ?>

    </head>


是的,我认为这是实现最佳SEO的方法。你想为不同的页面设置不同的标题。对这个问题,快速回答其他问题,如果我想在1 index.php文件中包含多个页面,该怎么办。那么有没有一个好的方法呢?看看我最新的答案。有了这一点,您可以包含不同的页面,这些页面与来自.htaccess.Long的请求路由有关。MVC方法绝对是最好的选择。“MVC方法完美地解决了这个问题。”但您建议在控制器中设置视图标题。。。我真的建议你再考虑一下;-)你会把它放在哪里?如果你在视图中设置它,它有点违背了这一点,并且模型对请求它的动作/模板是不可知的?我对服务器端的任何东西都是相当陌生的,但我想我会从某个地方开始。我已经做HTML/CSS三年多了。@David我在我的回答中又加了一点小结,希望能回答你的问题。哎哟,这似乎是非常糟糕的做法。好的。嗯,我喜欢你的想法胜过其他想法,但我知道任何一页都会有不同的标题。我还想让大多数页面运行1 index.php文件,方法是在.htaccess.You说的2件事中使用rewrites,在一个“简单的站点”上,但您说您只会为一堆页面推荐它。Argh。我不是以英语为母语的人。认为“一堆”也可能意味着“只有几个”。