Run composer on Kudu (Azure Websites)

Composer is a dependency package manager in PHP. With Composer you can solve the problem where your project depends on a number of PHP based libraries and some of those libraries depend on other libraries. With composer you can declare the things your project depends on and composer finds out which versions of which packages need to be installed, and installs them (meaning it downloads them into your project).

KUDU is a developer’s paradise for Azure websites where there are ample tools  from troubleshooting, analysis and diagnostics. Check out the kudu project on GitHub. To access your KUDU site, navigate to https://*****.scm.azurewebsites.net where ***** is the name of your website NAME running on Azure Websites.

KUDU supports single sign on , so enter your Azure account email credentials and once authenticated you should see a page similar to the one illustrated below

kudu

Installation

Click on Debug Console->CMD to access the debug console window. Create a directory bin under site root folder wwwroot and run the following command to Install composer.

install

To use composer globally on your site content , create a batch script

batchscript

To check if composer was installed correctly, run the following composer.bat

check

 

Declaring dependencies

 

Let’s say you are creating a Zend Framework. In order to add it to your project, all you need to do is create a composer.json file under site root wwwroot folder which describes the project’s dependencies. For example , my file would contain the following :

{
"require": {
             "zendframework/zendframework": "2.3.*"
   }
}

We are simply stating that our project requires Zend package starting with 2.3 and the repository from which the package can be installed by. The require key is used to list the project’s dependencies.

How to run composer

Once you have composer.json file created you can run the composer install command to install all the dependencies

dependencyinstall

In addition to downloading the Zend libraries , Composer also prepares an autoload file that’s capable of autoloading all of the classes in any of the libraries that it downloads. To use it, just add the following line to your code’s bootstrap process:

        require'vendor/autoload.php';

Now start using Zend Framework to build you Zend application on Azure websites. To keep learning more about Composer, check out Composer Documentation.

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s