Lesson 6 of 8

Composer & the Ecosystem

Learn to use Composer, PHP's and autoloader, and get an overview of the main frameworks. Almost every modern PHP project uses Composer.

Bash
# Install Composer: https://getcomposer.org

composer init                    # start a project interactively
composer require guzzlehttp/guzzle   # add a package
composer require --dev phpunit/phpunit

# Installs into vendor/ and writes composer.json + composer.lock
# (commit both json and lock; ignore vendor/)
PHP
<?php
// One require gives you every installed package + your own classes:
require __DIR__ . '/vendor/autoload.php';

use GuzzleHttp\Client;

$client = new Client();
$response = $client->get('https://api.github.com/users/octocat');
$data = json_decode($response->getBody()->getContents(), true);
echo $data['name'];

The most popular framework is Laravel, a full-featured web framework with an ORM, routing, , auth and well-regarded documentation. Symfony is the other major framework; it is more modular and many tools build on it. WordPress is its own ecosystem: older-style PHP, but very common in freelance web work.

Bash
# Try Laravel:
composer global require laravel/installer
laravel new blog
cd blog
php artisan serve      # dev server at localhost:8000

# Useful quality tools:
composer require --dev phpstan/phpstan   # static analysis
./vendor/bin/phpstan analyse src --level=8
Finished reading?Mark it done to track your progress.

Tip: use the ← and → keys to move between lessons.