One of the great things that happened in PHP world was the introduction of Composer, a dependency manager for PHP which allows us to specify dependencies for our project and have Composer install and update them for us. PhpStorm has full support for this great tool in the UI and in PhpStorm's command line tool.
In this tutorial, we'll cover the following tooling support:
Let's create a project using the Symfony framework and install dependencies using Composer support in PhpStorm.
Creating a new project with Composer
When creating a new project in PhpStorm using the File | New project... menu, we can choose from a list of project types. PhpStorm 6 introduced a new project type: Composer project.
When using this project type, PhpStorm will present us with a dialog which allows us to do two things. We can specify the path to composer.phar (or have PhpStorm download it from getcomposer.org) and we can start adding dependencies through Composer. Using the package filter we can search through packages listed at the Packagist.
Let's install the symfony/symfony package, the official package for the Symfony framework. By default the latest version will be installed, however we can select the version to install from this dialog as well. After clicking OK, PhpStorm will install the selected package into a new project.
Once finished, we have a blank Symfony project which we can start developing in.
In an empty project (or an existing project in which Composer was not initialized), we can right-click the project and use the Composer | Init Composer... context menu to enable Composer support for our project. If you don't have composer.phar anywhere on your system, the Click here to download from getcomposer.org helper will download the latest composer.phar from the official website.
This will generate a default composer.json file in our project in which we can describe our project and its dependencies.
Why type raw JSON in the composer.json file to add dependencies? Not to mention that Composer should be invoked to install dependencies described. PhpStorm comes with a new context menu which is displayed when right-clicking our project: Composer | Add dependency...
Imagine we would like to create spreadsheet documents in our application. We can add a dependency to the phpexcel/phpexcel package with the click of the Install button.
The IDE will add the dependency to composer.json and satisfy any other required dependency automatically for us.
Command line tool support
When full control is needed, we can always use the command line tool in PhpStorm to invoke Composer.
Enabling the Composer command line tool
Before we can use the Composer command line tool, we have to enable it. We can do this globally (for all projects) or on a per-project basis.
- From the Project Settings | Command Line Tool Support, add a new tool using the + icon on the toolbar
- Select Composer as the tool and specify project as the visibility. Note that other command line tools are supported too.
- Specify the path to composer.phar. If Composer is already being used in the project, PhpStorm should automatically fill out this path.
- Click OK and note that Composer has been added with an alias "c".
- Close the settings.
Using the Composer command line tool
We can bring up the command line tool using the Tools | Run Command... menu or with Ctrl+Shift+X (Cmd+Shift+X on Mac).
PhpStorm comes with full Composer framework support in the command line tool. When we start typing "c" (the alias created in the settings), PhpStorm will come with a list of suggestions and autocompletion support.