My project has two entry point
project (root-folder)
/config(folder)
config.php
/service(folder)
service.php
index.php
for example
File 1:/index.php ( first entry point) - here we include the config form the config folder
<?php
require_once('config/config.php');
require_once('service/service.php');
?>
File 2:service/service.php - here we include the config form the config folde
<?php
require_once('../config/config.php');
?>
if i call the File 2:service/service.php has no fatal error
but when i call the File 1:/index.php it became the fatal error as failed to require 'service/service.php' because it require again and config path is invalid
How to solve this issue.
Reason:
This issue arises because your execution starts from
index.phpand then you requireservice/service.php. Now inservice.phpwhen you do../config/config.php, PHP tries to resolve the directory path fromindex.phppoint of view and it doesn't find any such file or directory. Hence, the error.Solution:
Declare a constant in
index.php. Check if this constant exists inservice/service.php. If not, then require it, else skip it like below:index.php:
service.php:
Note: It is always better to use
__DIR__giving absolute paths than relative paths to avoid such issues.