How to configure "Directories" when using a Symfony project in PhpStorm
PhpPhpstormSymfonyPhp Problem Overview
I use PhpStorm to work on a Symfony project.
In the File > Settings > Project … > Directories configuration, I defined the vendor/
directory as a Resource root in order to have auto-completion and as an Excluded folder because I want to ignore vendors when performing a search in my project's code.
But my problem is that vendors are still shown in search results.
Here is my current configuration:
Here is what I'm trying to avoid: results from vendor/
are shown:
Here is the PHP configuration:
I can restrict search by selecting Scope = Custom but sometimes I forget to change this. I'm looking for some settings that I can use in my different Symfony2/3 projects.
How should I mark the vendor/
directory in order to allow PhpStorm to use it as a resource root and ignore it when performing a search?
And what is the correct configuration for the default directories structure of a Symfony2 project? Here are the default directories after a Symfony 2.8 installation with composer create-project symfony/framework-standard-edition symfony-2.8 "~2.8"
:
app/
├ config
├ cache
├ logs
└ Resources
src/
└ AppBundle/
vendor/
web/
Here is how I marked the directories at this moment:
.idea [excluded]
app/
├ config
├ cache [excluded]
├ logs [excluded]
└ Resources
src/ [source]
└ AppBundle/
└ Tests/ [test source folders]
vendor/ [excluded]
web/
Note: I installed the Symfony plugin for PhpStorm, I don't know if this change the IDE behaviour.
Php Solutions
Solution 1 - Php
The vendor
folder is not a resource root. A resource root is a folder where resources such as images and scripts will be served from by the web server.
In your case the only folder that should be marked as a resource root is probably the web
folder, but ironically, is almost the only one you haven't selected as a resource root. Marking web
as the resource root means that the absolute URLs /css/foo.css
and /images/foo.jpg
could be valid resources served by the web server; you probably want to remove all other folders from resource roots.
It is correct to exclude the vendor
folder because it is not part of your first-party project code. In order for code completion to work for third-party code you must add the vendor folder as an external library. This can be done by navigating to Languages & Frameworks > PHP in the options and specifying the vendor folder as an include path.
Solution 2 - Php
After having used advices from Quolonel Questions's answer, here is a summary of my configuration for Symfony2 (see Symfony3 at the end of this answer):
For auto-completion, use the vendor/
directory in Include path:
In order to avoid irrelevant results when searching in the project, the following directories have to be ignored:
.idea [excluded]
app/
├ cache [excluded]
└ logs [excluded]
vendor/ [excluded]
Here is my full configuration:
.idea [excluded]
app/
├ cache [excluded]
└ logs [excluded]
src/ [source]
└ AppBundle/
└ */Tests/ [test source folders]
vendor/ [excluded]
web/ [resources root]
Test Source Folders are optional, if they are defined the will appear in the toolbar:
With the default configuration for Symfony3, the directories are slightly different:
.idea [excluded]
src/ [source]
tests/ [test source folders]
var/
├ cache [excluded]
└ logs [excluded]
vendor/ [excluded]
web/ [resources root]
Update: after updating my dependencies with composer update
, PhpStorm perform searches in the vendor/
directory, even if these directories are ignored. The solution is to remove all the vendor/*
directories from Include path and keep only vendor/
directory, as on the first screenshot.
I'll have to test if marking all the vendor/*
directories as ignored can work and avoid to repeat this after each time composer update
is used.
Solution 3 - Php
Another option which is easier than manually excluding vendor
and then including it again in php settings, is to tell PhpStorm about composer.json
and composer.phar
in the composer settings as showin in this question.
Solution 4 - Php
I use PhpStorm 10 as my primary IDE for Symfony2. You don't need to install any Symfony plugins, because PhpStorm support Symfony2 by default.
- Your should mark your
public_html
directory as a Resource Root, or whatever you have that is going to be public - Sources - your
app/
directory - If you don't want
vendors/
in search, that's what I exclude also, you press on vendor and "Excluded" button on the top. You also want to exclude,tmp/
andapp/cache/
directories - As you already know, you can define scope and search there.
When you exclude directory, it also helps performance since PhpStorm not indexing and watching files there, something you don't want anyway.
As for directory structure of Symfony2, it's pretty flexible, I use my own. Here is Symfony 2.8 directory structure from the docs.
Excluded folders for me are:
-
app/DoctrineMigrations
-
app/cache
-
app/logs/
-
tmp/