To make our package available to others, we just need to initialize the Git repository inside our folder of /packages/laraveldaily/laravel-permission-editor
and push the code to GitHub, then register that repository on Packagist.
So, step by step.
First, we create a new repository on GitHub. You can actually use a different system like Gitlab or Bitbucket, just the repository should be public.
Important: to avoid conflicts, the repository name should be exactly the same as you specified in the composer.json
of the package, in the "name" field.
Then we follow the GitHub instructions to push our code to the repository, just replacing the suggested "readme.md" commit with pushing all files.
cd packages/laraveldaily/laravel-permission-editorgit initgit add .git commit -m "First commit"git branch -M maingit remote add origin https://github.com/LaravelDaily/laravel-permission-editor.gitgit push -u origin main
This is the result, in Terminal:
So, our package code is on Github now, great!
As you can see in the screenshot above, there are no installation instructions, and even GitHub itself is telling us to create a README file.
Let's be polite to our users and create this file, with installation instructions, in Markdown language, and push it to GitHub. This is the result:
We have the package on GitHub, but this is not where composer
takes the packages from. The central database of the packages is called Packagist. You need to create the account there, and then Submit your package.
But, before we do that, let's tell Packagist that we're launching our first version, let's assign it a version number of v0.0.1
.
In general, if you follow the so-called semantic versioning, those three parts mean this:
It's totally your personal preference which version to start from: some creators choose to start with v1.0.0
, while others release v0.0.1
and stay on v0.x
for months after that.
To release a version, we need to do two things.
First, in the composer.json
of our package, we need to specify the version and push that change to GitHub:
packages/laraveldaily/laravel-permission-editor/composer.json:
{ "name": "laraveldaily/laravel-permission-editor", "description": "Visual UI for managing Roles/Permissions for Spatie Laravel Permission package", // ... "version": "0.0.1"}
Next, we need to go to GitHub and look at the "Releases" on the right side, and click "Create a new release":
Inside that form, we need to create a new tag corresponding to our version number:
Finally, we can put the name and the description of the new version, which would be easily visible to the package users.
Feel free to put any text you want in there, and click "Publish release".
As I mentioned before, we need to publish the package on Packagist.
The direct URL to submit the package is this: packagist.org/packages/submit
As you can see, all we need to do is provide the repository URL from Github. Packagist will "scan" the contents of that repository and check if everything needed is present for the release.
So I put this: https://github.com/LaravelDaily/laravel-permission-editor
Then I get this text as a result:
The package name found for your repository is: laraveldaily/laravel-permission-editor, press Submit to confirm.
And I click "Submit"...
First, the package will be crawled, and then the information update will happen, and in a minute or so, I see THIS:
That's it! We released our package to the public!
Hard to believe? Let's test it! Let's create a new fresh Laravel project, install the Spatie Permission package, and install our package.
laravel new project5cd project5composer require spatie/laravel-permissionphp artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvider"php artisan migratecomposer require laraveldaily/laravel-permission-editorphp artisan vendor:publish --provider="Laraveldaily\LaravelPermissionEditor\PermissionEditorServiceProvider"
Launching the browser...
Ta-daaaa!
So, that's it for this long tutorial, this is how to create and publish a Laravel package. As always, if you have any questions or see some information missing, shoot in the comments below.
The package repository is available on GitHub: LaravelDaily/laravel-permission-editor
See you guys in other tutorials!