This article will cover the basics of setting up an empty package which you can add your own code to later. This package can be installed in a Laravel 7 application. I assume that you already know how to set up a new Laravel application with all the requirements and run it locally on your computer. If you're not sure how to do this, checkout the official documentation first: https://laravel.com/docs/7.x.
Setting Up the Environment and Package Files
If you don't have a Laravel application to install your package to, you will first need set up a new Laravel project following the official documentation. This will serve as your development environment where you will install your package via Composer.
Within your project's src
directory create a packages
directory and then a directory within that using whatever you choose for your vendor name. Then create another directory within the vendor directory using your package name as the folder name. The path to your package should look something like this:
laravel_project/src/packages/myvendorname/mypackage
If you're creating this within an existing Laravel App and are using Git, you will want to ignore this directory. Also, if you want to publish your package, go ahead and run git init
inside of the package directory to create a new repo. You will later need to upload the your source code to Github before publishing on Packagist.
Now, within your new directory, you can initialize your project using composer init
and answer each of the questions when prompted:
- Package name: This should match the path inside your packages directory
- Description: Type a short description to let people know what your package does.
- Author: Your first and last name followed by your email address, for example,
John Doe <john@example.com>
- Minimum Stability: dev
- Package Type: leave blank
- License: enter a license if you want this to be open-source or leave blank
- Would you like to define your dependencies (require) interactively? no
- Would you like to define your dev dependencies (require-dev) interactively? no
- Do you confirm generation? yes
The result of your answers will be contained within the newly generated composer.json
. There are a few more things you'll want to add to this file.
Add Dependencies
At a minimum you'll want to add PHP and Laravel as your dependencies. You may also want to add some development dependencies such as PHPUnit for testing:
"require": { "php": "^7.2.5", "laravel/framework": "^7.0",},"require-dev": { "phpunit/phpunit": "^8.5"},
Add Autoload
We're going to use you package name as a namespace to autoload the files. (Make sure to change "MyPackage"):
"autoload": { "psr-4": { "MyPackage\\": "src/" }},
Add the Service Provider
We will be creating a Service Provider to register our package's routes, resources, and many other things, but first, we'll add it here so Laravel knows to check that it exists (make sure to change "MyPackage"):
"extra": {
"laravel": {
"providers": [
"MyPackage\\MyPackageServiceProvider"
]
}
},
Now that your composer.json is ready, you can move on to creating the Service Provider.
Creating the Service Provider
The service provider is a single PHP file that we will use to register everything related to the package to make it accessible wherever it is installed. It has two essential methods: boot() and register().
Before creating the service provider, first create a src
directory inside of your package directory where it will live.
After that the best way to create the service provider is to run php artisan make:provider MyPackageServiceProvider
from within your main project's src
directory, making sure you use the same class name as is in your package's composer.json
. This Artisan command will create a new file in app/providers
folder of your main project. Copy that file over to your package's src
directory and we can begin making changes.
First, make sure that the namespace has your package name instead of App
. You'll also want to use your package namespace whenever adding new classes.
Your service provider should look something like this:
<?phpnamespace MyPackage;use Illuminate\Support\ServiceProvider;class TestServiceProvider extends ServiceProvider{ /** * Register services. * * @return void */ public function register() { // } /** * Bootstrap services. * * @return void */ public function boot() { // }}
Now, add a couple of lines that will help with testing the package.
To the register()
method add:
echo 'My package registered!';
To the boot()
method add:
echo 'My package booted!';
Installing the Package
Finally, we can install the package in your main project. To do this we are going to use the repositories
field in you're project's composer.json
to tell Composer to install your package from your packages directory.
Add the following to your main project's composer.json
. The url
should match the path to your package.
"repositories": { "mypackage": { "type": "path", "url": "packages/myvendorname/mypackage", "options": { "symlink": true } }},
Run composer require myvendorname/mypackage
to install your package. When the package installs you should see "My package booted!" in your terminal.
Once Composer is done, make sure your server is running and open up your browser to view your localhost and you should see "My package registered!"
At any time you can also take a peak inside your vendor
folder to verify that your package files were installed correctly.
Your Empty Package Is Ready
Now that you have the basic stuff completed you can start developing your package. Once you know what you want to build, the official documentation is a good start for understanding how to build out different features. You can also look at other open-source Laravel packages on Github to see how other developers wrote their code and structured their projects.