Tutorial

Building a Laravel Package: Lessons from Creating Ahantu

CD
Cyatangabo Didier
November 15, 2024 8 min read
No Image

How I built and published my first Laravel package on Packagist - a location data seeder for Rwanda's administrative divisions. Lessons learned about package architecture, testing, and distribution.

Why Build a Package?

When working on multiple Laravel projects targeting the Rwandan market, I found myself repeatedly creating the same database structure for provinces, districts, sectors, cells, and villages. Instead of copy-pasting code between projects, I decided to build a reusable package.

Package Architecture

The package follows Laravel's service provider pattern. The main components are:

  • Service Provider - Registers the artisan command and publishable assets
  • Install Command - Generates models, migrations, and seeders
  • Stub Files - Templates for the generated code

Publishing to Packagist

Getting the package on Packagist was straightforward:

  1. Structure the project with a proper composer.json
  2. Push to GitHub with semantic versioning tags
  3. Submit the repository URL on packagist.org
  4. Set up auto-update webhooks

Key Takeaways

Building packages forces you to think about API design, backward compatibility, and documentation. It's one of the best ways to contribute to the developer community while solving your own problems.

Share this article

Tags

Laravel PHP Open Source Packagist

Related Articles