Comprehensive Guide to Installing PHP Libraries Using Composer on cPanel Hosting Print

  • 0

Introduction

Composer is a powerful dependency manager for PHP, allowing developers to manage and install libraries and packages with ease. This guide provides a detailed walkthrough on how to install various PHP libraries using Composer on a cPanel hosting environment. For a list of pre-installed PHP libraries, refer to our Guide to Using Pre-Installed PHP Libraries on cPanel Hosting.

Prerequisites

  1. cPanel Access: Ensure you have access to your cPanel account.
  2. SSH Access: SSH access is necessary for installing and using Composer. If not already enabled, request SSH access from your hosting provider.
  3. PHP Version Compatibility: Ensure your PHP version is compatible with the libraries you intend to use.

Requesting SSH Access

SSH access is crucial for installing Composer and running various commands. Here’s how to request and activate SSH access in Domain India Web Hosting:

Contact Support:

  • Submit a ticket requesting SSH access. Include necessary details such as your domain name, the reason for needing SSH access, and any specific requirements you might have.

Connecting via SSH

  1. Using cPanel Terminal:

    • Some cPanel installations have a built-in Terminal feature. Navigate to the 'Terminal' icon under the 'Advanced' section in cPanel and launch the terminal.
  2. Using an SSH Client (e.g., PuTTY):

    • Download and install an SSH client like PuTTY.
    • Open PuTTY and enter your domain or server IP address.
    • Enter the SSH port (usually 22) and click 'Open'.
    • Log in using your SSH credentials provided by your hosting provider.

Installing Composer

 

1. Navigate to Your Project Directory:
- Use the `cd` command to navigate to your project's root directory. For example:
cd public_html/your_project_directory

 

2. Download Composer:
- Use the following command to download and install Composer:
curl -sS https://getcomposer.org/installer | php

 

3. Move Composer to a Global Location:
- Move the `composer.phar` file to make it globally accessible:
mv composer.phar /usr/local/bin/composer

 

Installing PHP Libraries Using Composer

 

With Composer installed, you can now install any PHP library required for your project. Below are examples of installing some commonly used libraries.

 

#1. DOMPDF

 

DOMPDF is a PHP library that converts HTML to PDF.

 

Installation Steps:
composer require dompdf/dompdf

 

Usage Example:
require 'vendor/autoload.php';
use Dompdf\Dompdf;

 

$dompdf = new Dompdf();
$dompdf->loadHtml('<h1>Hello World</h1>');
$dompdf->setPaper('A4', 'landscape');
$dompdf->render();
$dompdf->stream();

 

#2. TCPDF

 

TCPDF is a PHP library for generating PDF documents.

 

Installation Steps:
composer require tecnickcom/tcpdf

 

Usage Example:
require 'vendor/autoload.php';
$pdf = new \TCPDF();
$pdf->AddPage();
$pdf->writeHTML('<h1>Hello World</h1>');
$pdf->Output('example.pdf', 'I');

 

#3. MPDF

 

MPDF is another PHP library to convert HTML to PDF.

 

Installation Steps:
composer require mpdf/mpdf

 

Usage Example:
require 'vendor/autoload.php';
$mpdf = new \Mpdf\Mpdf();
$mpdf->WriteHTML('<h1>Hello World</h1>');
$mpdf->Output();

 

#4. PHPMailer

 

PHPMailer is a popular library for sending emails using PHP.

 

Installation Steps:
composer require phpmailer/phpmailer

 

Usage Example:
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

 

require 'vendor/autoload.php';

 

$mail = new PHPMailer(true);
try {
$mail->SMTPDebug = 2;
$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->Username = 'user@example.com';
$mail->Password = 'secret';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;

 

$mail->setFrom('from@example.com', 'Mailer');
$mail->addAddress('joe@example.net', 'Joe User');

 

$mail->isHTML(true);
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';

 

$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}

 

#5. Guzzle

 

Guzzle is a PHP HTTP client for making HTTP requests.

 

Installation Steps:
composer require guzzlehttp/guzzle

 

Usage Example:
require 'vendor/autoload.php';
use GuzzleHttp\Client;

 

$client = new Client();
$response = $client->request('GET', 'https://api.example.com');
echo $response->getBody();

 

#6. Carbon

 

Carbon is a PHP library for date and time manipulation.

 

Installation Steps:
composer require nesbot/carbon

 

Usage Example:
require 'vendor/autoload.php';
use Carbon\Carbon;

 

echo Carbon::now()->toDateTimeString();
Installing More PHP Libraries Using Composer

 

#7. Monolog

 

Monolog is a comprehensive logging library for PHP.

 

Installation Steps:
composer require monolog/monolog

 

Usage Example:
require 'vendor/autoload.php';
use Monolog\Logger;
use Monolog\Handler\StreamHandler;

 

// Create a log channel
$log = new Logger('name');
$log->pushHandler(new StreamHandler('path/to/your.log', Logger::WARNING));

 

// Add records to the log
$log->warning('Foo');
$log->error('Bar');

 

#8. Symfony Console

 

Symfony Console is a component for creating command-line applications.

 

Installation Steps:
composer require symfony/console

 

Usage Example:
require 'vendor/autoload.php';
use Symfony\Component\Console\Application;

 

$app = new Application();

 

// Define a new command
$app->register('greet')
->setDescription('Greet someone')
->addArgument('name', null, 'Who do you want to greet?')
->setCode(function ($input, $output) {
$name = $input->getArgument('name');
$output->writeln('Hello ' . $name);
});

 

$app->run();

 

#9. Whoops

 

Whoops is a PHP library for handling errors and exceptions with a user-friendly interface.

 

Installation Steps:
composer require filp/whoops

 

Usage Example:
require 'vendor/autoload.php';
use Whoops\Run;
use Whoops\Handler\PrettyPageHandler;

 

$whoops = new Run();
$whoops->pushHandler(new PrettyPageHandler());
$whoops->register();

 

// Example error
throw new RuntimeException("Something went wrong!");

 

#10. Faker

 

Faker is a PHP library for generating fake data, useful for testing and development.

 

Installation Steps:
composer require fzaninotto/faker

 

Usage Example:
require 'vendor/autoload.php';
$faker = Faker\Factory::create();

 

// Generate fake data
echo $faker->name;
echo $faker->address;
echo $faker->text;

 

#11. PHPUnit

 

PHPUnit is a framework for unit testing in PHP.

 

Installation Steps:
composer require --dev phpunit/phpunit

 

Usage Example:
use PHPUnit\Framework\TestCase;

 

class SampleTest extends TestCase
{
public function testTrueIsTrue()
{
$this->assertTrue(true);
}
}

 

#12. Predis

 

Predis is a flexible and feature-rich Redis client library for PHP.

 

Installation Steps:
composer require predis/predis

 

Usage Example:
require 'vendor/autoload.php';
$client = new Predis\Client();

 

// Set a value
$client->set('foo', 'bar');

 

// Get the value
$value = $client->get('foo');
echo $value; // Outputs: bar

 

#13. Intervention Image

 

Intervention Image is an image handling and manipulation library for PHP.

 

Installation Steps:
composer require intervention/image

 

Usage Example:
require 'vendor/autoload.php';
use Intervention\Image\ImageManagerStatic as Image;

 

// Open an image file
$img = Image::make('path/to/image.jpg');

 

// Resize the image to a width of 300 and constrain aspect ratio (auto height)
$img->resize(300, null, function ($constraint) {
$constraint->aspectRatio();
});

 

// Save the image
$img->save('path/to/resized_image.jpg');

 

#14. Slim Framework

 

Slim is a PHP micro-framework that helps you quickly write simple yet powerful web applications and APIs.

 

Installation Steps:
composer require slim/slim

 

Usage Example:
require 'vendor/autoload.php';
$app = new \Slim\App();

 

$app->get('/hello/{name}', function ($request, $response, $args) {
$name = $args['name'];
return $response->write("Hello, $name");
});

 

$app->run();

 

#15. Doctrine ORM

 

Doctrine ORM is a powerful PHP ORM (Object-Relational Mapper) for database management.

 

Installation Steps:
composer require doctrine/orm

 

Usage Example:
require 'vendor/autoload.php';
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;

 

$paths = array("/path/to/entity-files");
$isDevMode = true;

 

// Database configuration parameters
$dbParams = array(
'driver' => 'pdo_mysql',
'user' => 'dbuser',
'password' => 'dbpassword',
'dbname' => 'dbname',
);

 

$config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode);
$entityManager = EntityManager::create($dbParams, $config);

 

Conclusion

Composer simplifies the management of PHP libraries and dependencies, making it easier to install and maintain packages needed for your projects. By following this guide, you can efficiently install and utilize various PHP libraries on your cPanel hosting environment.

For a list of pre-installed PHP libraries, refer to our Guide to Using Pre-Installed PHP Libraries on cPanel Hosting.

For further assistance, you can refer to Domain India's Knowledgebase or submit a support ticket at Domain India's Support.


Was this answer helpful?

« Back