Using Composer

Cotonti Siena 0.9.23 features Composer support. Composer is a tool for dependency management in PHP.

#1. What is Composer

Composer is a dependency manager designed to simplify uploading and using 3rd party PHP libraries in your project. In instance, it can be used to add all necessary libs into an extension that is being developed.

composer.json is a text file where all side libs (packages) the project is based on are defined. Additionally it contains data on project info, PHP minimum version and required PHP extensions.

composer.lock contains a current list of all installed dependencies and their versions. The main purpose of this file is to save the complete environment in which the project is being developed and tested.

For example, if you work in a team you have to make sure that your colleague having downloaded (pull) the project from a git repository will receive the same environment and package versions that you use.

When a project is deployed to the live environment you have to make sure that it uses the same versions as in the development environment. This ensures that any location your project is deployed in is identical to the one used in the development and helps eliminate errors that might occur due to the versions updates.

#2. Using Composer to Develop Extensions

First off, make sure that Composer is installed.

Next we will add the Guzzle client to a Cotonti extension. This ensures that both sync and async requests are sent to remote servers using the same interface. A great tool for integration with various APIs.

We will also use Flysystem to upload files into a remote storage.

These two are used only as an example with no connection between them.

Let’s begin.

We follow Guzzle installation manual to add section require into the file composer.json located in the project root:

"guzzlehttp/guzzle": "^7.8"

This would result in something like this:

"require": {
    "php": ">=5.6",
    "ext-gd": "*",
    "ext-mbstring": "*",
    "ext-json": "*",
    "ext-hash": "*",
    "ext-pdo": "*",
    "guzzlehttp/guzzle": "^7.8"

Next we execute the following code in the command line:

> composer update

This command updates all dependencies installed within the project to the latest version available (as per composer.json), installs new dependencies added to composer.json and removes those that no longer exist. Following that Composer updates composer.lock.

Now our HTTP client is ready for use.

Next we add the following code to our extension:


use GuzzleHttp\Client;

// Create client instance with base URI
$client = new Client(['base_uri' => '']);

// Send POST-request application/x-www-form-urlencoded to
$response = $client->request('POST', '/test', [
  'form_params' => [
      'field_name' => 'abc',
      'other_field' => '123',
      'nested_field' => [
          'nested' => 'hello',

$body = $response->getBody();
echo $body;

Dead easy, is it?

Now we proceed to remote uploads.

As specified in the documentation we add section require to composer.json:

"league/flysystem": "^3.0",
"league/flysystem-aws-s3-v3": "^3.0"

Over again:

> composer update

Everything set. We can now use the library in our extension.


use Aws\S3\S3Client;
use League\Flysystem\AwsS3V3\AwsS3V3Adapter;
use League\Flysystem\Filesystem;

// For some reason AWS adapter works this way only

/** @var S3ClientInterface $client */
$client = new S3Client([
    'version' => 'latest',
    'endpoint' => '',
    'region' => 'ru-central1',

// The internal adapter
$adapter = new AwsS3V3Adapter(
    $client, // S3Client
    'test-new-bucket', // Bucket name
    'path/to/upload/dir' // path prefix

// FilesystemOperator готов и сконфигурирован. Можно использовать.
$fileSystem = new Filesystem($adapter);

// Запись в файл
$filesystem->write($path, $contents);

That is all. Do not forget to specify in the extension installation manual instruction to add the following lines to composer.json and run composer update:

"guzzlehttp/guzzle": "^7.8",
"league/flysystem": "^3.0",
"league/flysystem-aws-s3-v3": "^3.0"

#3. Deploy Project into the Live Server

Our project is ready and is git-managed. Time to move it to the production server.

In the lib/vendor folder we got new subfolders with dependencies added by Composer. Let’s add them into the .gitignore – no need to use them in the repository and increase its size.

Now commit the changes and push them into the repository.

Next we login into the server via SSH and execute:

> git pull

Followed by:

> composer install

Unlike composer update this command shall install all dependencies listed in composer.lock and only the versions specified there. If this file is missing in the project root, this command shall act similar to composer update and use composer.json to install dependencies.

We’re done. You can now open the project in the browser and enjoy the result.

No comments yet
Only registered users can post new comments