How to Build and Publish a Laravel Composer Package from Scratch
This step‑by‑step guide shows how to create a Laravel 5.6 project, develop a reusable Composer package with routing and permission features, test it locally, and finally publish it on GitHub and Packagist for easy installation via Composer.
Tools and Materials
git
composer
IDE
Preparation
Create a blank Laravel 5.6 project:
composer create-project laravel/laravel laraveltest --prefer-distConfigure the .env file and database settings.
Create a folder in the project root to hold the package resources.
Package Development Process
Develop the package in a separate project (e.g., a routing and permission validation package) and implement the desired functionality.
Copy the developed file structure into the custom folder of the blank Laravel project (e.g., project_root/dongdong/routeauth ).
Create a src directory and add a ServiceProvider.
In the provider implement two methods: register : bind services into the container. boot : publish assets using publishes . Running php artisan vendor:publish --force copies files to the target folder and overwrites existing ones.
Run composer init in the package folder to generate composer.json , then remove the "type": "project" entry.
Local Testing
Modify the main Laravel project's composer.json to point to the local package directory.
Run composer update , then publish with php artisan vendor:publish --force .
Publishing to Packagist
Create a GitHub repository (e.g., https://github.com/dongdonggo/routeAuth ) and push the package files.
Sign up on Packagist and submit the GitHub repository URL.
After approval, the package can be required with composer require vendor/route-auth . If you previously used a Chinese mirror, switch back to the main Packagist URL before requiring.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.