Tuesday 6 November 2012

Zend Framework 2 : Use Composer & Packagist PHP

Introduction

Composer is a tool for depedency management in PHP. It allows you to declare the dependent libraries in your project needs and it will install them in your project.

Composer is not a package manager but composer manages them (packages or libary) on a per-project basis, installing them in a directory (e.g vendor) inside your project. By default it will never install anything globally. thus , it's a dependency manager.

The problem that composer can solve are :

a) You have a project that depends on a number of libraries.
b) Some of those libraries depend on other libraries.
c) You declare the things you depend on.
d) Composer finds out which versions of which packages need to be installed, and installs them (meaning it download them into your project like apt-get command in ubuntu).

Installation

First, you have to install curl on your pc by type command on your terminal :
 sudo apt-get install curl 
Then type command to get executetable composer.phar
 $ curl -s https://getcomposer.org/installer | php
This will just check a few PHP settings and then download composer.phar to your working directory. This file is the Composer binary. It is a PHAR (PHP archive), which is an archive format for PHP which can be run on the command line, amongst other things.

You can install Composer to a specific directory by using the --install-dir option and providing a target directory (it can be an absolute or relative path):
 $ curl -s https://getcomposer.org/installer | php -- --install-dir=bin
If you failed on executing the terminal, you can go directly on to http://getcomposer.org/composer.phar for get the file. Then we move on to make composer.json file. the file that will be read when installing the packet.

Assume that we want install WebinoImageThumb package, the package is a module in zend framework 2, so we just have to write in composer.json file :
 {
    "require": {
         "webino/webino-image-thumb": "1.0.0"
    }
}
After that we move to our terminal, execute the composer.phar :
 php composer.phar install
if we did it correctly, there's must be notification as below :


and will make a new module in our vendor directory :


And then, we set to add our module in config application.config.php to load our WebinoImageThumb module.

Then, we make new module and settle the module up till its work properly. if you forget how to make it, you can try to make module in my previous tutorial.

well, then add an action in our controller :
 public function indexAction()
{
    $thumbnailer    = $this->getServiceLocator()->get('WebinoImageThumb');
    $thumb          = $thumbnailer->create('public/images/Astro.jpg',$options = array());
    
    $thumb->createReflection(40, 40, 80, true, '#a4a4a4');
    $thumb->resize(200,200);        
    $thumb->save('public/images/resized.jpg');
    
    $view = new ViewModel();
    $view->path = 'images/resized.jpg';
    
    return $view;
}
setting our index view template :
 
After that, we can go to browser to see the preview :


Note :

- Put your image into /public/images directory for as an example and make it thumbnail. in my application I use Astro.jpg.

reference :
http://getcomposer.org/doc/00-intro.md
http://www.ivankristianto.com/os/ubuntu/howto-install-curl-in-php-apache/379/
http://samsonasik.wordpress.com/2012/05/08/packagist-php-package-repository-for-composer/

Image source :
http://adamguzowski.blogspot.com/2010/10/youd-prefer-astronaut.html
Copyright © 2012 Clighter | Powered by Blogger