如果不存在,php包含来自其他文件夹的文件

时间:2017-01-21 16:02:15

标签: php .htaccess

我有不同的文件夹,文件名相同,内容通常相同

有没有办法从其他文件夹中包含文件,而不是为那些具有相同内容的文件创建相同的文件?

目录结构示例

rome
  - index.php
  - detail.php
  - show.php

另一个文件夹

london
  - index.php (same content as rome/index.php folder)
  - detail.php (different content as rome/detail.php folder)
  - show.php (same content as rome/show.php folder)

然后,london文件夹必须只包含与rome / detail.php具有不同内容的detail.php,并且必须从rome文件夹中加载“index”和“show”

现在我创建了与此内容具有相同内容的文件

require ../rome/index.php

并且工作,但我必须为每个文件夹创建文件,当我在“rome”文件夹中移动或重命名文件时,我必须对每个文件夹执行相同操作...

服务器是linux,如果可能的话我也可以在htaccess中使用一个字符串....

2 个答案:

答案 0 :(得分:1)

最简单的方法是使用FrontController控制所有人 - 您可以根据需要为所需的例外操作组织常见的响应。 FrontController应该是应用程序的核心部分。它是由many frameworks实现的,并且应该采用这种方式来创建和维护许多文件。

答案 1 :(得分:0)

尝试使用此根.htaccess:

RewriteEngine on

# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
# But not '/rome/'
RewriteCond %{REQUEST_URI} !^/rome/
# Rewrite to '/rome/...' if exist
RewriteCond %{DOCUMENT_ROOT}/rome/$1 -f
RewriteRule ^[^/]+/(.+)$ /rome/$1 [L]
相关问题