On this page
- 1 How to Build and Publish a Laravel Package on Packagist
How to Build and Publish a Laravel Package on Packagist
Laravel makes it easy to modularize your features into packages. In this blog, we will learn how to create a reusable Laravel package and publish it on Packagist so that anyone can install it using Composer.
On this page
Why Package Your Laravel Code?
- Reuse across multiple projects
- Distribute to the community
- Maintain clean and modular code
Step 1: Create Your Package Structure
Inside your Laravel project, create a packages/
directory and structure your package like this:
packages/ └── your-vendor/ └── laravel-settings/ ├── src/ │ ├── Models/ │ │ └── Setting.php │ └── SettingServiceProvider.php ├── database/ │ └── migrations/ ├── composer.json └── README.md
Step 2: Define Your Composer.json
Make sure your composer.json
includes autoload and provider configuration.
Step 3: Push to GitHub
Create a new repository on GitHub and push your package code there.
Step 4: Tag Your Release
Run the following commands to tag your first release:
git tag v1.0.0 git push origin v1.0.0
Step 5: Submit to Packagist
Go to https://packagist.org/packages/submit and submit your repository URL.
Step 6: Install the Package Anywhere
After submission, you and others can install the package using:
composer require your-vendor/laravel-settings
Conclusion
Packaging your Laravel features is a great way to share your work and reuse it in multiple projects. With Packagist, your package is easily installable with Composer, making distribution effortless.