As a developer, it is a necessity to use a front-end framework to manage your CSS and JavaScript when building pages in a website.
However, an obstacle you may encounter when doing so is the repetitiveness and time-consuming process involved in updating the framework to its latest version and rolling back to their previous versions when something gets broken.
This becomes a big problem when you are managing multiple websites that rely on dependencies to deliver JavaScript and CSS unto a page.
This is the reason why Composer exists.
This dependency management tool allows you to seamlessly retrieve the aforementioned dependencies from your front-end framework. Using this tool also lets you install libraries and packages in an organized manner so you can track and sort out the different versions, making it easier for you to detect conflicts.
If you are looking to a more efficient way to manage the dependencies on all the sites you’re working on, then keep reading to learn how you can reap the benefits of using Composer.
Installing Composer
After downloading the files from the website, there are different ways on how you can proceed with this. The first is by installing Composer locally (the installation process is different for Mac/Linux and Windows). For the former, you have the option to install the files globally so you can access it anywhere. Here is a more detailed post at A Beautiful Site on how to install Composer globally.
Using Composer
There are two elements that you need to take note when using Composer. Aside from the Composer itself, which runs the command for managing dependencies, the other element is the Packagist, a repository for the main packages you wish to use. Visit the site to download the dependencies you need for installing.
The dependency management will be stored in a single file named composer.json
in the root directory. In this file, you can edit the dependencies as you see fit or by referring to the require
section of Composer, which will detail the dependencies used is a specific project.
To install the dependencies into the file, you must enter composer install
to execute the command.
While you can start tweaking and configuring the JSON file by referring further to the guide featured in Composer’s website, there are other things you need to learn about this dependency management tool to unlock its true powers:
- Identify versions – Setting the parameters for the versions of the dependencies compatible with your project allows you to update the packages without having to edit the JSON file every time.
- Lock the file – Using the
composer.lock
command let you disable the use of certain versions of packages and components. This way, your team working in a single project will be forced to use the same version of the dependency to avoid conflict. If you wish to use the updated version of the dependency at all times, use thecomposer install
andcomposer update
commands along with locking. - Set up development requirements – When developing your project, you will need to use packages as part of your testing that won’t appear in the final product. To do this, place these packages into the
require-dev
array instead.
Resources on how to use Composer
To understand how to maximize the performance of this dependency manager to your projects, below are posts to will shed light to how Composer should be used.
Better Dependency Management In Team-Based WordPress Projects With Composer
by David Smith at Smashing Magazine
As mentioned earlier, the problem of developing multiple WordPress websites is when different developers are inserted into the projects. This prevents developers from knowing whether or not all of them are using the same core and version of the tools they’re using and working on.
To solve this problem, this post at Smashing Magazine offers a comprehensive guide on how to retrofit Composer to your different projects, among others.
Getting Started with Composer for PHP Dependency Management
Ervin Barta at Semaphore
A good introduction of Composer and how it is a better alternative to PEAR as a dependency management tool. The post also details the most common features of Composer and how you can use them in your projects.
Composer Best Practices – Jordi Boggiano #forumphp
Speaker and seasoned web developer Jordi Boggiano is fully aware of the steep learning curve developers experience when getting their hands on de-facto dependency manager Composer. In this video talk, she shows a hands-on presentation on some of the basis uses of this tool to expedite and streamline your approach to your PHP projects such as version constraints, semantic versioning, and others.
IMPROVING WORDPRESS DEVELOPMENT WORKFLOW WITH COMPOSER
Josh Pollack at Torque
Another introductory guide on how to use Composer for developing your WordPress themes and plugins using PHP.
Final thoughts: Even if you are used to managing your PHP projects by manually updating the versions of your packages, you have to also realize that there are more time-efficient and effective ways on how to handle your multiple web development projects. Hopefully, this post sheds light on Composer as the tool to make your job much easier.
More on dependency management:
27 PHP/MySQL Tutorials for Building Web Applications
7 PHP Development Techniques to Minimize Security Vulnerabilities
Author: Christopher Jan Benitez
Content marketer during the day. Heavy sleeper at night. Dreams of non-existent brass rings. Freelance writer for hire. Pro wrestling fan by choice (It’s still real to me, damnit!). Family man all the time.