extend-using-composer.md 5.85 KB
Newer Older
1 2 3
Composer
========

4
> Note: This section is under development.
Qiang Xue committed
5

6
Yii2 uses Composer as its dependency management tool. Composer is a PHP utility that can automatically handle the installation of needed libraries and
7
extensions, thereby keeping those third-party resources up to date while absolving you of the need to manually manage the project's dependencies.
8 9 10 11

Installing Composer
-------------------

12 13
In order to install Composer, check the official guide for your operating system:

14
* [Linux](http://getcomposer.org/doc/00-intro.md#installation-nix)
15 16
* [Windows](http://getcomposer.org/doc/00-intro.md#installation-windows)

17
All of the details can be found in the guide, but you'll either download Composer directly from <http://getcomposer.org/>, or run the following command:
18 19 20 21

```
curl -s http://getcomposer.org/installer | php
```
22

23
We strongly recommend a global composer installation.
24

Qiang Xue committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38
Installing Composer Class Autoloader
------------------------------------

Make sure the [entry script](concept-entry-scripts.md) of your application contains the following lines of code:

```php
// install Composer's class autoloader
require(__DIR__ . '/../vendor/autoload.php');

// include Yii class file
require(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');
```


39 40 41 42
Working with composer
---------------------

The act of [installing a Yii application](installation.md) with 
43 44

```
45 46 47 48
composer.phar create-project --stability dev yiisoft/yii2-app-basic
``` 

creates a new root directory for your project along with the `composer.json` and `compoer.lock` file.
49

50
While the former lists the packages, which your application requires directly together with a version constraint, while the latter keeps track of all installed packages and their dependencies in a specific revision. Therefore the `composer.lock` file should also be [committed to your version control system](https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file).
51

52 53
These two files are strongly linked to the two composer commands `update` and `install`.
Usually, when working with your project, such as creating another copy for development or deployment, you will use 
54

55 56 57 58 59 60 61
```
composer.phar install
```

to make sure you get exactly the same packages and versions as specified in `composer.lock`. 

Only if want to intentionally update the packages in your project you should run 
62 63

```
64
composer.phar update
65 66
```

67 68
As an example, packages on `dev-master` will constantly get new updates when you run `update`, while running `install` won't, unless you've pulled an update of the `composer.lock` file.

Qiang Xue committed
69
There are several parameters available to the above commands. Very commonly used ones are `--no-dev`, which would skip packages in the `require-dev` section and `--prefer-dist`, which downloads archives if available, instead of checking out repositories to your `vendor` folder.
70 71

> Composer commands must be executed within your Yii project's directory, where the `composer.json` file can be found.
72 73
Depending upon your operating system and setup, you may need to provide paths to the PHP executable and
to the `composer.phar` script.
74

75 76 77 78 79

Adding more packages to your project
------------------------------------

To add two new packages to your project run the follwing command:
80 81

```
82
composer.phar require "cebe/indent:>=1.0.2" "cebe/yii2-gravatar:>1.1"
83 84
```

85
This will resolve the dependencies and then update your `composer.json` file.
86 87
The above example says that a version greater than or equal to 1.0.3 of indent converter package 
and version 1.1 or greater of gravatar extension are required.
88

89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
For details of this syntax, see the [official Composer documentation](https://getcomposer.org/doc/01-basic-usage.md#package-versions).

The full list of available Composer-supported PHP packages can be found at [packagist](http://packagist.org/). You may also search packages interactively just by entering `composer.phar require`.

### Manually editing your version constraints

You may also edit the `composer.json` file manually. Within the `require` section, you specify the name and version of each required package, same as with the command above.

```json
{
    "require": {
        "michelf/php-markdown": ">=1.4",
        "ezyang/htmlpurifier": ">=4.6.0"
    }
}
```

Once you have edited the `composer.json`, you can invoke Composer to download the updated dependencies. Run 

```
composer.phar update michelf/php-markdown ezyang/htmlpurifier
``` 

afterwards.

> Depending on the package additional configuration may be required (eg. you have to register a module in the config), but autoloading of the classes should be handled by composer.
115 116


Qiang Xue committed
117
Using a specific version of a package
118 119
------------------------------------

120 121
Yii always comes with the latest version of a required library that it is compatible with, but allows you to use an older version if you need to.

122 123 124 125 126 127 128 129 130 131 132 133 134 135
A good example for this is jQuery which has [dropped old IE browser support](http://jquery.com/browser-support/) in version 2.x.
When installing Yii via composer the installed jQuery version will be the latest 2.x release. When you want to use jQuery 1.10
because of IE browser support you can adjust your composer.json by requiring a specific version of jQuery like this:

```json
{
    "require": {
        ...
        "yiisoft/jquery": "1.10.*"
    }
}
```


136 137 138 139 140 141 142 143 144 145 146
FAQ
---

### Getting "You must enable the openssl extension to download files via https"

If you're using WAMP check [this answer at StackOverflow](http://stackoverflow.com/a/14265815/1106908).

### Getting "Failed to clone <URL here>, git was not found, check that it is installed and in your Path env."

Either install git or try adding `--prefer-dist` to the end of `install` or `update` command.

147 148 149 150
### Should I Commit The Dependencies In My Vendor Directory?

Short answer: No. Long answer, see [here](https://getcomposer.org/doc/faqs/should-i-commit-the-dependencies-in-my-vendor-directory.md).

151

152 153 154
See also
--------

155
- [Official Composer documentation](http://getcomposer.org).