How to do relative pathing? how to use __dir__
here is my file structure before
public_html
├── config
| |__config.php
├── core
| |__init.php
├── helper
| |__helper.php
├── libraries
| |__page1.php
├── templates
│ ├── page1template.php
│
│__index.php
│__page1.php
After having too many pages. I want to tiddy them up into "rep" folder and "admin" folder. so my new file structure look like this
public_html
├── config
| |__config.php
├── core
| |__init.php
├── helper
| |__helper.php
├── libraries
| |__page1.php
├── templates
│ ├── page1template.php
│
│__index.php
|
├── rep
| |__page1.php
├── admin
│ ├── page1.php
The problem is, my requires and includes are still absolute pathing. see a sample below
my init file
//Start Session
session_start();
//Include Configuration
require_once('config/config.php');
//Helper Function Files
require_once('helpers/system_helper.php');
require_once('helpers/format_helper.php');
//Autoload Classes
function __autoload($class_name){
require_once('libraries/'.$class_name .'.php');
}
my page1.php
<?php require('core/init.php'); ?>
//Get Template & Assign Vars
$template = new Template('templates/rep/page1.php');
if I add an "../" in front of every path. then the page will loads correctly. see below
require_once('../config/config.php');
require_once('../helpers/system_helper.php');
require_once('../helpers/format_helper.php');
require_once('../libraries/'.$class_name .'.php');
require('../core/init.php');
$template = new Template('../templates/rep/page1.php');
but then my index.php will break because index is in the root folder, so the new path is wrong relative to it. I found two question with related issue. I suspect it is related to using __dir__
to replace ../ but I dont know where to put it.
Are PHP include paths relative to the file or the calling code?