Getting Started with Composer and Joomla!
If you’ve been listening to the various Joomla discussions about the Framework then you’re somewhat aware of what the Joomla! Framework is. If not, then this article will introduce you to the concept and then build on those concepts to provide some very specific examples.
The Joomla Framework is a collection of files that a developer / coder can use to build a web-based application for accomplishing a specific purpose or project. Other examples of frameworks include Laravel, Symfony, Yii, Codeigniter, CakePHP, and many more. These frameworks provide a starting point and a general set of classes and functions to hopefully speed up development time. You may hear these tools referred to as Rapid Application Development tools.
The bottom line: Building a custom application from scratch can be accomplished faster by using one of these frameworks.
When building a web-based application or even a website there are a few tools you should be familiar with and be able to use. The first tool we’re going to look at is called Composer. Composer is a “Dependency Manager for PHP”. Did that make sense? Don’t worry if that definition feels confusing. It’s actually quite simple. In order to understand Composer let’s compare it to something that might make more sense. I recently wanted to get something special for my wife. I went to my local florist and told her I wanted a floral bouquet that she would love. I know my wife likes the warm colors, yellow, orange, dark red; so I asked the florist to put a bouquet together that incorporated those colors. I didn’t go in the back and pick out the flowers myself, I didn’t even specify the exact flower type I wanted. I left all those details up to the florist.
Composer works much the same way. I “tell” Composer what I want in my project (e.g. Bootstrap, jQuery) and then let Composer go in the back and get each of those packages for my project. I don’t have to get them myself, I don’t even have to tell Composer what version to get. (Although if I wanted I could request a specific version). I hope that makes more sense of Composer. And just like with the florist, I can ask Composer to go back and do it again. Composer will go and download the packages all again, updating, replacing, adding more packages at any point in time. That’s pretty helpful.
You can at any point in time update any or all of your packages direct from the original creator without having to do anything other than tell Composer to do it. Due to the fairly good documentation for installing Composer found on their website I will simply link to it from this article.
If you have questions leave them in the comments and I’ll do my best to answer them. Go install it now. It won’t take long and you can install it easily on Linux,Windows, or Mac.
Note: Stop when you get to the “Using Composer” section and return here instead.
The terminal or command line can be a powerful tool you use and you don’t have to be scared of using it. If you went through the Composer setup (you did do that right?) then you’ve used your command line briefly to setup Composer. The commands we’ll use are quite basic.
composer install This is the command to run initially on a project.
composer update This is the command to run when updating a project.
That’s the only two we will work with right now. See? I told you it was easy.
Now you know what Composer is, you have a basic understanding of its functionality and you’ve explored a bit of the command line interface. The next step is to understand what makes up a Composer Package. Below are the required elements for a Composer package.
That’s right - a single file. This file is your request list for the “bouquet” you want Composer to put together for you. Here’s a snippet from a composer file:
"twig/twig" : "1.*",
As you can see we only have to tell Composer what packages we want and Composer does all the rest. Here’s how it works. Installing Open up terminal, or your preferred command line tool and navigate to the folder where you have this composer.json file.
Reminder: you’ll want to use a command like “cd /users/david/websites/sample”
Once you are in the folder with the composer.json file you only need to tell Composer to go to work. Type:
This command will tell Composer to take the composer.json file in this folder and get to work. It’s that easy. Sit back and wait as this can take a little time depending on how many things you’re asking for. In your terminal window you’ll see the things Composer is doing for you, accessing repositories, downloading files, and placing them in the right folders for you. This makes your life easy as a developer!
So by now you’ve probably figured this part out. The Joomla Framework is a package available through Composer. If you want to use the Joomla Framework in your project, you add a single line to your composer.json file and Composer will download and setup the files for your use.
I believe the best way to understand how to use the Joomla Framework and Composer is through a real-life example. I’ve created a sample app to use for the remainder of this article and follow-up articles if people are interested in more detail (Comment if you want me to continue this series!)
Sample Composer File
Download the entire repository and unzip it to a folder within your local web root (or on your server) . You’ll find an App folder, a www folder, and a composer.json file. The others are just a few random files you don’t need to worry about. The composer.json file is now quite familiar to you. Open this file and you’ll see the various packages that will be installed when you run Composer here. Go ahead and open terminal, navigate to this folder and run the following command.
Wait while Composer takes care of your order for you and then refresh the project folder. You’ll see some new directories created. Specifically a vendor directory where all the packages have been downloaded. Open a web browser and navigate to the www folder. The site should load up and you’re ready to begin Joomla Framework app development!
That’s the end of the basics of this article regarding Composer and Joomla and getting started with the Joomla Framework. But this could be the beginning of an entire series regarding basic app development. You, the reader, must decide. Leave a comment below if you would like me to explain and write an article or two regarding this sample app, the structure, the basics, and how to continue your app development experience.