0

I have different folder that have same file name and often same content

Is there a way to include file from other folders instead of creating the same file for those that have the same content?

Example of a directory structure

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

another folder

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)

Then london folder must include only detail.php that have different content from rome/detail.php and "index" and "show" must be loaded from rome folder

Now I create the file that have same content with this content

require ../rome/index.php

and work, but I have to create file for every folder and when I move or rename file in "rome" folder, I must do the same for each folder...

Server is linux, then I can use also a string in htaccess if possible....

FireFoxII
  • 828
  • 4
  • 18
  • 31

2 Answers2

1

The easiest way is to control all with FrontController - you can organise common responses for as so many actions as you want with exceptions you need. FrontController should be the central part of your application. It's implemented by many frameworks and it's the way things like this should be done to not create and maintain many files.

Community
  • 1
  • 1
Piotr Dawidiuk
  • 2,961
  • 1
  • 24
  • 33
  • FrontController seems to be a good solution, thanks... But now I don't know if I have to rewrite my original code (rome folder in this case)... For example, I'm writing a simple frontcontroller in this way $path = "{$_SERVER['DOCUMENT_ROOT']}$_SERVER['REQUEST_URI']}"; if (! file_exists ($path) ) include str_replace("london" , "rome" , $path ); and I have error for the file that require a subcontent.... – FireFoxII Jan 24 '17 at 11:45
  • @FireFoxII Hello buddy. Can you write the whole code in new question or edit this one? Also, please add more detail about this error (traceback). – Piotr Dawidiuk Jan 24 '17 at 16:35
  • Thanks, I open here http://stackoverflow.com/questions/41848860/php-create-front-controller-to-include-file-from-other-folder-if-not-exist – FireFoxII Jan 25 '17 at 10:15
0

Try with this root .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]
Croises
  • 18,570
  • 4
  • 30
  • 47