Import PHP files via URL instead of disk path

1

When I use an architecture like MVC in a PHP project separating the models , controllers , services , views many times using the include or require functions the application loses itself when importing the PHP files located in other directories, and for this you need to pass the full disk path of the file location in order to prevent a PHP file inside the model directory being imported from being searched in /controllers/model where this problem often occurs when returning directories ( ../ ) for example.

I would like to know if you can import these files using the URL instead of the disk path because you do not have to keep going back and forth in each directory.

So you could import simply by passing: /controllers/TerceiroController.class , for example, or localhost/projeto/controllers/TerceiroController.class instead of /var/www/html/...

    
asked by anonymous 03.12.2015 / 13:15

2 answers

1

Standardize with the use of the autoloader in the PSR-4 standard: link

For example, the concept idea is to use namespace to make it easier to set up a route.

The namespace feature is available from PHP 5.3.0.

Examples: link

    
03.12.2015 / 13:39
0

You can not do this simply because PHP is a server-side scripting language.

You can use resources from a file in another URL, usually when it is passed through POST, this would be an API of some service, or even get the output from some page, for example.

But it is not possible, do what you want, from the moment you use http: // (even if you omit and only put the external URL), you would no longer be on the "side" of the server .

To solve your problem, declare a constant using define('RAIZ_PROJETO', dirname(__FILE__)) in the index, how it will be global you can use to get the path, such as require_once(RAIZ_PROJETO.'/models/seuModel.php')

Alert : Creating global constants and using throughout your project can create a high coupling, that is, make your classes very dependent.

You can create a class to encapsulate this information and use dependency injection, as this is an MVC, you're likely to have to do this at some point in your project, be it with configuration files or something you need access at several points but do not want to create a direct dependency.

And it also does not hurt to use a constant, you can, but it has this negative side.

The negative side of creating a class and using DI would be the work of programming everything.

    
03.12.2015 / 13:30