Many sites/ projects, in order not to reinvent the wheel, are more convenient to do on ready-made php – CMS platforms (WordPress, Bitrix from 1C, Joomla, OpenCart, ImageCMS, etc.). Well, or, to give more flavor and a certain independence, on one of the frameworks (Laravel, Symfony, CodeIgniter and etc.).
Of course, we are talking about a development environment using the server-side programming language PHP.
The main advantage in using the most popular CMS is the possibility of expanding their functionality by developing their own scripts, connecting third-party libraries with their integration into the file environment of the engine itself.
In other words, we developed our own script/program in PHP, or took a string library (from other developers) and integrated it into the body of our engine (CMS) to interact with the functionality of your project and return the result of its work to the CMS page template.
In this post, we will talk exactly about how to connect a third-party library developed in PHP (Packagist standard) to your CMS project using the popular PHP dependency manager COMPOSER.
To understand how you can customize all this interaction, taking into account the specific needs of the developer in solving a specific task, let's give a simple example.
Since almost all popular CMS are based on OOP (object-oriented programming) approaches and their file structure is similar, the general approaches in implementing this task for all CMS (WordPress, Bitrix, Joomla, Drupal, etc.) will be similar.
We have a website based on the ImageCMS engine (CodeIgniter framework) and we need to implement such a simple task.
On our site, using the functionality of the engine (admin panel), we will create a regular page on which, having previously created additional fields for uploading two images (through the entire admin panel), we will output them in png format, and based on them we will generate one image in the gif format (in two variations).
The creation of the page and the output of the source images on it will be achieved using the standard administrative procedure for managing the engine(CMS), but a third-party library will help us in creating gif images (animations) on the same page.
In this example, we will connect the necessary library in two ways, with the output of the result of each of them in two areas of the page:
- 1 using the filefunctions.php , located at the root of the theme of the engine (used to expand its functionality)
- 2 –lib_gif library created by us in the body of the framework itself(codeigniter) engine(ImageCMS) (in the application/ libraries folder)
It remains to find the necessary library in PHP to solve the task.
There is such a wonderful resource https://packagist.org , where developers from all over the world place and share their software scripts (libraries), thereby facilitating the development process for other programmers.
Packagist is a repository of Composer packages. It allows you to find packages and tells Composer where to get the code from. Any developer can use Composer to manage their project or library dependencies.
Composer, in turn, is a popular PHP dependency manager that simplifies the process of installing and updating project dependencies (some analogue of NodeJS NPM).
He facilitates the installation, via the command line of his terminal/console, of all necessary versions of packages in accordance with the request of the developer.Among other things, Composer is often used to install new projects built on such popular PHP frameworks as Laravel and Symfony on its local server.
And so, we go to the site https://packagist.org and we are looking for something similar to solve our problem creating gif animations.
We find the library we need - pomirleanu/gif-create
We familiarize ourselves with the instructions for its installation and use.
Before we start installing pomirleanu/gif-create, we need to install it on our system first (in this case Windows) compser, in order to ensure that this library is loaded and connected to our project via the command line.
Since we have a local development environment on the Oep Server (OS), composer is already installed in the system by default along with the OS.
The only thing we will do is add for our convenience and the ability to work with it not only through the OS console, but also Visual Studio Code, which is our code editor (IDE - integrated development environment). Or, in other words, make friends with the VS Code console and OpenServer.
We do the following in the OS settings:
If your development environment is not on OS (but, for example, on XAMPP, WAMP, or separately plug-in PHP, etc.), then composer must be installed globally. Just download and run installer for Windows.
To check the installed version of composer in the terminal command line, enter:
composer –v
and we will get all the service information on it
Next, we will create a folder app_packagist (any name can be set) in the root of the project, into which we will download the necessary library using composer.
Go through the VS Code console to this folder and run the following command:
composer require pomirleanu/gif-create
After that, the app_packagist will be created in the folder:
- the composer.json file, where the plug-in package (name/version) is specified.
- the composer.lock file, where all installed dependencies and their versions are specified.
- and the vendor folder with the downloaded main and dependent packages (libraries)
If we had originally created a file composer.json in the app_packagist folder and specified a construction in it as an object require:
and after that we made a request:
composer install
then we got the same result.
In fact, the same as with the composer require pomirleanu/gif-create request, only with the first option, the composer.json file is created automatically if it was not in the folder before.
In the vendor folder, we see all the basic and auxiliary packages needed to create gif animations from png images.
Creating a page (my_page) of your site using the CMS admin panel:
We attach the output template of this page to the created file my_page.tpl, where we output our original images (/uploads/1.png and /uploads/2.png)
Let's see what we've got.
Below, on the same page, we will display the result of the connected library pomirleanu/gif-create.
To do this, in the file of the created template (my_page.tpl) of our page, we will write the following code, which will be responsible for the output of animation in two variants.
1. using the created gif function in the file functions.php :
{site_url(gif($image_1,$image_2))} – url of the first animation
2 using the created lib_gif library (in the application/ libraries folder):
{$url_animated = $CI->load->library('lib_gif')->index($image_1,$image_2)}
{site_url($url_animated)} - url of the second animation
In fact, the code is identical inside the index() method.
The difference between the first animation and the second is that the array $frame, which is the first argument of the method being called (->create($frames, $durations)) of the class object (new GifCreate\GifCreate()) in the 1st method includes the first element url = /uploads/1/png, and in the 2nd second url = /uploads/2.png.
Therefore, the alternation of the original images in the generated animation in the 1st and 2nd versions will be different for us.
This is how you can configure the interaction of the CMS with any external library installed using composer, as part of your project for the synthesis, processing and output of certain data, including on pages created directly by the engine itself (CMS).
By understanding how to use one or another library from the Composer package repository in your projects, you can significantly simplify the development process itself, especially since today most of all sites /resources, after all, are developed on ready-made php platforms (CMS).
How to connect a third-party PHP library using COMPOSER to an engine-based website (CMS) - Something like that!