How to handle PHP file paths

4

Hello guys for a long time I wanted to introduce myself in the PHP world but I had never finished giving the pleasure, one of the issues I have is how to manage the routes of the include and require already if you have several folders to include the files php of other folders is complex.

Example if I have a configuracon of my system similar to this.

The problem is to move between the routes because if I want to include the file config.php in the WorkerImp.php class I do not know how to do it because of the routes, PHP tells me that the file can not be included.

I have read about creating a configuration file with the base routes but I come to the same conclusion to be able to access those constant routes I need to add config.php

    
asked by crack81 29.07.2017 в 21:55
source

2 answers

4

In general the include , like the require work like this:

If the files are in the same directory as the script

include ("archivo.php");

If the files are in another directory

If the file is in the parent directory of the script, ../ is set in front of the file name:

include ("../archivo.php");

../ is used to backtrack between directories.

You can also use $_SERVER['DOCUMENT_ROOT'] as a reference point.

If config.php is in the root directory. You could make a include of this file from WorkerImp.php by:

include($_SERVER['DOCUMENT_ROOT']."/config.php");

Or by:

include("../../config.php");

here ../../ goes back two directories from where the script is located. That is, go back imp>dao> searching config.php in the directory found in the root of the App.

Another possibility

I have implemented the following, to stay independent of any possible change of route.

I have created a file called dirs.php in which I have defined all the routes of my project. I put that file in the root (root directory).

<?php 
define('ROOT_PATH', $_SERVER['DOCUMENT_ROOT'].'/');
define('CONTROLLER_PATH', ROOT_PATH.'controller/');
define('MODEL_PATH', ROOT_PATH.'model/');
define('DAO_PATH', ROOT_PATH.'dao/');
define('IMP_PATH', DAO_PATH.'imp/');
...
etc

?>

Then, when I need to include some project file:

include_once ($_SERVER['DOCUMENT_ROOT'].'/dirs.php');
include (DAO_PATH."PersonaDao.php");

Note that I use include_once , so that you do not include it again if another file of the project has done it (to understand the difference between include e include_once you can consult: What is the difference between require, require_once, include, include_once in PHP?

What is the advantage of using this method?

If for some reason I have to change a path in the directory Dao , I do not have to review all the parts where the include includes that directory, just change the value of DAO_PATH and you're done.

What is the disadvantage?

By including the file dirs.php you will be including some constants that you may not be using. Nor are hundreds or thousands of constants, so it will not affect the performance of your program.

    
answered by 29.07.2017 / 23:06
source
0

The routes in php in the beginning are somewhat complicated to manage, and even more if you are not using any framework (like laravel for example) then it will be a little more annoying.

the two options that you said in your question include and require except for an error, in this link you can know more about what I mean, with these you can take what is there in a specified file and "paste" it in yours (to put it briefly)

While if you want to use the functions and others of a created class you should use the reserved word use , in this link you can find out more.

use ruta;

What use is to search the route and name of the class from the root of your project

use carpeta\clase;

IMPORTANT NOTE

It is important that you remember that the php routes whenever you enter a container folder you should put anti-slash (\) until you find your files

Here you can see more about routes and files

I hope I could have helped you ... and luck with PHP

Greetings !!!

    
answered by 29.07.2017 в 23:00