Build your first Laravel package · Part 1 of 4
A package is just a folder
The folder, composer.json, PSR-4 autoloading, the service provider and auto-discovery. By the end, an empty package is installed in a real app and Laravel says it found it.
By Abhishek Fouzdar · Updated
By the end of this part you will have a package that does nothing, installed in a real Laravel application, with Laravel confirming it found it. That sounds like a small result. It is the part people get stuck on, because every step is new, so we take it slowly and check as we go.
Where the folder goes
Create the package folder next to your Laravel app, not inside it:
~/code/
├── settings-app/ your Laravel 12 or 13 app
└── laravel-settings/ the package, empty for now
Keeping it outside the app matters. Composer will later link the app to this folder, and the package should not know or care which app is using it.
cd ~/code
mkdir laravel-settings && cd laravel-settings
git init
composer.json: the package's identity
Every package starts with a composer.json. Create one with this content:
{
"name": "shipbytes/laravel-settings",
"description": "Application settings that live in the database, with a cache in front and a facade on top.",
"type": "library",
"license": "MIT",
"require": {
"php": "^8.2",
"illuminate/support": "^12.0|^13.0"
},
"autoload": {
"psr-4": {
"Shipbytes\\Settings\\": "src/"
}
},
"extra": {
"laravel": {
"providers": [
"Shipbytes\\Settings\\SettingsServiceProvider"
]
}
}
}
Replace shipbytes with your own vendor name throughout. Four parts of this file do real work.
name is vendor/package, all lowercase. The vendor is you or your company; the package is what it does. This is the name people will type into composer require, so make it say what the package is for.
require lists what the package needs. Notice it asks for illuminate/support, not laravel/framework. Laravel is published as one big framework package and as separate illuminate/* pieces, and a package should depend on the pieces it uses. Ours needs the ServiceProvider base class, which lives in illuminate/support. The constraint ^12.0|^13.0 says the package works with the pieces from Laravel 12 or 13; a package that only names one major version refuses to install the day the next one ships, which is exactly what happened to the first version of this one. In part 2 we add illuminate/database when we add a model. The example repository declares illuminate/contracts, illuminate/database and the facade alias from its first commit, because the finished package needs them; this series adds each one in the part where it is first used, so expect those lines in the diff. Depending on the pieces keeps the package light and makes it honest about what it touches.
autoload.psr-4 is the line most beginners get wrong. It says: any class whose name starts with Shipbytes\Settings\ lives under src/, with the rest of the namespace as folders. So Shipbytes\Settings\SettingsServiceProvider is the file src/SettingsServiceProvider.php, and Shipbytes\Settings\Models\Setting would be src/Models/Setting.php. The double backslashes are JSON escaping; the namespace is Shipbytes\Settings\. Case matters on Linux and in CI, even when it does not on your Mac.
extra.laravel.providers is Laravel's auto-discovery. When an app installs this package, Laravel reads this list and registers the provider on its own. Without it, every user would have to add your provider to their app by hand.
The service provider: how Laravel learns the package exists
A service provider is the one class Laravel calls when it boots. Everything a package does starts from here. Create src/SettingsServiceProvider.php:
<?php
namespace Shipbytes\Settings;
use Illuminate\Support\ServiceProvider;
class SettingsServiceProvider extends ServiceProvider
{
public function register(): void
{
$this->mergeConfigFrom(__DIR__.'/../config/settings.php', 'settings');
}
public function boot(): void
{
if ($this->app->runningInConsole()) {
$this->publishes([
__DIR__.'/../config/settings.php' => config_path('settings.php'),
], 'settings-config');
}
}
}
The namespace line is the first thing to check against composer.json: Shipbytes\Settings in the file, Shipbytes\\Settings\\ in the JSON, and the file sits at src/SettingsServiceProvider.php. If any of the three disagree, Laravel will say the class does not exist.
A provider has two methods, and the difference is worth learning now because it comes up in every package.
register() runs first, for every provider in the app, before anything is ready to use. Its job is to put things into the container: bindings, and here the config. mergeConfigFrom reads our config/settings.php and merges it under the key settings, so config('settings.table') works in the app even if the user never publishes the file. Their own config/settings.php, if they publish one, wins over ours key by key.
boot() runs after every provider has registered, so it is safe to use other services here. This is where a package wires itself into the app: routes, views, migrations, and publishable files. For now we publish the config, and only when running in the console, because vendor:publish is a console command and web requests never need to know the file exists.
The config file
Create config/settings.php:
<?php
return [
// The table the settings live in. Change it before running the migration.
'table' => 'settings',
'cache' => [
// Which cache store to keep the settings in. null means the app's default store.
'store' => null,
// The cache key that holds the whole settings table.
'key' => 'shipbytes.settings',
],
];
A package config file is an ordinary Laravel config file. The comments are not decoration: for most users this file is the only documentation they read, so say what each key is for and when to change it.
Your folder now looks like this:
laravel-settings/
├── composer.json
├── config/
│ └── settings.php
└── src/
└── SettingsServiceProvider.php
That is a complete package. Let's prove it.
Installing it into your app
Composer normally downloads packages from Packagist. For a package on your own disk you tell Composer where the folder is, using a path repository. In your app, not the package:
cd ~/code/settings-app
composer config repositories.settings path ../laravel-settings
composer require shipbytes/laravel-settings:@dev
Two things to notice. The path is relative to the app's composer.json, which is why the package lives next to the app. And @dev is required: the package has no tagged version yet, so you are asking for its development state. Composer answers by creating a symlink at vendor/shipbytes/laravel-settings that points at your folder. Edit a file in the package and the app sees the change immediately, with no reinstall. That is what makes developing a package pleasant.
At the end of the install Composer runs php artisan package:discover, and its output includes a line for your package:
shipbytes/laravel-settings ............................ DONE
That line is Laravel's auto-discovery reading extra.laravel.providers and registering your provider. If it is missing, run php artisan package:discover yourself and read the list; every package it found is there.
Checking it works
Three quick checks, in the app.
The config is merged:
php artisan tinker --execute="var_dump(config('settings.table'));"
string(8) "settings"
The config can be published:
php artisan vendor:publish --tag=settings-config
A config/settings.php appears in your app, a copy of the package's file. Delete it again if you like; the merged defaults keep working either way.
And Composer knows where the package comes from:
composer show shipbytes/laravel-settings
The output names the version dev-main and, on the dist line, [path] followed by your folder: the package came from disk, not from a download.
When it does not work
"Class Shipbytes\Settings\SettingsServiceProvider not found." The namespace in the file, the PSR-4 key in composer.json and the file path disagree somewhere. Compare the three character by character, then run composer dump-autoload in the app. Composer only regenerates its class map when told to.
The package is not discovered. Check that extra.laravel.providers is inside extra, spelled exactly, and that you ran composer require after adding it. Auto-discovery happens during install, so a change to composer.json in the package needs composer update shipbytes/laravel-settings in the app.
composer require says it cannot find a version. You forgot :@dev, or the path in repositories.settings is wrong. Run composer config repositories in the app to see what Composer thinks the path is.
Checkpoint
You should have:
- a folder with
composer.json,config/settings.phpandsrc/SettingsServiceProvider.php, plus aLICENSEfile (MIT, to matchcomposer.json) and a.gitignorewith/vendor/andcomposer.lockin it; - the package installed in your app as a symlink, with
shipbytes/laravel-settings ... DONEprinted under "Discovering packages" during install; config('settings.table')returning"settings"in Tinker.
Commit it. The matching commit in the example repository is Part 1: a package is just a folder. If your version differs, that diff shows exactly where.
In part 2 the package gets its feature: the migration, the model, the Settings class and the facade.
New posts by email
Get new posts by email. Nothing else, no digests, unsubscribe in one click.
Comments
Comments are GitHub Discussions in shipbytes/discussions. Sign in with GitHub to post.
In this series
1 / 4- OverviewBuild your first Laravel package
- Part 1A package is just a folder
- Part 2The settings feature
- Part 3Testing without an app
- Part 4Sharing it