Laravel 5.x Cookbook
上QQ阅读APP看书,第一时间看更新

Working with Composer install command and avoiding Composer update

Composer is an amazing tool in PHP which allows us to pull in libraries from cover how to install a library and note some steps to save time.

Getting ready

We covered installing Composer on your Mac in Chapter 1, Setting Up and Installing Laravel, though you can use it inside Homestead if you need to.

How to do it...

Perform the following steps to install Guzzle using Composer:

  1. In this example, we will use Composer to install Guzzle (a powerful PHP HTTP client). Make sure you are in your App directory, and in this example, I will be on my local computer and not in Homestead just to make the file processing go faster:
    >composer require "guzzlehttp/guzzle":"^6.1"
    

    This will take a minute or less to run.

  2. And that is it!

How it works...

First, let me say this is a good example of a short tip, which is better than a long one. Maybe other instructions will tell you to put this into your composer.json file and then run composer update, but stay away from composer update unless you really like waiting around, and you really need to update everything you have installed!

What else happened here is really good to understand, since this is a big part of Laravel and modern PHP. Composer added the library to your vendor folder:

It made a folder for the libraries called Vendor Name and Projects Name. Then, it updated composer.json to load this new library!

This means we did not even have to edit the file.

Also note that there is a composer.lock file now in the root of your application, which you never edited. It shows what Composer pulled in to satisfy the dependencies of all the libraries that you have installed, as well as the file called vendor/autoload.php to build up all the PHP namespaces.

We will use Composer a number of times in this book. It really has changed the way we build apps in PHP for the better.

There's more...

If we look inside the Composer.json file, we will also notice some default namespaces and hooks that we can add to as needed:

See also