Create a Laravel Workbench

For Package Development

A workbench is a project dedicated for creating all your packages.

First step

The first step that needs to be taken, is cloning Laravel. Go to your code directory and run git clone git@github.com:laravel/laravel.git. Your workbench directory will now be called laravel. The location of my workbench is ~/Server/laravel.

Packages folder

Within your workbench you need a directory for your packages. We are going to use this directory to autoload the packages you are creating. For your package you can create directories with the following structure: packages/[Vendor]/[Package]/src. This could result in packages/Captain/Awesome/src.

Composer.json

Navigate to you package (packages/Captain/Awesome/src) and run composer init. You will be asked several questions, answer them, and add the dependencies you need.

AwesomeServiceProvider

Now it’s time to create the bootstrapper of your package, the ServiceProvider. Place it in the src/ directory of your plugin. You can find more information about it in the docs. Most of the time, people use the name of their plugin + ServiceProvider. So in our example, it will be AwesomeServiceProvider.

Autoload your package

Now your plugin needs to be autoloaded, like all the files in vendor/. You can do that by adding the packages/ folder to the composer.json of your workbench.

"autoload": {
"psr-4": {
"App\\": "app/",
"Captain\\Awesome\\":"packages/Captain/Awesome/src"
}
}

Add your ServiceProvider to config/app.php

Now we need to add our package to the Laravel workbench project. We can do that by adding our AwesomeServiceProvider to config/app.php. It will look like this:

...
Illuminate\Session\SessionServiceProvider::class,
Illuminate\Translation\TranslationServiceProvider::class,
Illuminate\Validation\ValidationServiceProvider::class,
Illuminate\View\ViewServiceProvider::class,
/*
* Package Service Providers...
*/
Captain\Awesome\AwesomeServiceProvider::class,
...

Skeleton

We use a skeleton repo for creating packages, so we don’t have to do the same stuff (creating files, folders) over and over again. If you want to use it, have a look here. (props: Spatie)

Credits

I got my information from a clear topic on Laracasts. But I saw the post was old, so I checked if it still works, and wrote about it in this article.


Hope this will help you with your package development. I tried to test it the best I could, so I hope you won’t have any problems with this guide. Otherwise, let me know!

Happy coding! :)

Show your support

Clapping shows how much you appreciated Gijs Jorissen’s story.