自制php404_使用PHP的“自制”页面,第一部分

自制php404

One of the common overreaching uses of includes that I mentioned previously is to replace the entire leading section of HTML code, including the <head> section, in a single include file. This of course, causes every page to have the same title and meta tag values, which is unacceptable. However, with a few PHP functions and features we will introduce here, including PHP_SELF, constants, str_replace(), ucwords(), and filemtime(), we will show how it is possible to have a single include file containing the entire <head> section that still allows for a unique title and meta tag values on each page.

我之前提到的include常见超常用法之一是在单个include文件中替换HTML代码的整个前导部分,包括<head>部分。 当然,这会使每个页面具有相同的titlemeta标记值,这是不可接受的。 但是,我们将在此处介绍一些PHP函数和功能,包括PHP_SELF ,常量, str_replace()ucwords()filemtime() ,我们将展示如何有可能拥有一个包含整个<head>的单个include文件。 <head>部分,仍然在每个页面上提供唯一的标题和元标记值。

First, a few assumptions and rules:

首先,一些假设和规则:

  • Aside from index.php, the filename of the pages of your website will always reflect their intended purpose, and when multiple words are used in the file name they are separated by hyphens. For example, popular-routes-to-india.php. (Hyphens are preferred by Google and other search engines over underscores in filenames).

    除了index.php之外,网站页面的文件名将始终反映其预期用途,并且在文件名中使用多个单词时,它们之间用连字符分隔。 例如, popular-routes-to-india.php 。 ( 连字符在Google和其他搜索引擎中比文件名下划线更受欢迎)。

  • The majority of the microdata / metadata will be the same from page to page: for example, you will always be the creator of the page.

    每个页面的大多数微数据/元数据都是相同的:例如,您将始终是页面的创建者。
  • You will never require an embedded stylesheet: all style declarations will either be in a linked stylesheet or written inline.

    您将永远不需要嵌入式样式表:所有样式声明都可以在链接的样式表中或以内联方式编写。

With those assumptions in place, let’s take a look at the HTML code for the popular-routes-to-india.php page of the fictional website for Lao Che Air Freight:

有了这些假设,让我们看一下虚构的老挝航空货运网站的Popular-routes-to-india.php页面HTML代码:

<!doctype html>
<head>
	<meta charset=UTF-8>
	<title>Lao Che Air Freight - Popular Routes To India</title>
	<link rel="shortcut icon" href="lao-che.ico">
	<link rel="stylesheet" href="styles.css">
</head>
<body>
	<div id="wrapper">
		<h1>
			<a href="index.php">
				<img src="assets/images/lao-che-logo.jpg" alt="Lao Che Air Freight">
			</a>
		</h1>
	<nav>
		<a href="index.php">Home</a>
		<a href="about.php">About Us</a>
		<a href="popular-routes-to-india.php">Popular Routes To India</a>
		<a href="prices.php">Prices</a>
	</nav>
	<main>
	</main>
</html>

This page has nearly everything: a link to a stylesheet and favicon, a logo inside a <h1> element, a navigation bar, etc. As you can guess, the majority of the code, from the very first line to the opening <div> tag for the content section, will be exactly the same from one page to the next: the only details that would change would be the title content. Let’s do the previously unthinkable: take that entire section of code and make it a single include called pagestart.php.

此页面几乎包含所有内容:指向样式表和favicon的链接, <h1>元素内的徽标, 导航栏等。您可以猜到,从第一行到开头<div>的大部分代码。 <div>内容部分的标记,从一页到下一页将完全相同:唯一更改的细节将是标题内容。 让我们做一下以前无法想象的事情:将整个代码部分都放在一个名为pagestart.php的 include 文件中

注意: (Note:)

Realistically, we could give the include file any extension we wished - .inc is popular, as in pagestart.inc – as any code on the page will not be run until it is actually rolled into the page that calls it. Therefore, the extension that is provided to the include file is immaterial. Giving the file a PHP extension does have the singular advantage that and other programs will color the PHP code on the include file correctly by default, but that’s really the only reason.

实际上,我们可以给include文件,我们希望任何分机- 。公司很受欢迎,在pagestart.inc -为页面上的任何代码将不会运行,直到它被调用它实际上滚入页面。 因此,提供给include文件的扩展名无关紧要。 为文件提供PHP扩展确实具有独特的优势, 和其他程序默认情况下将正确地为include文件中PHP代码着色,但这确实是唯一的原因。

Let’s say we placed the pagestart.php file in an includes folder inside the assets folder. So our popular-routes-to-india.php page becomes:

比方说,我们放在pagestart.php文件中的一个包含文件夹内的文件夹中的资产 。 因此,我们的Popular-routes-to-india.php页面变为:

<?php include('assets/includes/pagestart.php'); ?>
		</div>
	</body>
</html>

The unique content for the page will be placed between the include and the closing </div>.

页面的唯一内容将放置在include和结束</div>

Turning back to the include itself, the most glaring issue is the fact that each page on which it is used will have the title Lao Che Air Freight – Popular Routes To India. Let’s improve on that. Change the <title> section of the include to:

回到include本身,最明显的问题是使用该页面的每个页面的标题都是“ 老挝航空货运-印度通行路线” 。 让我们对此进行改进。 将include的<title>部分更改为:

<title>Lao Che Air Freight - <?php echo $_SERVER['PHP_SELF']; ?></title>

… and upload your files to the server, viewing them in the browser. Depending on your server setup and where you are saving your files, you will see something like this as the title:

…并将您的文件上传到服务器,在浏览器中查看它们。 根据您的服务器设置和文件保存位置,您将看到类似以下内容的标题:

Lao Che Air Freight - /site/popular-routes-to-india.php

老挝航空货运-/site/popular-routes-to-india.php

(Note that $_SERVER['PHP_SELF']reflects the filename of the page, not the include).

(请注意, $_SERVER['PHP_SELF']反映页面的文件名,而不是include )。

There are a few things we want to do to improve the PHP statement within our <title>: obviously, we need just the filename of the page, without the directory structure of which it is a part:

我们需要做一些事情来改进<title>PHP语句:显然,我们只需要页面的文件名,而无需包含它的目录结构:

<?php echo basename($_SERVER['PHP_SELF']); ?>

We also want to eliminate the .php extension on the end:

我们还希望最后消除.php扩展名:

<?php echo basename($_SERVER['PHP_SELF'],".php"); ?>

…and convert the hyphens used in the filename to spaces:

…并将文件名中使用的连字符转换为空格:

<?php echo str_replace("-"," ",(basename($_SERVER['PHP_SELF'],".php"))); ?>

Finally, we want to make the words used in the title capitalized. Ironically, we can’t use CSS to do so, as text-transform: capitalize only affects content within the <body>, not the <head>. So instead we must use the ucwords() PHP function:

最后,我们要使标题中使用的单词大写。 具有讽刺意味的是,我们不能使用CSS进行text-transform: capitalize ,因为text-transform: capitalize仅影响<body> ,而不会影响<head> 。 因此,我们必须使用ucwords() PHP函数:

<?php echo ucwords(str_replace("-"," ",(basename($_SERVER['PHP_SELF'],".php")))); ?>

(Note the parentheses growing as we use each function).

(请注意,括号随着我们使用每个函数而增加)。

We want to use this generated title at least once in the <title> itself. Any time we expect to use something more than once, we must consider how to do so with greater efficiency. In this case, we don’t need a variable, as the filename of the page is not going to change during the execution of our script; instead we’ll take the function that we developed and use it to define the value of a constant, at the very start of our page:

我们希望至少在<title>本身中使用一次生成的标题。 每当我们希望使用不止一次时,我们必须考虑如何提高效率。 在这种情况下,我们不需要变量,因为在执行脚本的过程中页面的文件名不会更改。 取而代之的是,在页面的开头,我们将采用我们开发的函数并将其用于定义常量的值:

<?php define ("title", ucwords(str_replace ("-"," ",(basename($_SERVER['PHP_SELF'],".php"))))); ?>

Constants are defined only once; the name of a constant may be any single word that does not begin with a numeral or $. (It’s a common mistake to forget to start a variable name with the required $ sign; which often results in the server saying that it is an undefined constant).

常量仅定义一次; 常量的名称可以是任何不以数字或$开头的单词。 (忘记使用所需的$符号开头变量名是一个常见的错误;通常会导致服务器说这是一个未定义的常量)。

Now we can use the title constant in both places in our code:

现在,我们可以在代码的两个地方使用title常量:

<title>Lao Che Air Freight - <?php echo title; ?></title>

The other mic tag we should change is date. This refers to the last date of creation for the document. (It is also possible to use DC.date and DC.revised as separate tags, recording when the document was originally created and when it was last revised). The date used must be in the format yyyy-mm-dd.

我们应该更改的另一个mic标签是date 。 这是指文档的最后创建日期。 (也可以将DC.dateDC.revised用作单独的标签,记录最初创建文档的时间和上次修订的时间)。 使用的日期必须采用yyyy-mm-dd格式。

Rather than doing this by hand, let’s use PHP to inspect when the file was last modified. As a test, somewhere below the opening content <div> in our include file:

与其手动进行操作,不如让我们使用PHP来检查文件的最后修改时间。 作为测试,在include文件中开头内容<div>下方的某处:

<p>The file was last modified on:
<?php echo (filemtime(basename($_SERVER['PHP_SELF']))); ?></p>

This gives us a result that is in Unix time format. For the published date, we want to convert that to the correct format, using the date() function:

这给我们的结果是Unix时间格式。 对于发布的日期,我们想使用date()函数将其转换为正确的格式:

<body itemscope itemtype="http://schema.org/WebPage">
	<header>
		<h1 itemprop="headline">The Very First Rule of Life</h1>
		<p>
			<time itemprop="datePublished" datetime="
			<?php echo date ("Y-m-d", 			filemtime(basename($_SERVER['PHP_SELF']))); ?>
			3 days ago
			</time>
		</p>

摘要 (Summary)

While the effort to complete the include file was considerable, the benefits are significant: to start a new page on the Lao Che website, all we need to write is:

尽管完成include文件的工作相当可观,但好处却是巨大的:要在Lao Che网站上启动新页面,我们所需要做的就是:

<?php include('assets/includes/pagestart.php'); ?>

Everything – the page title, the link to the stylesheet, meta tags – is taken care of. All we need to remember is to provide a good filename for our page, with words separated by hyphens, and everything else is generated for us. This significantly speeds up the creation of new pages, allowing us to concentrate on content rather than code.

所有内容(页面标题,样式表的链接,元标记)都得到了处理。 我们需要记住的是为页面提供一个良好的文件名,单词之间用连字符分隔,其他所有内容都为我们生成。 这大大加快了新页面的创建速度,使我们能够专注于内容而不是代码。

翻译自: https://thenewcode.com/211/Self-Made-Pages-With-PHP-Part-I

自制php404

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值