5 valuable packages helpful for every PHP developer

5 valuable packages helpful for every PHP developer

Most of today’s web applications deliver a rich experience to their users. Website owners or business managers need more and more features integrated into their websites within a short period. Inevitably, it becomes a challenge for web developers to satisfy the expectations of the site owners if they had to develop everything from the beginning. That is where the packages come into action.

Being the most popular web programming language, PHP has many such packages available freely for anyone to use for their web projects. Composer is a tool that helps to find and install them in your PHP project. Composer uses Packagist, which is a directory of publically available PHP packages. GitHub hosts most of the packages you find on Packagist.

I’m trying to list a few of the most commonly required packages available on GitHub for you. Perhaps it will save a few valuable hours of your development time. I avoid PHP frameworks and focus on few libraries you can use with any framework.

1. Swift Mailer

Sending emails is a common feature of many web applications. They send emails for registration verifications, action notifications, newsletters, password recovery, and many more. Swift Mailer helps you streamline your email sending process.

Installation

The recommended way to install Swiftmailer is via Composer:

$ composer require "swiftmailer/swiftmailer:^6.0"

Basic Usage

// Require composer autoload
require_once '/path/to/vendor/autoload.php';

// Create the Transport
$transport = (new Swift_SmtpTransport('smtp.example.org', 25))
  ->setUsername('SMTP_USERNAME')
  ->setPassword('SMTP_PASSWORD');

// Create the Mailer using your created Transport
$mailer = new Swift_Mailer($transport);

// Create a message
$message = (new Swift_Message('Greetings from NTERMS'))
  ->setFrom(['info@nterms.com' => 'NTERMS'])
  ->setTo(['receiver@example.com', 'other@domain.org' => 'John Doe'])
  ->setBody('Hello there, Welcome to NTERMS!');

// Send the message
$result = $mailer->send($message);

We are using SMTP Transport to send the email in the example. SwiftMailer supports both SMTP and SendMail email transport methods. Also, it comes with LoadBalanced and Failover transport methods to cover advanced email sending scenarios. Read the official guide to learn more.

SwiftMailer comes as the default mail library in some famous PHP frameworks. Also, it has wrappers for easy integration with many others. Thought you can’t

The good side is using it for your project is straightforward. You can install and use its classes directly in your code.

2. Carbon

One of the most challenging tasks for me as a web developer is dealing with dates and timestamps. Almost all web applications are making use of data and time values. PHP has its DateTime class for supporting this. But, it comes with a limited set of features. Carbon is there to solve this by extending the functionality of the DateTime class. It comes with a rich set of features such as parsing time strings in many formats, a vast set of functions for time calculations, convert time values to display in different formats, and performing comparisons between times.

Installation

The recommended way to install Carbon is via Composer:

$ composer require nesbot/carbon

Basic Usage

// Require composer autoload
require_once '/path/to/vendor/autoload.php';

use Carbon\Carbon;

// show the time at the moment
printf("Now: %s", Carbon::now());

// Miltiple ways to create a date time object
$date = new Carbon('first day of January 2008');
$date = Carbon::parse('2018-03-16')
$datetime = Carbon::create(1975, 12, 25, 14, 15, 16);

// Display in different formats
echo $datetime->toDateString();           // 1975-12-25
echo $datetime->toFormattedDateString();  // Dec 25, 1975
echo $datetime->toTimeString();           // 14:15:16
echo $datetime->toDateTimeString();       // 1975-12-25 14:15:16
echo $datetime->toDayDateTimeString();    // Thu, Dec 25, 1975 2:15 PM

Its concise documentation is simple enough for anyone to understand what does it do and how.

3. mPDF

Most of the time, PHP code generates HTML as the output. But there are cases you need to generate PDF as well. Web applications allow their users to download copies of the invoices and reports as PDF files. mPDF could be the best PHP library out there to assist you in this.

mPDF comes with a comprehensive set of functions to help you with everything you need to do with your PDF documents. The most significant feature among all is converting HTML+CSS documents to PDF. Often you can use the same HTML & CSS to show your documents on screen and to generate the PDF version of them. mPDF also supports generating table of contents, setting different page sizes, layouts, page orientation, page breaks, using true type fonts, and embedding images.

Installation

The recommended way to install mPDF is via Composer:

$ composer require mpdf/mpdf

Basic Usage

// Require composer autoload
require_once '/path/to/vendor/autoload.php';

use Mpdf\Mpdf;

// Create an instance of the class:
$mpdf = new Mpdf();

// Write some HTML code:
$mpdf->WriteHTML('<h1>Hello world!</h1>');

// Output a PDF file directly to the browser
$mpdf->Output();

You have many output options such as viewing the generated PDF documents in the web browser itself, download or save into a file on the server.

Please read the official documentation of mPDF to find out more.

4. Intervention Image

Today’s web applications can’t forget images. They need to work with images at least to let their users have a profile picture. Not all image files uploaded by the users are in the format the web application need. They need to be resized, cropped, optimized, or enhanced according to the application requirements. That is where Intervention Image becomes your best ally.

Intervention Image supports creating new images as well as working with images loaded from the filesystem. It offers many image operations such as resizing, cropping, adjusting image properties (brightness, contrast, gamma), drawing, and applying filters.

Installation

The recommended way to install Intervention Image is via Composer:

$ composer require intervention/image

Basic Usage

// Require composer autoload
require_once '/path/to/vendor/autoload.php';

// Import the Intervention Image Manager Class
use Intervention\Image\ImageManager;

// Create an image manager instance with favored driver
$manager = new ImageManager(array('driver' => 'imagick'));

// Load an image from disk and resize it
$image = $manager->make('public/foo.jpg')->resize(300, 200)->save('bar.jpg');

You can create Image objects and chain functions to apply multiple operations. It also utilizes a plugin (intervention/imagecache) to support image caching that improves performance by storing processed images. If you are developing a Laravel application, you can use URL to manipulate images dynamically while caching the result for better performance.

5. PHP Debug Bar

Last but not least, this package serves you, not your application users. One thing you can’t forget while programming is debugging your code. Often programmers need to check the current application state to understand how things are going on with their code. PHP developers use var_dump, die, print_r or echo functions to write the value of variables to the output to achieve this. You can use PHP Debugbar to get a snapshot of the application state to make the debugging process easier and faster.

Installation

The recommended way to install PHP Debug Bar is via Composer:

$ composer require maximebf/debugbar

Basic Usage

<?php
// Require composer autoload
require_once '/path/to/vendor/autoload.php';

use DebugBar\StandardDebugBar;

$debugbar = new StandardDebugBar();
$debugbarRenderer = $debugbar->getJavascriptRenderer();

$debugbar["messages"]->addMessage("hello world!");
?>
<html>
<head>
    <?php echo $debugbarRenderer->renderHead() ?>
</head>
<body>
    ...
    <?php echo $debugbarRenderer->render() ?>
</body>
</html>

The DebugBar has two components. The main DebugBar object collects data, and the renderer object renders them into HTML and JavaScript to show them in a compact panel similar to browser developer tools. It comes with a set of base collectors and allows you created your own collectors. Among the base collectors, message collector logs user messages while request and database collectors monitor the HTTP request and database connections respectively.

PHP Debug Bar - Dabase Panel

Please read the official documentation of PHP Debug Bar to learn more.

Cover image created by pch.vector - www.freepik.com

Saranga
Saranga A web developer and highly passionate about research and development of web technologies around PHP, HTML, JavaScript and CSS.
comments powered by Disqus