Introduction to Package Development
In my experience, learning to develop a package for Laravel can be quite challenging, which is why I previously wrote a blog series about that.
Over time, I began thinking this topic deserves proper documentation, rather than a couple of posts that only cover my insights. That's where I feel this open-source documentation on Laravel Package Development will come in. I've bundled up my blog posts and expanded on a couple of more topics in separate chapters. Contributions (in the form of pull requests) are highly welcomed and appreciated. I hope this website can become a place to share knowledge on Laravel package development to help developers get a head start.
You are highly encouraged to participate and contribute to this project. Please feel free to submit a PR, even only for a typo.
First of all, I want to thank Marcel Pociot. His clear and structured video course encouraged me to create PHP packages myself. I can highly recommend his video course if you want to learn how to make (framework agnostic) PHP packages.
💡 Would you rather watch than read? The famous and reputable package builders from Spatie launched a full video course on Laravel Package Development.
Reasons to Develop a Package
You might encounter a scenario where you want to reuse some feature(s) of your application in other applications, open-source a specific functionality or keep related code together but separate it from your main application. In those cases, it makes sense to extract parts to a package. Packages or "libraries" provide an easy way to add additional functionality to existing applications and focus on a single feature.
In this documentation, we'll build a demo package along the way (called "BlogPackage") by introducing the listed functionalities one-by-one. Make sure to check out the finished version of this companion package to have a handy reference, for example, when something doesn't work as expected. The demo package contains a test suite comprising unit and feature tests for the covered topics.
Composer & Packagist
There are nearly 240,000 packages available on Packagist, the primary repository for PHP packages at the time of writing.
Packages are downloaded and installed using Composer - PHP's package management system - which manages dependencies within a project.
To install a package in your existing Laravel project, the
composer require <vendor>/<package> command will download all necessary files into a
/vendor directory of your project where all your third party packages live, separated by vendor name. Consequently, the content from these packages is separated from your application code, which means this particular code is maintained by someone else, most often by the creator of that package. Whenever the package needs an update, run
composer update to get the latest (compatible) version of your packages.
Tools and Helpers
The first chapter will address the basic structure of a package. While it is good to understand the general structure of a package, check out one of the following helpful tools to instantly set-up the basic skeleton.
Package Skeleton by Spatie This package skeleton by Spatie offers a great starting point for setting up a Laravel package from scratch. Besides the essential components of a Laravel Package, the skeleton comes with a GitHub specific configuration including a set of (CI) workflows for GitHub actions. They also offer a skeleton for generic PHP packages.
Laravel Package Boilerplate This tool by Marcel Pociot allows you to generate a basic template for Laravel specific and generic PHP packages that can be downloaded as a
Laravel Packager This package by Jeroen-G provides a CLI tool to quickly scaffold packages from within an existing Laravel application. The package was featured on Laracasts in the Building Laracasts series.
Laravel Packager Hermes This package by DelveFore is an extension of the Laravel Packager package, enabling usage of Artisan commands within that package to quickly generate Laravel specific classes. Currently, it only supports the scaffolding of
Orchestral Canvas The Orchestral Canvas package offers code generators and replicates all of the
makeartisan commands available in your basic Laravel application.
Yeoman Laravel Package Scaffolder This package provides a standalone generator to quickly scaffold a Laravel package. It will generate a skeleton structure, a ready-to-go composer.json file, and a fully configured service provider. Just uncomment what you need and start developing.
Laravel Packer A PHP package offering a command-line tool to scaffold a basic package directory structure and
composer.jsonfile and provides the
makeartisan commands within your package.
Laravel Package Maker A PHP package that provides all the Laravel
makecommands for package development. It uses Composer's repositories feature to symlink your test app with your package to make testing as easy as possible.