Create php project with composer

Project template for creating and initializing a new PHP project from with a composer project or a composer package is named PHP Project from Composer.

Open the New Project window in File / New / Project menu, choose PHP Project from Composer, and specify the new project location.

Choose the composer project

The Wizard lets user to choose the composer package, shows the packages inforation, and subsequently searches for other available packages.

Creating the project files

The next step allows to specify PHP version and project format. See the New Project for details.

Project is initialized from the specified composer package using the composer create-project command.

Note, that specified PHP and composer.phar are both installed automatically if they are not found on the local computer.

Related links

  • New Project - creating a new PHP project, either empty or from a template.
  • New Project from Existing Code - creating a Visual Studio project in an existing location.
  • New Project from Remote location - creating a new PHP project that is initialized with files from a remote location, and setups both-way synchronization.

Introduction

Dependencies are essential for modern development. They save you time and energy. Functionalities you may need for your app like sending e-mails or logging can all be easily included as third party libraries. Thanks to the open source movement, there are many high quality packages to choose from. In the early days, including third-party libraries was cumbersome and error prone but luckily, today we have tools like Composer to help us.

Composer is an exceptional dependency manager for PHP. It replaces PEAR and rightfully so. PEAR requires that your project is specially prepared to work with it, where Composer gives you all the freedom you need without any special requirements. One major difference between these two tools is that PEAR installs dependencies globally and Composer installs them locally, in your project structure. PEAR is essentially a package manager and Composer is a dependency manager. That’s where the emphasis is.

Composer was inspired by projects like NPM and Bundler. The vast selection of compatible packages are hosted on the official Composer repository called Packagist. These packages are open source so you can contribute to them too. Popular frameworks and tools like Laravel, PHPUnit and Monolog can all be found here. You can even use a specific code revision of the package when including it in your project so you are getting great flexibility. Composer packages are versioned, so you can pin down the exact version of the package you need. This makes porting your project to another machine or to a CI service such as [Semaphore] [//semaphoreci.com] effortless.

In this tutorial, we’ll explore some of the most used Composer features and show how to use them. After following through, you should be comfortable with managing your PHP project’s dependencies with Composer.

Prerequisites

The software you need is as follows:

  • Some version of PHP 5, preferably the latest. Composer is compatible with PHP versions 5.3.2 and up.
  • Clients for Git and Subversion

I’ll be using PHP version 5.6.5, but it’s perfectly fine to use any other version you have as long as it’s newer than 5.3.2. If you don’t have PHP installed, you can do it with phpbrew which makes managing multiple PHP versions on the same machine easy.

Installation

Composer can be installed in two different ways.

Install Locally

Local installation will download composer.phar to the current directory. The drawback of this method is that you’ll always have to reference the Composer’s executable from the directory where it’s downloaded to.

$ curl -sS //getcomposer.org/installer | php

All settings correct for using Composer
Downloading...

Composer successfully installed to: /workspace/composer.phar
Use it: php composer.phar

$ php composer.phar --version

Composer version 1.0-dev [1d8f05f1dd0e390f253f79ea86cd505178360019]

Install Globally [Recommended]

Installing Composer globally is a handy way to have access to the tool from anywhere by just executing the composer command.

$ curl -sS //getcomposer.org/installer | php

All settings correct for using Composer
Downloading...

Composer successfully installed to: /workspace/composer.phar
Use it: php composer.phar

$ sudo mv composer.phar /usr/local/bin/composer
$ composer --version

Composer version 1.0-dev [1d8f05f1dd0e390f253f79ea86cd505178360019]

If you installed PHP with phpbrew running the command below is sufficient

We continue this tutorial with the assumption that Composer has been installed globally.

Configuring Composer

Composer is configured with a single file named composer.json located in the root directory of the project. To follow along, just create an empty directory called composer-tutorial, fire up a text editor and create an empty composer.json file in this directory. Here’s how the most simple Composer file looks like:

{
  "require": {
    "symfony/yaml": "2.6.4"
  }
}

The only requirement other than the fact that the file has to be in a JSON format, is the inclusion of the require key. This key defines your project’s dependencies with a list of packages and their respective versions.

Defining Dependencies

By convention, package names consist of the package’s vendor and its project name. This is done in an effort to avoid name conflicts. In our example above, the vendor is symfony and the project is called yaml.

The version of the package can be defined in multiple ways. This is where Composer gives you huge flexibility.

  • 2.6.4, locks the package to the exact version defined
  • >= 2.6.4, defines a range where the package’s version has to be at least 2.6.4 but a newer version is used if it’s available. Operators like = and != can also be used.
  • 2.6.*, which is equivalent to >=2.6 =2.6 =2.6.4 =2.6.4

Chủ Đề