PHP 文件包括 app.php > 应用程序包括 template.php > 模板包括 header.php & footer.php
PHP File includes app.php > App includes template.php > Template includes header.php & footer.php
这是我们的文件夹结构:
index.php
-app/
--app.php
---template/
----template.php
----parts/
-----head.php
-----header.php
-----body.php
-----(etc)
我基本上只是在每个文件中使用 include 来导入我的 html 模板。
index.php :
<?php
include ('app/app.php');
?>
app.php :
<?php
include ('template/template.php');
?>
template/template.php :
<?php
// Load our Head
include ('parts/head.php');
// Load our Header
include ('parts/header.php');
// Load our Body
include ('parts/body.php');
// Load our Footer
include ('parts/footer.php');
?>
每个 head.php、body.php 等...都有一个 <?php echo "test head" ?>
但最后我的结果是一个空白的白页。
这不起作用有两个原因,因为当您使用“/”包含其他 PHP 文件时,将引用 index.php 的“/”,因此在所有包括您必须将“/”更改为 __DIR__
以及 index.php 中的这个使用 include ('/app/app.php');
将无法找到正确的当前目录,因此针对您的具体情况将代码更改为:
index.php :
<?php
include ('.\app\app.php');
?>
app.php :
<?php
include(__DIR__.'\template\template.php');
?>
template\template.php :
<?php
// Load our Head
include (__DIR__.'\parts\head.php');
// Load our Header
include (__DIR__.'\parts\header.php');
// Load our Body
include (__DIR__.'\parts\body.php');
// Load our Footer
include (__DIR__.'\parts\footer.php');
?>
这是我们的文件夹结构:
index.php
-app/
--app.php
---template/
----template.php
----parts/
-----head.php
-----header.php
-----body.php
-----(etc)
我基本上只是在每个文件中使用 include 来导入我的 html 模板。
index.php :
<?php
include ('app/app.php');
?>
app.php :
<?php
include ('template/template.php');
?>
template/template.php :
<?php
// Load our Head
include ('parts/head.php');
// Load our Header
include ('parts/header.php');
// Load our Body
include ('parts/body.php');
// Load our Footer
include ('parts/footer.php');
?>
每个 head.php、body.php 等...都有一个 <?php echo "test head" ?>
但最后我的结果是一个空白的白页。
这不起作用有两个原因,因为当您使用“/”包含其他 PHP 文件时,将引用 index.php 的“/”,因此在所有包括您必须将“/”更改为 __DIR__
以及 index.php 中的这个使用 include ('/app/app.php');
将无法找到正确的当前目录,因此针对您的具体情况将代码更改为:
index.php :
<?php
include ('.\app\app.php');
?>
app.php :
<?php
include(__DIR__.'\template\template.php');
?>
template\template.php :
<?php
// Load our Head
include (__DIR__.'\parts\head.php');
// Load our Header
include (__DIR__.'\parts\header.php');
// Load our Body
include (__DIR__.'\parts\body.php');
// Load our Footer
include (__DIR__.'\parts\footer.php');
?>