Laravel Package Training v2.0
Together with my colleagues at Spatie, we have produced over 250 packages that have been download over 200 million downloads.
We learned a lot by quality packages like laravel-permission, medialibrary, browsershot, and many more. We feel we have a pretty good workflow to produce reliable, readable, and maintainable packages.
In this course, we’ll share the knowledge we have built up over the year with you.
This video course is the perfect place to start your package building journey. The course covers both building PHP agnostic packages and Laravel packages.
You’ll learn how to create a package from scratch, how to support multiple versions of PHP and Laravel, how to run tests on GitHub Actions, and much more! We’ll also source dive a few of our packages, so you can see a few good example of how we do things.
I can’t wait to see the cool packages you will code up with this knowledge.
Overview
In this video you’ll see a short overview of the entire course.
Links
- How Spatie started creating packages
- Spatie’s open source packages
- Package Skeleton for framework agnostic PHP Packages
- Package Skeleton for Laravel
Starting a PHP Package Using The Skeleton
In this video, we’ll review the contents our skeleton repository and explain how it can be used to start a package.
Links
- The PHP Skeleton Repo
- Changelog best practices
- An introduction in semantic versioning
- Spatie’s open source packages
- Pest
Adding Functionality To The Package
Let’s add some functionality to our package
Links
Using PHPUnit for tests
By default, our package skeleton using Pest as the testrunners for test. Don’t worry if you prefer PHPUnit, you can easily run those too. In this video, I’ll explain how you can do this
Links
Running Tests On GitHub Actions
We are going to take a look at how the tests can run on GitHub actions. You’ll learn how you can tests multiple PHP versions in one go using a matrix, and we’ll explore all steps of the run-tests workflow.
Finally we’re going to take a look at how GitHub displays the status of the tests in a PR.
Links
- GitHub actions docs
- How to use GitHub actions to run the tests of Laravel projects and packages
- How to use a MySQL database on GitHub Actions
- spatie/weight-conversions
Enforcing a Code Style
php-cs-fixer is an excellent tool to automatically fix code styling issues. In this issue you’ll learn how to run in locally and on GitHub Actions
Links
- FriendsOfPHP/PHP-CS-Fixer
- PSR-12 Code Style Guide
- Sharing PHP-CS-Fixer rules across projects and teams
- [spatie/unit-conversions]- spatie/weight-conversions
Releasing a Package
Now that our package contains some functionality, let’s release it, so anyone can use it.
Links
Creating a New Release
Let’s add some functionality to the package and create a new release from the command line.
Somewhere in this video, you’ll see me using the pf
command. This is an alias defined in my dotfiles, that expands to vendor/bin/phpunit --filter
Links
Updating The Changelog Automatically
When keeping a changelog, you don’t need to update it manually. This GitHub workflow can do it automatically for you.
Links
Handling Feedback With Issues And Discussions
When your package is released, users will likely start to give feedback. They want to report issues, suggest feature requests, and have general questions.
In this video, you’ll learn how you can use the issue and discussion tracker to triage all feedback.
Links
- Package skeleton for PHP
- Package skeleton for Laravel
- Issue template docs
- Spatie’s guidelines for handling feedback
Starting a Laravel package Using The Skeleton
Our Laravel skeleton repo is an easy way to get started with a Laravel package.
Links
Exploring The Service Provider
In the service provider, you can let your package hook in the various functionalities that Laravel provides.
Links
Installing an Unreleased Package in a Full Laravel App
Even though, you have a full Laravel app available in the test suite of the package, it can be handy to, before releasing it, install the package in a real Laravel app.
Links
Adding a Config File
To allow users to customise behaviour, your package can expose a config file. In this video we’ll add a config file to our package and discuss some interesting behaviour.
Links
Adding Migrations and Models
Packages can have migrations and models too. In this video I’ll show you how to add them to and test them in your package. As a bonus, you’ll also learn how to use factories in package tests.
Links
Using MySQL when running tests
In some cases, using SQLite just isn’t enough. In this video we’ll cover how to use MySQL in tests locally and on GitHub Actions
Links
Using Routes, Controllers and Views
In this video we’ll cover how you can add routes in the package in a way that they won’t conflict with existing routes in the app. You also learn how controllers and views can be added, and how those can be tested.
Links
Let’s Build a Package Together
In this video we’re going to use everything that we’ve learned during the course to build a package from scratch.
If you want to practice your package building skills by creating a PR to the spatie/laravel-disk-monitor repo, please do so!
This video was originally recording in the v1 version of our course. Most of the things you see still apply. In the next video, you’ll see how that old style service provider can be converted to a fancy new PackageServiceProvider
.
Links
Updating An Old Package To Use PackageServiceProvider
In the previous video, we’re still registering things in a very verbose way in our service provider. Let’s update it to use our PackageServiceProvider
Links
laravel-tail
Our Laravel Tail package allows you to easily tail local and remote logs. Let’s take a look under the hood.
This video was recorded in v1 of this course, most of the things show still apply.
Links
laravel-collection-macros
We’re going to take a look at another smallish package. Here you’ll see a nice way of separating functions to their own files.
Links
laravel-medialibrary
In this video I’ll show you that you can structure your package any way you want.
This video was recorded in v1 of this course, most of the things show still apply.
Links
laravel-responsecache
In this video I walk through the spatie/laravel-responsecache package. This one can speed up any Laravel app by caching response. You’ll learn how to use it, how it works under the hood, and how it is tested.
This video was recorded in v1 of this course, most of the things show still apply.
Links
laravel-multitenancy
Our laravel-multitenancy package can make any Laravel app tenant aware. The philosophy of this package is that it should only provide the bare essentials to enable multitenancy.
In this video, we’ll take a look at how it works under the hood.
This video was recorded in v1 of this course, most of the things show still apply.
Links
- laravel-multitenancy docs
- An unopinionated package to make Laravel apps tenant aware
- Mohamed Said’s videos on multitenancy
- Tom Schlick’s talk on multitenancy at Laracon US 2017
laravel-short-schedule part 1: Using the package
In this three part source we are going to take a look at the spatie/laravel-short-schedule. This package allows you to run Artisan commands at sub-minute intervals. In this first part you’ll learn how to use the package.
This video was recorded in v1 of this course, most of the things show still apply.
Links
laravel-short-schedule part 2: How the package works under the hood
In this second part, we are going to explore how the package works under the hood.
This video was recorded in v1 of this course, most of the things show still apply.
Links
laravel-short-schedule part 3: Testing the package
Testing functionality that uses a never ending loop doesn’t have to be hard. In this video you’ll learn a pragmatic way to tests ReactPHP loops.
This video was recorded in v1 of this course, most of the things show still apply.