Composer is a dependency management utility for PHP projects. What that means is: you can save time on your projects by importing libraries that other developers have written.

For this series of examples, I plan to build a basic image uploading script using the Silex PHP microframework. I want to try uploading images from my iPhone using Mobile Safari, because I have heard of some real-world limitations in that situation that I want to explore. Part One will deal with the basic setup and a slightly different approach to the standard "Hello World".

Composing the Project

To get started with composer, install it by following the instructions at http://getcomposer.org/download/. I prefer to install it globally on my systems (Linux and OS X), so I can invoke it easily and keep it up to date - this simply means that I put the file in one of the folders in my system path, usually /usr/local/bin/composer. If you've followed the instructions from my previous post, PuPHPet by the BBQ, you'll have a full virtual development environment with composer pre-installed.

Once composer is installed, let's begin by creating a folder for the project (in this case, I will call it /data/vhosts/upthing/). Inside the folder, run this command:

$> composer init

This kicks off the interactive initialization process. Give the project a name (whateverthing/upthing), a quick description (UpThing), add your contact info, set minimum stability to "dev", and choose an MIT license.

The next stage is choosing dependencies. This part will be very simple:

Search term: silex/silex Version: *

Then, after saying "no" to dev requirements and "yes" to confirm generation, run the install command to finish up:

$> composer install

Now that the composition is finished, we can begin getting things wired up.

Bootstrapping (and I don't mean CSS)

The first thing to do is create a Bootstrap file. This will just be a quick and simple one:

<?php require __DIR__ . '/vendor/autoload.php'; $app = new Silex\Application(); // Disable this setting in production $app['debug'] = true;

This includes the composer autoloader, and initializes the Silex core.

Controlling the Front

The next step is creating the initial index.php, which will act as a 'Front Controller' for the project. I like to put this in a subfolder called "web/".

<?php require __DIR__ . '/../bootstrap.php'; use Symfony\Component\HttpFoundation\Request; // Declare our primary action $app->get( '/', function() { return 'Mr Watson, come here, I want to see you.'; }); $app->run();

At this point, if you load index.php from your web server, it should respond in plain text:

Mr Watson, come here, I want to see you.

To try it out in the PHP development web server included in PHP 5.4, run "php -S 0.0.0.0:80 web/index.php" and then load http://localhost/ in your browser. If you're using a Vagrant/PuPHPet VM, you would use something like http://192.168.56.101/ instead. You may require administrator privileges to open port 80, so you would use "sudo php" instead of just "php" to accomplish that on Linux and OS X.

Ready to Roll

That's one quick way to get ready to roll on a fresh Silex project using Composer. I just did a test run to make sure the source examples worked, and it took about four minutes to go from an empty folder to a working primary action. In the next part, I'll show you how to write some controller actions that accept a file upload & provide a result.

Don't forget to subscribe to the newsletter. Thanks for checking in! :)

Continue to Part 2 »

View the Source on GitHub »