Reader small image

You're reading from  Laravel 5.x Cookbook

Product typeBook
Published inSep 2016
Reading LevelIntermediate
PublisherPackt
ISBN-139781786462084
Edition1st Edition
Languages
Tools
Right arrow
Authors (2):
Terry Matula
Terry Matula
author image
Terry Matula

Terry Matula is a web developer and Laravel advocate based in Austin, TX. He's been a passionate computer enthusiast since he first played Oregon Trail on an Apple//e. He started programming in BASIC at a young age, making simple Scott Adams-like games on a Commodore Vic-20. Since then, he's worked as a developer using Flash/ActionScript, ASP.NET, PHP, and numerous PHP frameworks, with Laravel being his favorite by far. He blogs web development tips and tricks at his website http://terrymatula.com
Read more about Terry Matula

Alfred Nutile
Alfred Nutile
author image
Alfred Nutile

Alfred Nutile is an Enterprise Architect and Laravel lead based in Western Massachusetts. He's been working in the industry since the mid 90's. He started in PHP and MySQL back around that time and has worked with Ruby on Rails, Drupal, and Angular along the way. He introduced Laravel into an enterprise web stack, where he is currently contracted at, as Laravel proved itself as an amazing framework to build API's and Angular heavy application. You can read more about him and checkout his blog at http://www.alfrednutile.info/ or follow him on Twitter at https://twitter.com/alnutile Make sure to checkout the discount LaraCasts is offering those who buy the book! Get a coupon for 50% on your first bill. Make it a yearly subscription and save $43! Coupon Code: LaracastsLovesPackt https://laracasts.com/signup?plan=yearly&coupon=LaracastsLovesPackt.
Read more about Alfred Nutile

View More author details
Right arrow

Chapter 2. Using Composer Packages

In this chapter, we will cover the following topics:

  • Working with the composer install command and avoiding the Composer update

  • Downloading and installing Guzzle using Composer

  • Making a provider

  • Using the Facade pattern

  • Using private packages

Introduction


We will cover some of the day-to-day workflows of setting up Laravel to use composer and many of the packages that exist out there for us to easily use in our PHP applications.

Working with Composer install command and avoiding Composer update


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.

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...

Making a provider


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.

Getting ready

Follow the steps in the Working with Composer install command and avoiding composer update section to pull in Guzzle, and start up your terminal.

How to do it...

The following steps will help you in making a provider...

Using the Facade pattern


Using the preceding work, let's take it one step further in how easy it is to use this Client in our code.

Getting ready

Install Guzzle and set up the provider just as we did previously, and you are ready for this next recipe.

How to do it...

  1. Make a folder called Facades in your app folder.

  2. Then add a file called APIClient.php and make it look like this:

  3. Then scroll way down to the Façade section of this file, where we can register the Facade in our config/app.php file like this:

  4. Now, let's see it working in a test by adding the method called seeing_our_facade_work to our test:

  5. Now run the test:

How it works...

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.

See also

  • Laracasts and another great video from them: https...

Using private packages


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.

Getting ready

We need a private repo, so if you have it and its composer.json is set up properly, you will be set from there.

How to do it...

  1. First, go to GitHub and navigate to SettingsPersonal access tokens:

  2. At the command line, type this:

    >composer config –g github-oauth.github.com THE_TOKEN_FROM_ABOVE.

  3. Then, edit composer.json so that there are two new sections:

  4. Then, let's tell the composer to install this:

    >rm –rf composer.lock vendor
    >composer install
    

How it works...

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...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Laravel 5.x Cookbook
Published in: Sep 2016Publisher: PacktISBN-13: 9781786462084
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Authors (2)

author image
Terry Matula

Terry Matula is a web developer and Laravel advocate based in Austin, TX. He's been a passionate computer enthusiast since he first played Oregon Trail on an Apple//e. He started programming in BASIC at a young age, making simple Scott Adams-like games on a Commodore Vic-20. Since then, he's worked as a developer using Flash/ActionScript, ASP.NET, PHP, and numerous PHP frameworks, with Laravel being his favorite by far. He blogs web development tips and tricks at his website http://terrymatula.com
Read more about Terry Matula

author image
Alfred Nutile

Alfred Nutile is an Enterprise Architect and Laravel lead based in Western Massachusetts. He's been working in the industry since the mid 90's. He started in PHP and MySQL back around that time and has worked with Ruby on Rails, Drupal, and Angular along the way. He introduced Laravel into an enterprise web stack, where he is currently contracted at, as Laravel proved itself as an amazing framework to build API's and Angular heavy application. You can read more about him and checkout his blog at http://www.alfrednutile.info/ or follow him on Twitter at https://twitter.com/alnutile Make sure to checkout the discount LaraCasts is offering those who buy the book! Get a coupon for 50% on your first bill. Make it a yearly subscription and save $43! Coupon Code: LaracastsLovesPackt https://laracasts.com/signup?plan=yearly&coupon=LaracastsLovesPackt.
Read more about Alfred Nutile