Best practices for directory setup and Composer


I'm trying to find the "perfect" directory setup for my projects, but I have some difficulties in including Composer resources.

My current project setup:

/ -> base directory
/stuff -> project files that shouldn't be published
/src -> base directory for code
/src/private -> code outside of the public web directory
/src/private/config -> configuration files for web application
/src/private/lib -> PSR-4 root for PHP clases
/src/public -> so-called htdocs (public dir on web server)
/vendor -> managed by Composer

Deployment on web server:


When deploying my project to the webspace I move the project dirs around (with PhpStorm deployment mappings) to fit my needs.

Using the initial project setup, I pull some Composer dependencies which are kept in /vendor.
Then I create /src/public/index.php and just put this in it:

require_once 'vendor/autoload.php';

This already leads to a code inspect error: "Path 'vendor/autoload.php' not found."
I tried to play with PhpStorm's PHP include-path, marked directories as  resource root or source root, but none of these let me just use  "vendor/autoload.php".

I could just rewrite this to

require_once '../../vendor/autoload.php';

Unfortunately, PhpStorm works fine with this, but after deploying my project to the web server, the path will become invalid because the directory structure is different there.
On the web server I just add my base directory to the include path and 'vendor/autoload.php' works fine, but adding this include path in PhpStorm results in unresolvable classes of composer dependencies.
I also tried to change the location of the "vendor" directory within composer.json to be kept in "/src/private/vendor" but then PhpStorm seems to have a problem with it, so this doesn't seem to be a good idea.

1. I'd really like to know what your setups look like. Do you use a different approach?

2. Let's say we ignore the deployment stuff. How do you include the 'vendor/autoload.php' in your projects to that PhpStorm can resolve the classes?


1 comment
Comment actions Permalink

Hi there,

In my opinion -- just ignore that inspection and either disable it completely (which is what I do in most cases) .. or use inspection suppression (for that line only). Since you are using autoloading .. then you should not be having any more include/require statements (excluding possible config files or resources  of course (templates/translations etc)) and it will load all classes for you if configured appropriately.

IDE does not depend on that (include/require path resolution) to find your classes -- it searches for classes by names and not file paths. This means -- you may have your class anywhere in the project for IDE to recognize it and use in code completion.


Please sign in to leave a comment.