【问题标题】:Using PHP requires with varying directory levels使用 PHP 需要不同的目录级别
【发布时间】:2015-01-19 01:40:22
【问题描述】:

我有一个项目,目录结构如下(简化):

+css
+js
+partials
  -header.php
  -footer.php
+blog
  -index.php
-index.php

我想在 blog index.phproot index.php 中使用以下代码 require_once header.phpfooter.php

<?php require_once("partials/header.php"); ?>

当然,在 博客 中它不起作用,因为 header.php 指向与 root @987654331 相关的 cssjs 文件@不是博客index.php

是否可以否定这一点,或者我需要 两个 partials 文件夹,一个在 root 级别,一个在 blog 级别.

【问题讨论】:

  • 我建议css和js使用绝对url,而不是相对url
  • 我同意,但如果您打算重用脚本,常量确实让生活更轻松。

标签: php reference directory structure require-once


【解决方案1】:

创建一个名为 constants.php 的文件并将其包含在脚本的顶部。将此常量定义为文件根目录的基本路径:

define("BASE_PATH", "http://www.yourURL.com/");

然后为了确保使用正确的路径,只需将此常量添加到您的网址:

require_once(BASE_PATH."/any_subdirectory/anyfilename.php");

【讨论】:

    猜你喜欢
    • 2014-11-13
    • 2012-10-08
    • 1970-01-01
    • 2011-11-29
    • 2018-02-05
    • 2011-07-11
    • 1970-01-01
    相关资源
    最近更新 更多