简体   繁体   中英

Is there a shorthand for the Project Root path in PHPStorm?

I'm new to working with PHPStorm and am looking for a helper to make requireing files a bit easier. I'm in a file that's nested fairly deep in folders, and I need to require() a file from just under the Project Root in PHPStorm.

Is there a shorthand in PHPStorm (or plain old PHP) which let's me do something like

require('{ROOT}/lib/somefile.php');

Instead of having to use relative paths?

require('../../../../../../lib/somefile.php');

Apache stores the path to the document root folder in an environmental variable which you can access from PHP like this:

$ROOT = getenv("DOCUMENT_ROOT");

Then you can say:

require("$ROOT/lib/somefile.php");

If you are usign IIS see this article in helicron.net .

If it is not an overkill, I would suggest to use an auto loader. Here is a standalone one from symfony components: http://symfony.com/doc/current/components/class_loader/introduction.html I am sure that Zend or other frameworks have the same ability.

It handles the location of files and may even remove your need to require files manually.

Reinventing the wheel is cumbersome for production, IMHO.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM