Composer packages are a great way of making your code available to everyone.
It my be a daunting thought to create your first package, but if you break it down into simple steps, it becomes much less scary! Let's see:
- Create your composer file
- Register your namespaces and autoloaders
- Write your code đĽł
- Push your code to your repository
- Create a tag or a version
- Submit your package to Packagist (the repo of composer packages)
- Done!
Your composer file
Naturally, to do this, you should have composer installed.
To check if you have composer, go ahead and run the below in your command line:
composer
If you have Composer installed, you'll see:
______
/ ____/___ ____ ___ ____ ____ ________ _____
/ / / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__ ) __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
/_/
Composer version 2.0.4 2020-10-30 22:39:11
...
If you do not, you'll need to install Composer.
Now that we've got composer, we can create our Composer file.
To get the ball rolling, just run:
composer init
This will begin the interactive Composer file creation.
First you'll see:
Package name (<vendor>/<name>) [pc/composer]:
This will be the name of your package Few things to note:
- Vendor is the author of the package
- Name is the... name of the package.
Enter your name, for example I will say joem/tutorial
.
Next you'll be asked for the description of your package.
After that, you'll be asked for the author.
You'll then be asked for the min-stability, you can skip this one, as well as Package Type
and License
.
You'll see:
Would you like to define your dependencies (require) interactively [yes]?
Go ahead and enter n
.
Next:
Would you like to define your dev dependencies (require-dev) interactively [yes]?
Again, a cheeky n
.
Do you confirm generation [yes]?
yes
!
Fantastic, we've created our Composer file.
Project structure and namespaces
Typically, you'll see this structure (or similar):
project-name (root)
âââ composer.json
âââ composer-lock.json
âââ src
â âââ ClassName.php
â âââ helpers.php
âââ tests
â âââ ClassNameTest.php
Note: ClassName.php
is the name of your class!
With this in mind, we can create our autoloaders to load our files. Inside of composer.json
, add:
"autoload": {
"psr-4": {
"Joem\\App\\": "src"
},
"files": [
"src/helpers.php"
]
}
inside of your object below require
. Where Joem
is the name of your project. Here we are autoloading all classes inside of src
, with the namespace of Joem\App
, and loading our helper file.
Now, let's create our class inside of src
.
Here is the example class I am using:
<?php
namespace Joem\App;
class ClassName {
}
Notice the namespace Joem\App;
, this means it will be autoloaded into our vendor.
To generate the autoloader, go head and fire:
composer dump-autoload
into your console.
This will generate an autoloader containing our class and helper file.
We can now include our autoloader into our files.
If we make a index.php and add:
<?php
require_once 'vendor/autoload.php';
$class = new \Joem\App\ClassName();
We'll see our autoloader is working!
Fantastic 𼳠From here, you can add all the code you would like!
Git repo
Once your class(es) are ready, we need to attach the code to a git repo, in this case, a Github repo.
Once you have created you repository and added it to your codebase, you'll need a .gitignore
in your project root, containing vendor/
. This will prevent your repository from being filled with Composer dependency gunk.
Your repo is important, since Packagist will use it to fetch your code.
You'll need to create a release for your main branch, you can version it as v1.0.0
, this will prevent min-stability errors when we try to install it later!
Submitting the package
We will use Packagist to submit our package to the Composer repository.
Head to packagist and create your account.
Once your account is created, click the "submit" button. You will be prompted to enter the URL of your Github repository. Once submitted, your package will be available as a Composer package!
⨠A M A Z I N G â¨
Now, you can install your package through composer with the command
composer require joem/tutorial
Where joem/tutorial
is the name of your package, which we inputted when we created the composer file.
A note on testing
In our structure, you'll see we added a tests
directory, it is encouraged that your package contains tests, these can be created with a tool such as PHPUnit.
Top comments (3)
Thanks for this article. I followed your steps, but also needed to apply Git-Flow and create/push a release before the package was considered "steady" for installation by composer.
After that, all is fine. Thanks again!
How were you able to implement that
Git-Flow isn't even necessary.
You can do it by creating a release, labelling it with the version.
Then save / push that release.
Then, you have a version to use for you application that is steady instead of dev.