
Good content takes time and effort to come up with.
Please consider supporting us by just disabling your AD BLOCKER and reloading this page again.
Hello friends in this article, you will learn a simple way to create a composer package & add it to packagist. Let's built RSS Generating package as an example. Here I wont be explaining a whole project code, will add some partial code which will give you good understanding.
This article covers how to
Basic knowledge on composer & write PHP scripts, in case if your new to composer or would like to know in depth knowledge this please refer this article What Is Composer? How Does It Work? Useful Composer Commands And Usage.
First create a project folder in www, html or htdocs folder when you run your basic php projects.
For our example I have created rss
inside htdocs
folder.
Basically my folder structure is as follows
You project must have the composer.json
file in the root of your project folder as shown in the above image and will have the following content
{
"name": "channaveer/rss",
"description": "Simple RSS 2.0 Generator",
"keywords": ["rss", "rss2.0", "simple-rss", "rss-generator"],
"type": "library",
"license": "MIT",
"authors": [
{
"name": "Channaveer Hakari",
"email": "channaveer888@gmail.com",
"homepage": "https://stackcoder.in/about-me",
"role": "Developer"
}
],
"require": {
"php": ">=5.6.0"
},
"autoload": {
"psr-4": {
"Channaveer\\RSS\\": "src/"
}
},
"extra": {
"branch-alias": {
"dev-master": "1.0-dev"
}
},
"minimum-stability": "dev",
"config": {
"sort-packages": true
}
}
name
- Package name of your project, make sure that this must be unique
description
- Description of your project
keywords
- Keywords your project needs to be searched for
type
- Mine is library, if you are creating something like Laravel then this will be project
license
- Type of license of your project
author
- Array of author details
require
- What packages does your project depends on add it over here. Mine doesn't depend on any so I left empty here
require-dev
- If your project depends on any development packages then add it here like PHPUnit
autoload
- Here you can autoload the default folder & files that is need by your project
I would like to add more details for autoload over here
"psr-4": {
"Channaveer\\RSS\\": "src/"
}
Here I would like to explain about autoload, all the files from src/
folder of my composer project will be auto loaded. All the files inside this src/
folder will have the namespace with Channaveer\RSS
namespace. Make sure to add it as Channaveer\\RSS\\ as is.
src
FolderAs you can see from the above image I have a folder called as src
this houses all the project code. RSS.php
is the main file which will have all of my code. Since I am writing a very tiny project I do have only 1 file. If you look at really good projects their src
folder is very large
I have written the main code inside src/RSS.php file
. I wanted to developed the package so that I can call it with ease as follows
$rss = new RSS();
$rss->siteName('SiteName')
->siteUrl('https://siteurl.com')
->description('Some random description of the site')
->language('en-US') /** Default is en-Us you can set any of yours */
->lastUpdated($feedItems[0]['updatedAt']) /** Just pass datetime string or date string */
->generate($path, $rssItems, $filename);
As you can see the I have used method chaining to make my calls dam easy to understand. I will possibly create another article on method chaining and explaining about my code. But for this article it's out of scope.
I have written my main code inside src/RSS.php
file
I have deployed it in GitHub
& even in Packagist
. Feel free to explore it.
Now you have the above code setup & RSS.php
file inside src/
but don't know if it works for you or not, so we need to test it for the sake of debugging.
I will create another project named as testrss
inside htdocs
folder for testing my rss
composer package.
The following is our project structure
Our testrss
folder will have the following composer.json
file
{
"name": "dc/demo",
"description": "",
"require": {
"channaveer/rss": "@dev"
},
"autoload": {
"psr-4": {
"Channaveer\\RSS\\": "src/"
}
},
"repositories": [
{
"type": "path",
"url": "../rss"
}
]
}
Concentrate on the following code inside the above composer.json file
"repositories": [
{
"type": "path",
"url": "../rss"
}
]
This code is the one while will tell that we wan to load the package from the local repository path. Since my rss
package is one level up I have added url as ../rss
Once you add the above code inside composer.json file. Just run
composer install
This will install the rss
project as normal composer project and will point symbolically to the rss
project. Any changes you make in rss
project will automatically reflect here directly.
Go to your rss
composer project.
Now we have MVP of our package so I want to push it to packagist
. Since we have developed this in a dev
environment, we have to add the releases
in GITHUB directly or can add tags
with GIT. If we don't add the tag or release then we need to install the package as dev, else it wont install
I will use GIT annotation tags. I have added annotation tag from master
branch with the following command
Tip - tag in GIT is like releases ie if we want to mark any specific version of the project that it was working awesome then you can use it.
git tag -a v1.0 -m "MVP Stable Release"
This will create v1.0
tag. We can push this tag version similar to that as of your normal branch as shown below
git push origin v1.0
Now you package is ready for its first release.
First go to packagist.org & create your account.
First go to your Packagist Profile Name -> (1) Profile -> (2) Click On Show API Token as shown in the following image to get your API token
Login To GitHub and go to the project repository settings as shown in the following image
Now click on WebHooks
from the left sidebar to add new WebHooks
as shown in the following image
Go to you Packagist dashboard. Now you can publish your new packages by clicking on Submit in Packagist.
Add your GitHub package URL here as shown in the following image. It may ask you for GitHub verification allow it to access your repos.
Once you successfully submit your package you will get the following page if everything is okay
You have successfully uploaded your 1st composer package.
Facebook Login With PHP Laravel Socialite
URL Redirects From Called Functions In Laravel
Route Model Binding In Laravel & Change Default Column id To Another Column
Stripe Payment Integration With Laravel
Create Custom 404 Page In Laravel
Ensure text remains visible during Webfont load
Send SMS With Amazon SNS (Simple Notification Service) Using PHP
Generate RSS Feeds in PHP Laravel
Firebase Cloud Messaging (FCM) Browser Web Push Notifications Using Javascript And PHP
Why You Should Run Cronjobs In Laravel?
Dependency Dropdowns With Javascript And PHP