21/03/2017

Installing Composer With Magento

By snorlaxprime

To install Composer with Magento, you will need to have access to the ssh on your web hosting, if you don’t have this option, the suggestion is to create a virtual environment for your UAT environment that you can install prior to deployment to the production.

In this example, I was using “Turnkey Magento”, you can download the VM in the following link

https://www.turnkeylinux.org/magento

Follow the instruction to install and configure it. Then login to the root account. In this example I will be installing the “Fontis Australia Post” extension.

First things first, I will copy the composer.json file to the root of magento installation.

The content of the composer.json is as below

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
{
  "name":"fontis/australia",
  "type":"magento-module",
  "license":"OSL-3.0",
"description":"Provides essential functionality for Australian stores",
"homepage":"https://www.fontis.com.au/australia-magento-extension",
  "require":{
    "magento-hackathon/magento-composer-installer": "*",
    "fontis/auspost-api-php": "1.x",
    "fontis/composer-autoloader" : "*"  
  }
  "extra":{
    "magento-root-dir":"/var/www/magento"
  }
}

The composer.json file can be created manually in the location /var/www/magento

Once the composer.json configuration file is created, all that needs to be done is install composer (curl –O http://getcomposer.org/composer.phar) and run it:

    php composer.phar install

And that’s all. The listed packages and all their dependencies will be installed in the project.

The place where composer installs the libraries is a directory called vendor.

Should the contents of the composer.json file ever be changed, run

    php composer.phar update

and the new configuration will be applied.

That is also how you uninstall a package: you remove it from the require list in the composer.json and run composer.phar update.

Directory layout

Before we wrap up, there is one more thing that should be talked about. As mentioned before, composer installs libraries (and Magneto modules) into a vendor directory.

So this directory needs to be integrated somehow into the project.

When placed directly in a Magento root directory we would end up with a structure like this:

1
2
3
4
5
6
7
8
9
10
11
12
var/www/magento/
 ├── .git/
 ├── composer.json
 ├── composer.lock
 ├── index.php
 ├── app/
 ├── js/
 ├── lib/
 ├── media/
 ├── skin/
 ├── var/
 └── vendor/

This works, but you need to make sure that the vendor directory, and also the compser.json and composer.lock files aren’t accessable from a web browser.

Once this is done, you can then ftp the vendor directory out from your UAT environment (in this case the turnkey magento virtual machine) and put it into your production environment.

And last but not the least, the one file that is being modified by composer is Mage.php file located in

/var/www/magento/app/Mage.php

make a backup of the same file in your production environment, and ftp this file to your production folder

/magento/app 

Once this is done, you can go to the Magento System -> Configuration and configure the Fontis Australia Post extension.