Как пользоваться composer и для чего он нужен?

Для начало нужно установить composer глобально, как приложение:

Далее заходим на packagist.org и ищем пакет, который нам нужен. Допустим, для генерации штрих-кодов.

Выбрали вот этот пакет: https://packagist.org/packages/tecnickcom/tc-lib-barcode

В терминале заходим в папку нашего проекта и устанавливаем пакет:

composer require tecnickcom/tc-lib-barcode

Должно вывести примерно следующее:

Using version ^1.16 for tecnickcom/tc-lib-barcode
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 2 installs, 0 updates, 0 removals
  - Installing tecnickcom/tc-lib-color (1.12.15): Downloading (100%)
  - Installing tecnickcom/tc-lib-barcode (1.16.1): Downloading (100%)
Writing lock file
Generating autoload files

В вашем проекте должна появиться папка vendor, в ней файл autoload.php и файлы пакетов.

Чтобы использовать наш пакет, нужно подключить файл autoload.php. Делаем это:

require_once 'vendor/autoload.php';

Эта строка подключит в наш проект не только файлы нашего пакета, но и всех других установленных или которые будут устанавливаться.

И тестируем пакет, как написано в документации. Весь файл для теста получился таким:

<?php
require_once 'vendor/autoload.php';

// instantiate the barcode class
$barcode = new \Com\Tecnick\Barcode\Barcode();

// generate a barcode
$bobj = $barcode->getBarcodeObj(
    'QRCODE,H',                     // barcode type and additional comma-separated parameters
    'https://tecnick.com',          // data string to encode
    -4,                             // bar width (use absolute or negative value as multiplication factor)
    -4,                             // bar height (use absolute or negative value as multiplication factor)
    'black',                        // foreground color
    array(-2, -2, -2, -2)           // padding (use absolute or negative values as multiplication factors)
)->setBackgroundColor('white'); // background color

// output the barcode as HTML div (see other output formats in the documentation and examples)
echo $bobj->getHtmlDiv();

Скрипт вывел нам такой qrcode:

Обновление всех пакетов

Можно обновить все пакеты командой:

composer update

0 0 голоса
Рейтинг статьи
Подписаться
Уведомить о
guest

0 комментариев
Межтекстовые Отзывы
Посмотреть все комментарии