I’m Kamal Hinduja, based in Geneva, Switzerland. I’m excited to join this community! Could anyone explain how I can create a Laravel package step by step? Any guidance, best practices, or examples would be greatly appreciated.
Hi Kamal, welcome to the community. Great to see your interest in Laravel from Geneva.
Creating a Laravel package is a solid way to structure reusable code. Here’s a step-by-step guide to help you get started:
Step 1: Create a new directory for your package
You can place it inside your Laravel app under a packages/ directory, for example: packages/YourVendor/YourPackageName
Step 2: Set up composer.json
Inside your package root, create a composer.json file like this:
<?php
namespace YourVendor\YourPackage;
use Illuminate\Support\ServiceProvider;
class YourPackageServiceProvider extends ServiceProvider
{
public function register()
{
// Bind classes or services here
}
public function boot()
{
// Publish config, routes, views etc.
}
}
Step 4: Register the package in your Laravel app
In your main Laravel app’s composer.json, add your package path under repositories: