Skip to content

Instantly share code, notes, and snippets.

@richjava
Created November 23, 2014 23:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save richjava/e38d97693fa4371cab1e to your computer and use it in GitHub Desktop.
Save richjava/e38d97693fa4371cab1e to your computer and use it in GitHub Desktop.
Getting Started on Composer
*************Install Composer on your computer*************
If you are not behind a proxy, skip this section - Composer has a windows installer executable which makes it easy. If you are behind a proxy, you can do the following:
Go to Environment Variables (start menu "env"+Enter)
In "User variables..." section (the top section), click "New".
For the variable name, write "PATH".
For the variable value, paste the path to your php executable file (in .../xampp/php).
Click OK on this dialog and OK on the main Environment Variables dialog.
In user directory, create a folder called "Composer".
If Git Bash is already open. Close and reopen it. On Git Bash:
Navigate to Composer directory
Paste in:
curl -sS https://getcomposer.org/installer | php
Check your "Composer" folder to see if composer.phar has been inserted.
In Git Bash, (at the “Composer folder directory),run this command to check Composer is running:
composer.phar
Composer can't be run from any directory on your machine, so you need to add it to your path. Add path to composer.phar to your PATH Environment Variable by appending ";" and then the path to your "Composer" folder (just append, don't overwrite any existing values because you still need them in your path).
*************Adding composer to a project*************
Navigate to project in Git Bash (type full path or use "Insert" button on your keyboard to paste), i.e.: cd /d/Users/<myusername>/Documents/NetBeansProjects/<MyProject>, or right click on project directory if you specified this functionality during Git installation.
There are a few ways you can use Composer to add dependencies to your project. One way is to add a composer.json file to the root of your project and specify the dependencies your project needs (this is the way we will do it). So your composer.xml might look something like this, depending on the dependencies you want to add to your project (Carbon is a simple API extension for DateTime with PHP 5.3+. It is on Github at https://github.com/briannesbitt/Carbon):
{
"require": {
"nesbot/Carbon": "*"
}
}
and then on the command line/Git Bash, you can run:
composer install –-no-dev
The “—-no-dev” flag indicates that we don’t want to develop the package, we just want to use it, so some stuff we don’t need will be excluded.
A folder named “vendor” will be created in the root of your project which will contain the classes that Composer will use to load classes into your project, as well as the nesbot package which is specific to Carbon.
Now, in the index.php of the project use this code to check that Carbon functionality is working (after this, check out the code on the Carbon Github repository readme to get started with Carbon):
require 'vendor/autoload.php';
use Carbon\Carbon;
printf("Now: %s", Carbon::now());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment