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
mod-rewrite-ubuntu-14-04-lts

How to install / setup PHP 5.5.x on Ubuntu 12.04 LTS

Please note: This works fine. But this package will also upgrade your apache to version 2.4 which has different config

Perfect HTML email templates for perfect HTML emails (outlook!) with INK

The people behind FOUNDATION, the excellent responsive frontend framework, have just released a set of highly professional, perfectly compatible HTML

js javascript

JavaScript Testing Tactics (21min video by Justin Searls)

How to hack time (KUNG FURY promo campaign)

:) Finally, KUNG FURY is out! No need for further words, unless you lived in a cave for the last

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

[Link] Interesting: Designing a Nuclear Waste Warning Symbol That Will Still Make Sense in 10,000 Years

Interesting article for everybody with a faible for UI/UX: Slate – Designing a Nuclear Waste Warning Symbol That Will Still

First look on Gitter, the chat for GitHub

GitHub has definitly become the #1 platform for git-based public repositories on the planet, no question. The site offers excellent

Create a fast, perfect and bootable 1:1 Windows backup (full clone of HDD) for SSD migration

In this article I want to share a super-simple, extremely fast and totally free workflow with you that will create

How to show the available version of a package (before doing apt-get install)

To show the version of the package that will be installed with apt-get install, do apt-cache policy packagename. To show

Useful basic linux stuff: Show kernel version, distribution name and distribution version on Ubuntu systems

Always nice to know: Show kernel version: uname -r Show kernel version and CPU: uname -a Show distribution version: lsb_release

1/4

Categories

Search

How to fix the ugly font rendering in Google Chrome
Ghost
[FREE SERVER PROMO] Install GHOST for free on a free SSD server with this coupon
phpstorm 7.0 php
How to setup and use XDEBUG with PHPStorm 6/7 (locally in Windows 7/8 and Mac OS X)
MINI2, an extremely simple barebone PHP application on top of Slim
How Snapchat wants to earn money (by establishing vertical videos)
js javascript
JavaScript Testing Tactics (21min video by Justin Searls)
php
“Belt” adds very clever everyday functions to PHP, comes with JavaScript naming styles and eventually solves the needle/haystack problem
GitHub introduces revert button / rollback for merged pull requests
[Link] Improving Smashing Magazine’s Performance: A Case Study
github-logo-octocat
GitHub buys Easel.io, a code-free full website creator worth a look
phpstorm-8
PHPStorm 8 has just been released
php
How to install PHP curl extension (in 5 seconds)
hiphop php
PHP’s HipHop outperforms PHP 5.5 with Zend OPCache and Nginx by 15-20 times
ilovepreloaders – A tumblr collection of preloader animations
php
PHP 5.7 gets refactored core, is 10%-30% faster than PHP 5.5! Wow!

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