Composer Dependencies

Composer Dependencies in Extension

The Composer as a package or dependency manager plays a vital role in helping us manage some of these dependencies which our application requires for optimal performance by saving them on a folder, most times the “vendor” folder.

How to setup composer in an extension

To install Composer in your project, you need an important file called “composer.json”. This file is where you define all the dependencies/packages needed in your project. Under your extension folder create this file “composer.json”.

The “require” key

This is a set of JSON definitions used to tell the composer what packages your extension will depend on. There may be other variables you may need to define in this composer.json file, but most of the time, this “require” key is often the only content of the composer.json file.

{
    "require": {
         "dompdf/dompdf": "^1.1"
    }
}

In the above code create the “composer.json” file inside your extension. Then inside the composer.json file, goes my “require” key.

Folder

  extension->
       |->minical-extension-boilerplate
                  |->composer.json
                  |->vendor  

Package Names

The package names consist of a vendor name and a project name. In most cases these names are identical. This naming format plays a vital role in preventing naming conflicts, as two or more persons can have a package with the same name but a different vendor name.

Version Specification

As seen in the above code snippet, we specified, telling Composer to download only the version of dompdf in the version range of 7.77 the composer will download the latest stable version, within the specified version range.

Installing Dependencies

To install these defined dependencies to our extension, we must run the composer “install” command from the terminal in your extension directory. After this command, you will find a vendor folder under the extension, make sure you will upload the folder while creating PR for the miniCal extension.

C:\xampp\htdocs\minical-extension-boilerplate> composer install

Updating Packages

We can also update some of these installed packages in our extension to their latest version, but this must be done with care to prevent our program from breaking. To update packages, we use the composer update command.

C:\xampp\htdocs\minical-extension-boilerplate> composer update

The above code snippet will update the whole packages, installed in our extension. To update only a single package, we need to specify the name of the package to update as shown below.

C:\xampp\htdocs\minical-extension-boilerplate>  composer update dompdf/dompdf

How to Use composer dependencies in an extension

In the vendor folder, you will find a autoload file you can load this file anywhere you want.

  extension->
       |->minical-extension-boilerplate
                  |->composer.json
                  |->vendor  
                         |->autoload.php

here is the syntax

require_once FCPATH . 'application/extensions/minical-extension-boilerplate/vendor/autoload.php';

Last updated