0
0
PHPprogramming~30 mins

Composer require and dependency management in PHP - Mini Project: Build & Apply

Choose your learning style9 modes available
Composer require and dependency management
📖 Scenario: You are building a simple PHP project that needs to use an external library for handling HTTP requests. Composer is the tool that helps you manage these external libraries, called dependencies.In this project, you will learn how to add a dependency using Composer and check that it is installed correctly.
🎯 Goal: Learn how to use composer require to add a dependency to your PHP project and verify it is installed by checking the vendor folder and composer.json file.
📋 What You'll Learn
Create a new PHP project folder
Initialize Composer in the project
Use composer require to add the Guzzle HTTP client library
Verify the dependency is installed and listed in composer.json
💡 Why This Matters
🌍 Real World
Composer is the standard tool for managing PHP project dependencies. It helps you add, update, and remove libraries easily without manual downloads.
💼 Career
Knowing Composer is essential for PHP developers to manage project libraries efficiently and keep projects up to date with the latest versions.
Progress0 / 4 steps
1
Initialize a new Composer project
Open your terminal and run composer init --no-interaction inside your project folder to create a composer.json file with default settings.
PHP
Need a hint?

Use the terminal command composer init --no-interaction to quickly create a composer.json file.

2
Add Guzzle HTTP client dependency
Run composer require guzzlehttp/guzzle in your terminal to add the Guzzle HTTP client library as a dependency to your project.
PHP
Need a hint?

Use composer require guzzlehttp/guzzle to add the Guzzle library.

3
Check installed dependencies
Open the composer.json file and verify that guzzlehttp/guzzle is listed under the require section. Also, check that the vendor folder exists in your project directory.
PHP
Need a hint?

Open composer.json in a text editor and look for the require section. The vendor folder should be created after installing dependencies.

4
Use Guzzle in a PHP script and output version
Create a PHP file named test_guzzle.php with code that loads Composer's autoloader and prints the Guzzle version using GuzzleHttp\Client::VERSION. Run the script and print the output.
PHP
Need a hint?

Use require 'vendor/autoload.php'; to load dependencies. Then print \GuzzleHttp\Client::VERSION to show the version.