You're reading from Laravel 5.x Cookbook
Composer is an amazing tool in PHP which allows us to pull in libraries from https://packagist.org/ and even our own private repositories. We will cover how to install a library and note some steps to save time.
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.
Perform the following steps to install Guzzle using Composer:
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.
And that is it!
In the previous section, we used composer to pull in Guzzle, so we're ready to use it in our project. However, we'd rather not have to instantiate the Guzzle client manually every time we invoke it—hardcoding URLs and authentication and settings with each use. A service provider can help to centralize some of this configuration, and later, we will use service providers to help swap in a mock implementation for testing purposes.
Providers can also help us to avoid writing code that directly calls to a service, which is often a very helpful practice. For example, we may make BillingProvider
that can use either Swipe or BrightTree
as a billing service. BillingProvider
allows us to easily switch between different implementations of the billing service.
Follow the steps in the Working with Composer install command and avoiding composer update section to pull in Guzzle, and start up your terminal.
Using the preceding work, let's take it one step further in how easy it is to use this Client in our code.
Install Guzzle and set up the provider just as we did previously, and you are ready for this next recipe.
Make a folder called
Facades
in yourapp
folder.Then add a file called
APIClient.php
and make it look like this:Then scroll way down to the
Façade
section of this file, where we can register theFacade
in ourconfig/app.php
file like this:Now, let's see it working in a test by adding the method called
seeing_our_facade_work
to our test:Now run the test:
So, the amount of work is all it takes to make your Provider just as easy to use as View, File, Storage, and all the other Facades that come from Laravel that make it easy and enjoyable to use.
Also, we can change the test to show it being just as easily swapped out. We will cover this in the testing chapter.
Laracasts and another great video from them: https...
Sometimes, you need to use a private repository on GitHub or another location. I will cover here how to set this up in your composer.
We need a private repo, so if you have it and its composer.json
is set up properly, you will be set from there.
First, go to GitHub and navigate to Settings│Personal access tokens:
At the command line, type this:
>composer config –g github-oauth.github.com THE_TOKEN_FROM_ABOVE.
Then, edit
composer.json
so that there are two new sections:Then, let's tell the composer to install this:
>rm –rf composer.lock vendor >composer install
Alright, let's talk about these steps. The first one is to make sure we are setting up our Homestead or Mac for easy access to the private repository. This is really key as well if you are doing 2FA on your GitHub account (which you should be doing). Step 2 wraps this up by adding it to your ~/.composer
configuration.
In the next part, we edit the composer.json
...