Composer is a dependency manager for PHP that recently became quite popular. It allows you to configure all the dependencies for your project and then handles all the downloading and configuring. Since it's easy to install it's a good choice if:
Composer is well documented but there are some specific things about using it with Yii. In the next example we'll configure our application to download Yii and some additional packages using Composer.
We need to prepare first.
webroot
.webroot/protected/composer.json
and put the following code inside:{ "repositories": [ { "type": "package", { "type":"package", "package":{ "name":"yiiext/iconized-menu-widget", "version":"1.2.1", "source":{ "type":"git", "url":"https://github.com/yiiext/iconized-menu-widget", "reference":"v1.2.1" } } } ], "require": { "php":">=5.3.0", "yiisoft/yii": "dev-master", "yiiext/iconized-menu-widget": "1.2.*" } }
cd webroot/protected composer update vendor/yiisoft/yii/framework/yiic webapp webroot
Y
then paste the following into your webroot/protected/views/site/index.php
file:<?php $this->widget('application.vendor.yiiext.iconized-menu-widget.EIconizedMenu', array( 'items'=>array( array('label'=>'Yii Framework', 'url'=> 'http://yiiframework.com/'), array('label'=>'Yii Application Development Cookbook', 'url'=> 'http://yiicookbook.org/'), array('label'=>'@sam_dark', 'url'=> 'https://twitter.com/sam_dark'), ), ))?>
webroot
. You should get the following:Every project is a package in Composer terms. So our project should be described as a package as well. Composer has lots of different configuration options. Here we're using only a small part of it. Let's review our configuration.
The repositories
part is about configuring where to look for packages. By default, Composer checks its main package repository at https://packagist.org/ and since Yii is officially supporting Composer, we can use it without defining a package.
Since the yiiext
extensions aren't aware of Composer yet, we should define package ourselves. The extension needed is at GitHub so it's a good idea to take code directly from the repository:
{ "type": "package", "package": { "name": "yiiext/iconized-menu-widget", "version": "1.2.01", "source": { "type":"git", "url":"https://github.com/yiiext/iconized-menu-widget", "reference":"v1.2.1" } } }
type
can be specified as:
package
: We're defining package definition ourselves. That's what we're using.composer
: That means the actual packages.json
definition is hosted elsewhere and should be downloaded via HTTP, FTP, or SSH and then parsed.vcs
: Same but packages.json
is in the version control repository.pear
: Useful to seamlessly get packages from http://pear.php.net/.Let's move on to package definition. name
is a unique package name. By convention packages are usually named as vendor/product
. By extension, yiiext/iconized-menu-widget
. version
indicates the current package version. To tell Composer where to get actual code we can specify source
and/or destination
. Which one will be used is determined based on which options the user supplied while executing composer update
. In our case we're taking source code from the git
tag named v1.2.1
using HTTPS. There are lots of other options such as downloading and unpacking a ZIP file, downloading a single file, or using version control systems other than git
.
So now we have packages defined and we're ready to define the main package that is our actual application. For an application it's not required to specify name
or version
. The only mandatory section is require
:
"require": { "php":">=5.3.0", "yiisoft/yii": "dev-master", "yiiext/iconized-menu-widget": "1.2.*" }
In our case we're checking if the PHP version is equal to or higher than 5.3.0
. Composer supports more environmental packages so you can check, for example, for the existence of a PHP extension. Then we're requiring yiisoft/yii
, latest development version, and yiiext/iconized-menu-widget
, any 1.2.x
version. When executing composer update
it will look for these packages starting from the package definition.
Note that when we've tried a widget from a package it was referenced as application.vendor.yiiext.iconized-menu-widget.EIconizedMenu
. Here application
is a root alias that points to your protected
directory, vendor
is a standard name for a directory containing downloaded Composer packages, and yiiext.iconized-menu-widget
is a combination of vendor
and product
as defined in composer.json
.
Composer provides much more functionality than was described in this recipe. To learn more about it check documentation at http://getcomposer.org/. Also it's useful to check answers at Stack Overflow (http://stackoverflow.com/questions/tagged/composer-php).
3.144.116.159