PHP front-controller to include file from other folder if not exist

浪尽此生 提交于 2019-12-12 04:34:44

问题


I'm creating a php front-controller because I need to include file from other folders instead of creating the same file for those that have the same content

Example of a directory structure

[root]
  - start.php
  [rome]
     - index.php
     [subfolder]
         - page.php
  [london]
     (no file)

Here contents of the file

index.php

require '../start.php';

start.php

require 'subfolder/page.php';

And rome/index.php show page.php correctly...

Now in london folder, these are htaccess / front-controller

.htaccess

RewriteEngine On
RewriteRule . front-controller.php [L]

front-controller.php

$path = $_SERVER['DOCUMENT_ROOT'].$_SERVER['REQUEST_URI']; 
if (! file_exists ($path) ) include str_replace("london" , "rome" , $path ); 

When I call london/index.php, response is

Warning: require(subfolder/page.php): failed to open stream: No such file or directory in ...

Fatal error: require(): Failed opening required 'subfolder/page.php' 

来源:https://stackoverflow.com/questions/41970755/php-front-controller-to-include-file-from-other-folder-if-not-exist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!