← Back to Course

Creating Custom Laravel Packages

When code needs to be shared across multiple projects, or kept cleanly separated from the main application, turning it into a standalone Composer package is often the right move.

Basic Package Structure

my-package/
├── src/
│   └── MyPackageServiceProvider.php
├── composer.json
└── README.md

Defining composer.json

{
    "name": "yourname/my-package",
    "autoload": {
        "psr-4": { "YourName\MyPackage\": "src/" }
    },
    "extra": {
        "laravel": {
            "providers": [
                "YourName\MyPackage\MyPackageServiceProvider"
            ]
        }
    }
}

The Service Provider

The service provider is where the package registers its routes, views, config, and any bindings the main application should have access to.

public function boot()
{
    $this->loadRoutesFrom(__DIR__.'/routes.php');
    $this->publishes([
        __DIR__.'/../config/mypackage.php' => config_path('mypackage.php'),
    ]);
}

Testing Locally Before Publishing

A package can be linked into a local project using Composer's path repository type, letting it be developed and tested before it's ever pushed to Packagist.

"repositories": [
    { "type": "path", "url": "../my-package" }
]

That completes the packages and ecosystem section of the course — from installing third-party tools to shipping a reusable package of your own.

Ready to master Laravel Training Course?

Join Uncodemy's hands-on Laravel program and build real, production-ready applications.

Explore Course