Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Building RESTful Web Services with PHP 7

You're reading from  Building RESTful Web Services with PHP 7

Product type Book
Published in Sep 2017
Publisher Packt
ISBN-13 9781787127746
Pages 244 pages
Edition 1st Edition
Languages
Author (1):
Waheed ud din Waheed ud din
Profile icon Waheed ud din

Table of Contents (16) Chapters

Title Page
Credits
About the Author
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface
1. RESTful Web Services, Introduction and Motivation 2. PHP7, To Code It Better 3. Creating RESTful Endpoints 4. Reviewing Design Flaws and Security Threats 5. Load and Resolve with Composer, an Evolutionary 6. Illuminating RESTful Web Services with Lumen 7. Improving RESTful Web Services 8. API Testing – Guards on the Gates 9. Microservices

Chapter 5. Load and Resolve with Composer, an Evolutionary

Composer is not only a package manager, but also a dependency manager in PHP. In PHP, if you want to reuse an open source component, the standard way to do it is to use an open source package through Composer, as Composer has become a standard for making packages, installing packages, and auto-loading. Here, we have discussed a few new terms, such as package manager, dependency manager, and auto-loading. In this chapter, we will go into detail of what they are and what Composer provides for them.

The preceding paragraph explains what Composer mainly does, but Composer is more than that.

In this chapter, we will look at the following things:

  • Introduction to Composer
  • Installation
  • Usage of Composer
    • Composer as a package and dependency manager
    • Installing packages
      • How Composer works
      • Composer commands
      • The composer.json file
      • The composer.lock file
    • Composer as an autoloader

Introduction to Composer


The PHP community is a sort of divided one and there are lot of frameworks and libraries. Since there are different frameworks available, a plugin or package written for one framework cannot be used in another. So, there should be a standard way to write and install packages. Here comes Composer. Composer is a standard way to write, distribute, and install packages. Composer inspired by npm (Node Package Manager) from the node.js ecosystem.

In fact, most developers use Composer to install the different packages they use. This is also because using Composer to install a package is convenient because packages installed through Composer can also be easily auto-loaded through Composer. We will look into auto-loading later in this chapter.

As stated before, Composer is not just a package manager, but also a dependency manager. This means if a package needs something, Composer will install those dependencies for it and it will then auto-load accordingly.

Installation


Composer requires PHP 5.3.2+ to run. Here is how you can install on different platforms.

Installation on Windows

On Windows, installing is very easy, so we not go into much detail. You just need to download and execute the setup from getcomposer.org. Here is the link: https://getcomposer.org/Composer-Setup.exe.

Installation on Linux/Unix/OS X

There are two to install Composer, they are locally and globally. You simply install through the following commands:

$ php -r "copy('https://getcomposer.org/installer', 'Composer-setup.php');"
$ php -r "if (hash_file('SHA384', 'composer-setup.php') === '669656bab3166a7aff8a7506b8cb2d1c292f042046c5a994c43155c0be6190fa0355160742ab2e1c88d40d5be660b410') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
$ php composer-setup.php
$ php -r "unlink('composer-setup.php');"

The preceding commands perform following respectively:

  1. Downloads the Composer setup PHP file
  2. Verifies the Installer...

Usage of Composer


Composer is a dependency manager and has other different uses. Composer is used to install packages while resolving dependencies. Composer is also very good at auto-loading. There are more uses of as well. Here, we will discuss the different uses of Composer.

Composer as a dependency manager

Composer is a dependency manager. Now you can package your in a way that you don't need to ship third-party dependencies with it. You just need to tell its dependencies. In fact, your package dependencies can have more dependencies, and those dependencies can also have more dependencies. So, resolving all those dependencies while making a package or bundle could be really tiresome. But it is not, thanks to Composer.

Since is also a dependency manager, dependencies are not a problem anymore. We can just specify dependencies in a JSON file, and those dependencies are resolved by Composer. We will look into that JSON file shortly.

Installing packages

If we have dependencies (other packages...

Summary


Composer is a standard way to make and use reusable components. Nowadays, a lot of stuff has already been done and can be reused. For this reason, in PHP, Composer is a standard method. In this chapter, we have seen how Composer works, what it's usages are, how can one install packages through it, and a lot more. However, one thing that we haven't touched on in this chapter is how we can make packages for Composer. This is because our focus was on how we can reuse already available Composer packages. If you want to learn how to a create Composer package, then start from here: https://getcomposer.org/doc/02-libraries.md.

If you want to know more about Composer, you can:

  1. Go and read Composer documentation at https://getcomposer.org/doc/.
  2. Open and start reading important files. You can open and read different Composer files, such as composer.json and composer.lock , from different packages.

Till now, we have seen how we can reuse Composer components to avoid writing everything on our own...

lock icon The rest of the chapter is locked
You have been reading a chapter from
Building RESTful Web Services with PHP 7
Published in: Sep 2017 Publisher: Packt ISBN-13: 9781787127746
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.
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}