Install composer

To begin with, you need to install the composer. If you have Windows and opsenServer, then it's already installed and ready to work, if not, install it as described here: Installing Composer on Windows. If you use Linux, then you are here: Install composer globally for Linux/Unix/OSX

Configuring autoloading packages for the Yii Framework

After or before you download the package(s) you need for your project, you need to ensure that the package(s) in the project are automatically downloaded. Since the composer will add all the packages to the /vendor folder, you must enable this folder to autoload. To do this, you need to make some changes to the Yii Framework code, namely, to add code:

require __DIR__ . '/vendor/autoload.php';

The following files: index.php, protected/yiic.php, protected/tests/bootstrap.php.

There is also an alternative method for connecting autoloading, at the beginning of the file protected/config/main.php add:

require_once( dirname(__FILE__) . '/../../vendor/autoload.php');

Hide files and folders of the composer from superfluous eyes

Add the following code to the root .htaccess to deny access to /vendor, composer.phar, composer.json composer.lock and redirect the request to index.php for this, add the following code (.htaccess):

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} vendor/ [OR]
RewriteCond %{REQUEST_FILENAME} composer.phar [OR]
RewriteCond %{REQUEST_FILENAME} composer.json [OR]
RewriteCond %{REQUEST_FILENAME} composer.lock
RewriteRule . index.php

Download the package with composer

To load the required package, execute the command (usually these commands are described in the package manual):

php ./composer.phar require packegeName

or

composer require packegeName

That all, enjoy the work of Yii1 and composer.

Example of loading a package through composer in Yii1 and working with it

Example #1

And so, take any package for tests, the first thing I got was Endroid QR Code (https://packagist.org/packages/endroid/qrcode). Open the console, go to the root of the project and run the installation command:

composer require endroid/qrcode

We wait until everything is downloaded, and then open any view or action and add the code from the instruction:

<?php

use Endroid\QrCode\ErrorCorrectionLevel;
use Endroid\QrCode\LabelAlignment;
use Endroid\QrCode\QrCode;
use Symfony\Component\HttpFoundation\Response;

$qrCode = new QrCode('Composer + Yii | Кодер.укр');
$qrCode->setSize(300);

$qrCode
    ->setWriterByName('png')
    ->setMargin(10)
    ->setEncoding('UTF-8')
    ->setErrorCorrectionLevel(ErrorCorrectionLevel::LOW)
    ->setValidateResult(false);

header('Content-Type: '.$qrCode->getContentType());
echo $qrCode->writeString();

$qrCode->writeFile(__DIR__.'/qrcode.png');

$response = new Response($qrCode->writeString(), Response::HTTP_OK, ['Content-Type' => $qrCode->getContentType()]);

And get a working extension in our project on the Yii Framework.

Example #2

For the purity of the experiment, take another (second available) package (https://github.com/dompdf/dompdf) and install it:

composer require dompdf/dompdf

And make work it like the previous one. We open any view (view) or action (action), we add the code from the instruction:

<?php

use Dompdf\Dompdf;

$dompdf = new Dompdf();
$dompdf->loadHtml('Composer + Yii | Кодер.укр');
$dompdf->setPaper('A4', 'landscape');
$dompdf->render();
$dompdf->stream();

Run and enjoy)

Original article: https://кодер.укр/записи/добавляем-composer-к-приложению-на-yii