Making Packages For Laravel 5.2

Access all tutorials in sprocket icon.

Published April 6, 2016 by Bill Keck.

Making Packages For Laravel 5.2

I recently made my first custom Artisan command, which was a simple code generator for traits, available now through composer install from packagist. I find that I’m constantly using Artisan to create models, migrations, controllers, requests, middleware etc., and I wanted to keep a consistent work flow as much as possible. So I came up with a rather trivial, but useful artisan command:

php artisan make:trait MyTrait AllMyTraits

That creates the AllMyTraits folder if it doesn’t already exist and stubs out MyTrait:


namespace App\AllMyTraits;

trait MyTrait


Obviously it’s just an empty stub and that’s exactly what we want from our code generator.

Once I had this done, wrote a chapter on creating customer artisan commands in my book, Laraboot: Laravel 5.2 For Beginners, which I’m happy to say just got its first review on Goodreads. A nice 5 star review always motivates me to write more, which is why I keep updating the book.

So I was pretty happy with all that and then I thought, wouldn’t it be great to share the trait command? One way the PHP community shares work is by creating packages that are hosted by, which are then installed via composer. These are not limited to Laravel. That said, my experience in working with packages for laravel has been extremely positive. It just works really really well, from composer install to laravel configuration.

To make a package available via packagist, you of course first have to create a package. Fortunately, and this might sound a little funny, there’s an awesome package for making packages named packager by JeroenG.

So what that does is create a packages folder with everything stubbed out for you. It’s very easy to use.

Instead offering my own tutorial on this, I’m going to reference another, since it is extremely well-written. Check out:

Creating Laravel 5 Packages For Dummies by Cristian Tăbăcitu.

This is a step by step guide that will get you up and running, and he even covers how to put your package up on packagist.

So without recreating the entire tutorial here, let me just point out a tip. If you create the package within a project and then import that package via composer, make sure to follow the instructions in the tutorial about deleting the old package and the service provider reference in your config. Then I would also recommend running composer dump-autoload. I ran into this weird thing where PHP Storm was complaining about visibility on the package, even though it was installed properly, so dump-autoload took care of that for me.

Here is another article that is worth reading if your package includes routes, controllers and views:

How to Create A Laravel 5 Package in 10 Easy Steps by Povilas Korop

Package development is more an intermediate than beginner level task, but it is a lot of fun once you get up and running. If you want to play around with the package I created and use the artisan command, you can get it here on Github as well as on Packagist.

I hope you have enjoyed this article and found it useful. Click on the sprocket icon at the top of the page to see all my Laravel articles and tutorials. Please comment, share, like and review if you can, thanks.

I don’t have a donate button, but If you would like to support my work and learn more about Laravel, you can do so by buying one of my books, Laraboot: laravel 5.2 For Beginners, I really appreciate it.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s