Programming

  minte9
LearnRemember



Modern Php / Composer


COMPOSER

Composer is a tool for dependency management in PHP. You declare the libraries your project depends on and it will install (update) them for you.
 
sudo apt update
sudo apt install wget php-cli php-zip unzip curl
curl -sS getcomposer.org/installer |php
To use Composer a project simply need a composer.json file in its root directory. This file defines the dependencies of the project.
  copy
{
    "require": {
        "nesbot/carbon": "2.12.*"
    }
}

REQUIRE

We can edit composer.json manually, or we can use commands (require/update).
 
mkdir /var/www/html/composer-test
cd /var/www/html/composer-test

composer require nesbot/carbon

   # Failed to open stream: Permission denied
   # Cannot create cache directory /home/catalin/.composer/cache/
   # Avoid sudo when running composer.

sudo chown -R myuser.myuser composer-test
sudo chown -R catalin.catalin /home/catalin/.composer/
  copy
require __DIR__ . '/vendor/autoload.php';
use Carbon\Carbon;

printf("Now: %s", Carbon::now()); // Now: 2020-05-08 10:34:49
printf("Lastweek: %s", Carbon::now()->subWeek());

UPDATE

Composer will update the package if newer version found. Also, version constraint match with the one specified in the composer.json.
 
composer outdated
composer clearcache
composer update

SELF-UPDATE

 
sudo composer self-update
composer -v





References