DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • AWS Application Composer
  • How to Install OroCRM on Ubuntu 20.04
  • Inspecting Cloud Composer - Apache Airflow

Trending

  • Run Gemma 4 on Your Laptop: A Hands-On Guide to Google's Latest Open Multimodal LLM
  • Agentic Testing: Moving Quality From Checkpoint to Control Layer
  • Ujorm3: A New Lightweight ORM for JavaBeans and Records
  • The ORM Is Over: AI-Written SQL Is the New Data Access Layer

Using Composer in an Existing Project

By 
Lorna Mitchell user avatar
Lorna Mitchell
·
Jan. 02, 14 · Interview
Likes (2)
Comment
Save
Tweet
Share
10.9K Views

Join the DZone community and get the full member experience.

Join For Free

I've got an application (OK, scratty PHP script) that glues together some API things and shows them onto a dashboard for me. Recently, I updated it to use Guzzle as the consuming client, since Twitter now needs me to authenticate (I wrote about that if you're interested), and I used Composer to bring the new library in. It was very simple, so I thought I'd share it since it's quite a minimal example, and those are my favorite kind.

Getting Started with Composer

First of all, you'll need to either install composer, or at least get the .phar -- these examples use the composer.phar file from under the "Manual Download" section.

Composer comes with instructions, for a list of command types:

php composer.phar list

Once you've got Composer ready to go, we can move on and configure it.

State Your Dependencies

All you need to do to use Composer is to create its configuration file and then tell it to do its magic. :) All I want here is the Guzzle library, so my config file is rather simple:

{
    "require": {
        "guzzle/guzzle": "3.7"
    }
}

Get the Libraries

From this point, Composer can do the rest by itself. It will fetch all the libraries you set in the config file, but will also get any dependencies of those libraries. To ask it to do this, just type:

php composer.phar install

This will download all the libraries you need and place them in a vendor directory. In the case of Guzzle, it depends on other libraries (from the Symfony project), so my vendor directory now contains:

.
├── autoload.php
├── composer
├── guzzle
└── symfony

And there you have it: all the libraries you need are in place.

Tell Your Application About Its Dependencies

One more step: just add the following line into your application to make those new libraries available to it.

require 'vendor/autoload.php';

There you go. :) Enjoy using Composer even in existing/legacy applications, projects that aren't built in "the one true way" or really anywhere else you don't want to have to include libraries in your repositories.

Oh, and one more tip if you're just getting started: Add the vendor folder to your source control "ignore" file.

Further Reading

Nice post from Cal about including non-composer libs in your autoloader: http://blog.calevans.com/2013/07/21/using-3rd-party-libraries-in-composer-projects/

Composer documentation: http:// http://getcomposer.org/doc/

Further Reading

  • Upgrading PHP
  • Tools for Better PHP
  • Twitter Search API Using PHP and Guzzle
  • PSR-What?
Composer (software)

Published at DZone with permission of Lorna Mitchell. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • AWS Application Composer
  • How to Install OroCRM on Ubuntu 20.04
  • Inspecting Cloud Composer - Apache Airflow

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook