我希望使用位于/ template文件夹中的页眉文件(包括CSS,JS和图像)和页脚文件。然后,这些文件将包含在目录下的每个文件中,例如:
/template/header.php + footer.php
/home/index.php
/products/products.php
/products/hardware/types/hardware.php
/css
/js
/images
当我使用时:
include (__DIR__ . "/../template/header.php");
该文件是包含在内的(尽管我需要更改每个文件,并且在目录的更下方添加更多“ /../”),但所有的CSS,JS和图像都已损坏。
这是我的标头(位于header.php中,其中包含的文件之一):
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="shortcut icon" href="images/favicon.ico">
<link rel="icon" type="image/gif" href="images/favicon.ico">
<link rel="stylesheet" type="text/css" href="../css/layout.css"/>
<link rel="stylesheet" type="text/css" href="../css/slider.css"/>
<link rel='stylesheet' type='text/css' href='../css/menus.css'/>
<link rel='stylesheet' type='text/css' href='../css/forms.css'/>
<link rel="stylesheet" href="css/jquery-ui.css" type="text/css" media="all"/>
<script type="text/javascript" src="../js/jquery.js"></script>
<script type="text/javascript" src="../js/jquery.elastic.source.js"></script>
<script type="text/javascript" src="../js/control.js"></script>
<title>APEC Energy - <?php echo($PageTitle);?> </title>
</head>
我现在正在尝试使用配置文件(很难使用,但对我而言仍然不可用)。
config.php(放在根文件夹中)(是否需要在数组,文件路径或目录路径中放置更多信息?
<?php
$paths = array(
"root" => "./",
"controller" => "controller",
"stylesheets" => "css",
"images" => array(...),
"javascript" => array(...),
);
$projectName = "/";
function fInc($filePath){
global $projectName;
return '//'.$_SERVER['HTTP_HOST'].'/'.$projectName.'/'.$filePath;
}
?>
并在每个.php文件中使用以下命令调用包含文件的im:
<?php
$rootDir = $_SERVER['DOCUMENT_ROOT']."/".(explode ('/', $_SERVER['PHP_SELF'])[1]);
require_once($paths['helper']['html']."template/header.php");
?>
当我使用var_dump($ paths)时,它打印出“ NULL”
但是现在就变成黑屏了吗?
谢谢你
我总是config.php
在每个php的第一行中都包含一个:
require_once($_SERVER['DOCUMENT_ROOT']."/".(explode ('/', $_SERVER['PHP_SELF'])[1])."/config.php");
此代码应始终找到根目录,然后包含该config.php
文件。请注意,您可以将此文件放置在所需的任何位置-它总是可以自行找到其位置。如果随后将文件移动到另一个目录,则无需更改任何内容。
然后,此配置文件定义相对于根目录的所有子目录和其他重要文件的路径:
$paths = array(
"root" => "",
"controller" => "controller",
"stylesheets" => "css",
"images" => array(
"icons" => "img/icons",
"controls" => "img/controls"
),
"javascript" => array(...),
...
);
该数组意味着根文件夹(您的public_html
)包含目录“ controller”,“ stylesheets”,“ images”等,图像放置在“ img / icons”或“ img / controls”中。
包括所有其他文件,因此:
require_once($paths['helper']['html']."/form.php");
这可能非常有用,因为它允许您重组完整的目录布局,并且只需要更新config.php。
最后但并非最不重要的一点是,该配置还包含如下功能:
$projectName = "YourProjectFolderName";
function fInc($filePath){
global $projectName;
return '//'.$_SERVER['HTTP_HOST'].'/'.$projectName.'/'.$filePath;
}
它可用于转换将插入HTML文档中的路径。
编辑:请注意,定义一个包含所有路径的数组只是一个建议。这对我的项目很有帮助,因为我经常调整项目布局。如果您在项目中不这样做,则可以定义一个
$rootDir = $_SERVER['DOCUMENT_ROOT']."/".(explode ('/', $_SERVER['PHP_SELF'])[1]);
然后包含文件
require_once($rootDir . "/home/index.php");
这样,您将不再需要config.php
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句