Configure PHPStorm to account for relative paths for included files?

I just started with PHPStorm and I ran into a problem.

Therefore, when you include a file, if you then include another file in it, the path is not understood correctly.

eg. Folder structure:

root/ Contains index.php view/ Contains view.php lib/ Contains functions.php 

So, if I include view.php in index.php:

 index.php: include('view/view.php'); or require('view/view.php'); 

When this code is run, anything inside view.php is included in index.php, so any files included in view.php are in the root directory.

 view.php: include('lib/functions.php'); 

But PHPStorm believes that they belong to the view directory.

 view.php: include('../lib/functions.php'); 

In this situation, this is not so.

This issue also occurs for things like: <a href="index.php">Link</a>

How to configure PHPStorm to get this situation? I suggested that this will work automatically, but currently it is not.

Specific example:

 private/ privatefile.php public/ index.php views/view1.php //Included in index.php views/view2.php //Included in index.php 

Will "public" be the root of the resource in this situation if I want to include privatefile.php in view1.php?

Thus, the include in view1.php will look like this: include ( '../private/privatefile.php' );

+6
source share
2 answers

A tagging folder as "Resource root" will only help for HTML / CSS (for example, links to images / css / js files, etc.). this will NOT affect the actual php. >

In your public/index.php (which is your script entry point - all requests will go through it), define some constants, for example.

 define('DIR_WEB', __DIR__); // points to your public define('DIR_APP', dirname(__DIR__)); // points to your project root 

and use them in your include / require statements, for example.

 include (DIR_WEB . '/views/view1.php'); include (DIR_APP . '/private/privatefile.php'); 

Thus, you ALWAYS refer to the same file (using the absolute path ), regardless of where the file is located.


You have to remember one thing: PhpStorm only performs static analysis - it cannot do the same thing as the PHP interpreter (which runs at runtime). Therefore, the IDE checks your included statements regarding the root of the project / module or the actual script where it is used.

Using the above, they will satisfy both PhpStorm and PHP itself (you will use the full path, so there is no need to look for include files - a little faster - there may be a difference on very busy sites with a lot of inclusions). Plus is safer - because you always refer to a specific file, so there is no chance that PHP will load a similarly named file from another location by mistake.

+9
source

Go to Settings-> Languages ​​and Framework-> PHP and add the appropriate directory as the "include path". Checks will show OK. The only drawback is that these global settings are not for one project.

0
source

Source: https://habr.com/ru/post/974184/


All Articles