-
Notifications
You must be signed in to change notification settings - Fork 0
Composer
Composer is a valuable tool in PHP development that makes it easy to integrate different libraries into your project. You must install it to use the instructions contained in this wiki.
Composer allows you to manage these libraries as well, choosing between development or stable releases to ensure newer versions work properly with your application before it's released.
###Installing Composer
Go to the Composer website and download the appropriate version of Composer for your operating system. The installation is straightforward.
###Using Composer
Composer is used through a terminal (either Command Prompt on a Windows machine, or Terminal on Unix-based machines like Mac OS X or Linux distributions). You need to navigate to the directory containing the 'composer.json' file, which is typically in your project's root directory. All composer commands are preceded by composer
, so if you wanted to call the 'install' command, you would type composer install
into your Terminal and hit Enter.
####Note for Macs/Linux
There doesn't appear to be any consistency here with how you call composer. Sometimes you can call it as php composer.phar {command}
or just php composer {command}
. This appears to depend on how you install it, but even then the result isn't always quite right. From here on out when you see composer commands, make sure you're using the correct pattern for your setup.
###composer.json
composer.json is a JSON-formatted file that contains all of the instructions specific to your project. Many of these fields describe your project, while others help manage the libraries you're utilizing. The Composer Documentation outlines all possible fields.
Any time you're working with Composer in your project, you need to be calling the commands from the directory which contains this file. Otherwise, it won't know how to handle them. The exception is when you're creating a new project, in which case this file won't exist yet.
###composer.lock
This is a file created after Composer has fetched all dependencies which describes the exact versions of libraries that have been installed in the project. You won't need to touch this, as Composer manages this on its own.
###Important Commands
There are a few commands you'll find yourself using constantly with Composer. The complete list of commands can be found in the Composer Documentation, but the most notable are below:
####dump-autoload
One of the greatest features in Composer is its built-in class autoloader, which is based off of namespaces in your project. The only downside is that each time you add a new class, you must run 'dump-autoload' for it to detect them. If you create a new class and are wondering why your project can't find it, you probably forgot to do this.
On a production site, you can run with command with the '--o' option, which optimizes the autoloader. This isn't recommended for test sites.
####install
This command installs all project dependencies listed in composer.json. If you've just created a new project, you shouldn't need to run this unless you cloned it from a repository without Composer.
####update
This updates all dependencies to their latest version(s). Be sure that you're not using this on a production site unless you've double-checked that these versions work in test. It also updates autoload classes.
THINKer Framework + Libraries Copyright © Cory Gehr. All rights reserved.