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
24

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”.

This article was written quite a while ago (9 years), please keep this in mind when using the information written here. Links, code and commands might be outdated or broken.

Random articles

  • The difference between “composer install” and “composer update” – nailed on the headThe difference between “composer install” and “composer update” – nailed on the head
  • [Link] How to require versions of PHP, HHVM / HipHop, GD, curl, openssl etc. with Composer[Link] How to require versions of PHP, HHVM / HipHop, GD, curl, openssl etc. with Composer
  • Composer problems ? Try full reset !Composer problems ? Try full reset !
  • How to install Composer on Windows 7 / 8 or UbuntuHow to install Composer on Windows 7 / 8 or Ubuntu
  • PHPStorm: 42 Tips and Tricks (47min video talk by Mikhail Vink at Dutch PHP Conference 2015)PHPStorm: 42 Tips and Tricks (47min video talk by Mikhail Vink at Dutch PHP Conference 2015)
  • What’s new in PHPStorm 9What’s new in PHPStorm 9
  • Experimenting with HHVM at Etsy (Link)Experimenting with HHVM at Etsy (Link)
  • Dangerous Performance Myths in the Web (video talk by Thomas Lohner, PHPUG Latvia)Dangerous Performance Myths in the Web (video talk by Thomas Lohner, PHPUG Latvia)
  • Install MINI in 30 seconds inside Ubuntu 14.04 LTSInstall MINI in 30 seconds inside Ubuntu 14.04 LTS
composerPHP
Share this

24 Comments

  • Yuvraj
    November 18, 2020 7:34 pm

    Just did my first composer project. Thank very much very useful article.

    Reply
  • swapnil
    September 9, 2017 1:43 pm

    good

    Reply
  • Jimmy
    August 16, 2017 8:16 pm

    Thank you so much. Thanks God for someone who knows how to explain something to someone without assuming that you already know it.

    I finally got my stuff installed. Awesome.

    Reply
  • morgans
    May 23, 2017 11:00 pm

    Hello, please am new to composer, am working on a project which uses unirest so how do I get the composer to run in my Web server, am using shared hosting. This question might sound awful but please help me out.

    Reply
  • Clocks
    May 17, 2017 6:06 pm

    Like many others, this was the only tutorial that was basic enough for me to follow xD

    I had to use the command line attached to xampp, the regular windows command line didn’t work to install =O

    also, I had to search through all of my C: files for every php.ini file and uncomment the openssl extension

    Reply
  • trafalgar
    February 9, 2017 3:03 pm

    Really nice explanation

    Reply
  • Susanth K
    June 20, 2016 7:57 pm

    Simple, Small and Beautiful !!!

    Reply
  • David Banes
    June 3, 2016 6:38 pm

    Excellent writing.

    Reply
  • amir
    April 27, 2016 11:14 pm

    really usefull. wonderful

    Reply
  • Edward Eldon
    February 1, 2016 5:13 pm

    thanks for this – I was going round and round and round trying to work out how to do the really obvoius stuff with composer – like set it up!

    Reply
  • Ankit Gaurav
    July 12, 2015 7:53 am

    This is one of the best tutorial on composer I found. I was having a real bad time going through the other posts on the web, gaining nothing. Thank you a lot.

    Reply
  • rajmohan
    May 26, 2015 6:57 am

    This is really an great tutorial fore anyone who need to understand php composer easily. Thanks for publishing

    Reply
  • Justin Zammit
    April 5, 2015 1:26 am

    Well done. Finally I understand exactly how composer works. Thanks.

    Reply
  • Purusothaman
    February 16, 2015 2:32 pm

    Really a good tutorial. Clearly written. I have a doubt on using Composer.

    I noticed that composer downloads all files/folders of the libraries to the vendor folder. What if I want to include only the required files for the production? I want to keep only the required files in the production version.

    Reply
  • raheel masood
    September 30, 2014 4:40 pm

    But what if we want to download a library which does not exists in composer ?

    Reply
    • raheel masood
      September 30, 2014 9:12 pm

      So do we know if a library exists in composer ?

      Reply
  • raheel masood
    September 30, 2014 4:39 pm

    I love you for this tutorial :*

    Reply
  • thephantomcat
    August 5, 2014 11:02 am

    Great tutorial, very very useful … 5 stars

    Reply
  • Dheeraj Sharma
    July 20, 2014 5:31 pm

    This one is the real tutorial. I did’nt get understand composer from any other.This one is great illustration. Thank you so much.

    Reply
    • Sajin
      March 29, 2016 10:53 am

      I agree 100%

      Reply

Leave A Comment Cancel reply

node.js

PayPal drops Java, goes node.js / JavaScript

First: this is not a Java-vs.-AnyOtherLanguage diss, just an article about a very interesting development: Using “frontend-languages” for serious server-side

composer

How to install Composer on Windows 7 / 8 or Ubuntu

Installation on Windows 7 / 8 Installing Composer on Windows 7 or 8 is extremely easy: Get the latest version

8 awesome pure CSS spinner / loader

Excllent spinner / loader in pure CSS, made with just one element (even if the demo shows this in a

Migrating Wikipedia to HHVM (@Scale Conference 2014)

Awesome topic, superfresh, directly from Scale Conference 2014. The title says everthing. Full quote from video’s description: As a top

How to get a single table out of a massive MySQL .sql database backup file (mysql dump splitter)

Imagine the following situation: Somebody backs up an entire MySQL database – a very large one – with common tools.

symfony-framework-logo

SensioLabs, creator of Symfony and Silex PHP frameworks, gets $7 million capital

The company behind the two popular PHP frameworks Symfony and Silex just raised 7 million dollars to “expand their open-source

GitHub finally introduces repo traffic stats

It has been a veeeery long time, but finally GitHub has introduced private traffic stats for all projects. To open

phpstorm-8

When PHPStorm’s cache eats up your harddisk space

I recently stumbled over this: By default PHPStorm (obviously in all versions since 1.0) creates a local history of all

“Fuck you. Pay Me.” How to deal with clients, the professional way. An excellent talk with Mike Monteiro.

Excellent (and very useful) speech by Mike Monteiro about how to deal with clients, how to create a professional relation

unreal-4-engine

Tim Sweeney talks about the future of game graphics (and which hardware we need to “clone” reality)

If you are interested in 3D/CAD and game graphics and get sweaty hands at each new high-end game engine trailer

1/4

Categories

Search

composer
A short & simple Composer tutorial
js javascript
JavaScript Testing Tactics (21min video by Justin Searls)
hack-php
Facebook releases HipHop (HHVM) 3.0, adds mysqli and support for Hack language
How to setup a config-free WordPress, PHP and MySQL (for local development) in Windows 7 / 8 in under 3 minutes
Experimenting with HHVM at Etsy (Link)
hiphop php
Vote for “Hack” for HipHop/HHMV support (future style PHP) in PHPStorm 8
-45% (or even 50%) off on DesignWall today
windows-xp-eol
Windows XP is officially dead from today. Do you know people still using it ? Punch them.
[Link] Interesting: Designing a Nuclear Waste Warning Symbol That Will Still Make Sense in 10,000 Years
ubuntu-14-04-lts lamp
How to install/setup a basic LAMP stack (Linux, Apache, MySQL, PHP) on Ubuntu 14.04 LTS
Support FLARUM, the future of PHP forum scripts (with some dollars on Kickstarter)
MINI, an extremely simple barebone PHP application
How to install/setup latest version of PHPMyAdmin on Ubuntu 12.04 LTS (Precise Pangolin)
js javascript
How JavaScript really works – An introduction into the JavaScript call stack by Philip Roberts (20min video)
hack-php
Wow! Facebook devs have rewritten and fixed PHP, releasing it as new language called “Hack” today

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
 
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
Do not sell my personal information.
Cookie SettingsAccept
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytics
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Others
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
SAVE & ACCEPT