Import PHP files through the URL instead of the disk path

Asked

Viewed 275 times

1

When I use an architecture like MVC in a PHP project separating the models, controllers, services, views often using the functions include or require the application gets lost importing the PHP files located in other directories, and for that you need to pass the path of the full disk of the file location in order to prevent a PHP file inside the directory model to be imported is sought in /controllers/model where this problem often occurs when going back directories (../) for example.

I wonder if it is possible to import these files using the URL instead of the disk path because you do not need to keep entering or going back to each directory.

So it could matter simply by passing: /controllers/TerceiroController.class, for example, or localhost/projeto/controllers/TerceiroController.class rather than /var/www/html/...

2 answers

1

  • What is happening that are negativizing our question and answers? I didn’t know about this +1

  • 1

    It is the headquarters of ranking that is inhabiting some users here.

  • Have a repository on Github to indicate that you use the namespace feature including?

0

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

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

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

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

Alert: Create global constants and use throughout your project can create a high coupling, ie, make your classes very dependent.

You can create a class to encapsulate this information and use dependency injection, as it is a MVC, it is likely that you will have to do this at some point in your project, whether with configuration files or something you need to have access to at various points but don’t want to create a direct dependency.

And it also doesn’t hurt to use a constant, you can, but there’s this downside.

The downside of creating a class and using DI would be the job of programming everything.

  • 1

    Thanks for your contribution, I gave +1 to take this negative. I liked the change you made mentioning a solution to the problem.

  • No problem @Giancarloabelgiulian, an extra advice I can give you is to avoid using only "name.class" for your models and controllers, put a PHP extension at the end, there are servers that are bad configured that the user could try to access this file and it would be downloaded. The recommended is to protect from direct access to this file, but it would be helpful to put a . php at the end. Of course, it is also not an obligation, this is my suggestion for your project. I am also working on an MVC project, I am grateful to be able to help.

  • Actually I forgot to put the .php in the end hehe valeu

Browser other questions tagged

You are not signed in. Login or sign up in order to post.