Logo
  • PHP
    • HipHop / HHVM
    • Modern PHP
    • PHPStorm
    • LAMP
    • Laravel
    • Composer
    • PDO
  • JavaScript
    • node.js
    • AngularJS
  • CSS
    • SASS
    • “CSS4” (CSS level 4)
  • HTML
  • Git
  • LAMP
  • Vagrant
  • UI / UX
  • Architecture of …
  • Off-Topic
With ♥ from Berlin
December 15, 2013
Chris
Composer, PHP
Comments Off on A short & simple Composer tutorial

A short & simple Composer tutorial

PreviousNext
composer

In this little Composer tutorial we’ll walk through the absolute basics of Composer, the PHP dependency management tool. Composer has changed the way PHP applications are built, and you should definitly take some minutes to get into this handy little thing.

 

What does Composer do ?

In the PHP world (and nearly every other language too) there are solutions for most common tasks, most of them available for free as open-source projects. So if you want to create professional HTML mails with PHP, you’ll probably not write everything by yourself from scratch, you’ll search for a mail library, in this case obviously PHPMailer, and put it into your project. And here Composer comes in: Instead of downloading the chart library by hand, moving it by hand into a manually selected folder, including it by hand and maybe messing around with autoloading issues, Composer organizes this, and everything Composer needs is ONE line of code, something like this:

"phpmailer/phpmailer": "5.2.*"

This simple line will add the latest version of the 5.2.x-branch of PHPMailer to your project – in the most cleanest and updateable (!) way possible. Composer will also check if your current server setup fits the minimum requirements of PHPMailer 5.2.x, so let’s say this version needs PHP 5.4 but you have PHP 5.3, then Composer will give out a big fat warning. Composer will also automatically download all necessary dependencies of the specific library. You’ll be able to use PHPMailer everywhere in your project, without further including issues. When you see a new version of PHPMailer comes out, you can update everything without breaking anything with just a simple action on the linux (or windows) command line.

 

A real example

To install Composer (on Ubuntu, Debian, CentOS, etc. or Windows 7/8) follow this tutorial.

Once Composer is installed, move into your web root and create a new project folder (to keep things clean and not mess into other projects), something like:

/var/www/myproject/

Create and empy file in the myproject folder, called composer.json:

/var/www/myproject/composer.json

Edit the composer.json, put this into the file and save it:

{
    "require": {
        "raveren/kint": "0.9"
    }
}

Note the syntax! As the file extension says, it’s a simple JSON file. This file contains structured data that say Composer what to do. In this very simple example we simply want to include the wonderful KINT tool into your (empty) project. KINT is the massivly improved version of var_dump() or print(). Now make sure you are in your project’s folder (var/www/myproject for example) and tell composer to download the stuff we just declared in the composer.json by doing:

composer install

The result will look like:

composer-install-successful

Note that Composer has created a folder named vendor in your project and downloaded KINT (and other stuff) into it! Don’t touch this folder, Composer handles everything. Let’s create an index.php in the project’s root, open it and put this in the file (and save):

<?php

require 'vendor/autoload.php';

This loads the Composer autoloader. So this line includes everything that Composer has downloaded into the project.

Let’s have a look onto the KINT GitHub page to get an idea on how to use KINT: The tools offers a very short and really cool debugging function d() ! So with the above require line in our index.php we can use stuff loaded by Composer directly, like – in this case –

d($any_variable);

I like! Okay, let’s try this out in our mini example. Create a demo data array and then echo it out via KINT, like this (we are still in the same index.php):

<?php
// load Composer
require 'vendor/autoload.php';

// create demo data
$variable = array(1, 17, "hello", null, array(1, 2, 3));

// use KINT directly (which has been loaded automatically via Composer)
d($variable);

Open this file with a browser now. The result should look like:

composer tutorial

This looks and behaves so much better than var_dump()! So what have we just done ? We have included a full external php tool with one line of code in the composer.json (and with a one-line-autoloader in the index.php). The external tool is instantly ready to use. We have included KINT in a very clean and commonly accepted way.

To add another external “dependency” simply add another line into the composer.json (keep the JSON syntax in mind, especially the comma in the end of the lines!):

{
    "require": {
        "raveren/kint": "0.9",
        "phpmailer/phpmailer": "5.2.*"
    }
}

This will add PHPMailer in the latest version of the 5.2 branch to the project. To get this, run

composer update

from the folder where your composer.json is. Usually when working with Composer we do a “composer update” or a “composer install”, depending on the situation. To get an idea what’s the difference between these two, have a look into The difference between “composer install” and “composer update”.

composerPHP
Share this

[Link] Improving Smashing Magazine’s Performance: A Case Study

The title says it all: A very interesting guide through the optimization process on one of the largest blogs in

battlefield-3-free

Electronic Arts / Origin offers Battlefield 3 for free (limited promo action) !

A little bit off-topic, but definitly cool: EA offers the award-winning Battlefield 3 for free these days, but only for

Redesigning Windows 8 – fantastic and clever drafts by Jay Machalani

The interface of Windows 8 has been the topic of heated discussions for a long time now, and everybody who’s

css4

Angelina Fabbro talks about “CSS4” in this excellent conference video

A very interesting talk about the future of CSS – let’s name it “CSS4” as we talk about spec level

The architecture of StackOverflow

One of the most interesting talks these weeks, and a rare insight into one of the most active pages on

Meet the developers behind Ableton (14min video)

An excellent short documentation about the developers behind Ableton, the legendary creators of Ableton Live and Push. Without doubt, Live

MINI2, an extremely simple barebone PHP application on top of Slim

For my daily work I often needed to setup super-simple PHP applications, just some more or less static pages plus

Hacking ATMs – A conference talk about the current security state of Windows XP driven cash machines

A few days ago the 30th edition of Germany’s Chaos Communication Congress took place, a high-profile event for IT-security and

O’Reilly’s Learning JavaScript Design Patterns by Addy Osmani for free

Addy Osmani has published his excellent book Learning JavaScript Design Patterns for free! You can read the entire book including

Disappointed by Watch Dogs’s graphics ? See how it looks with unlocked, hidden settings. Awesome!

When you are interested in 3D and game graphics in general, you probably stumbled upon these excellent and extremely good-looking

1/4

Categories

Search

phpstorm 7.0 php
A perfect video tutorial to get started with xdebug in PHPStorm
hiphop php
PHP’s HipHop outperforms PHP 5.5 with Zend OPCache and Nginx by 15-20 times
Ghost
[FREE SERVER PROMO] Install GHOST for free on a free SSD server with this coupon
Berlin, prepare for TOA conference (15th – 17th of July)
mod-rewrite-ubuntu-14-04-lts
How to enable mod_rewrite in Ubuntu 12.04 LTS
MINI2, an extremely simple barebone PHP application on top of Slim
php
How to install/setup latest version of PHP 5.5 on Debian Wheezy 7.0/7.1/7.2 (and how to fix the GPG key error)
phpstorm-8
A PHPStorm shortcuts cheat sheet (for Windows, Mac OS and Linux)
A quick history of Comic Sans, the most wrongly used font ever
ubuntu-14.04-lts
First view: Ubuntu 14.04 LTS brings PHP 5.5 and Apache 2.4
js javascript
Push database changes to all clients in real-time (!) with AngularJS and Firebase
bash-command-line-tutorial
Best introduction to unix command line / bash ever (by André Augusto Costa Santos)
Frontend Ops Conf 2014 – Keynote by Alex Sexton: “Front End Operations”
php
PHP 5.6.0 RC1 is available
github-logo-octocat
GitHub rolls out .PSD diff and viewing

Tags

apache bash centos composer conference coupon CSS debian fonts framework git GitHub hack HHVM HipHop HTML HTML5 IDE JavaScript JS LAMP laravel linux mod_rewrite MVC MySQL Nginx optimization PHP PHP 5.5 PHP 5.6 phpmyadmin PHPStorm security server SSD Ubuntu UI UX vagrant video virtual machine voucher VPS wordpress
Side-Project: Wordle-Solver:
www.wordle-helper.info

Pages

  • Privacy Policy