Laravel makes good use of packages, there are hundreds of packages available and even the framework itself is a package.

A lot of people create packages to add new functionality to Laravel, like a Shopping Cart or extra commands to Artisan (Laravel php cli)

Creating a package isn’t the easiest thing, and there isn’t much documentation about it. So let’s start creating your first package.
In this tutorial we are going to create a User Behavior package.

To start off, let’s create a new folder structure in Laravel.
I personally made a new folder in the laravel root called “packages”.

In the packages folder you have to create a folder with the vendor name, usually your name or the company you work for.
In there you create a folder with the name of your packages. In the end I had the following folder structure:

Laravel Root
  - packages
    - HazeDevelopment
      - UserBehavior

In your package (UserBehavior) folder, create  a folder “src”, all your code will go into this folder.

Move to your package folder, and open your command prompt.
run the following composer command:

composer init

Follow the instructions, and a composer.json file will be created for your package, add any dependencies your package will use, if needed.
In my case, I didn’t need any new dependencies as it was a standalone library.


Let’s tell laravel to autoload our files, so laravel knows in what kind of namespace we work.
Open composer.json in the laravel root and search for the following:

"autoload": {
	"classmap": [
	"psr-4": {
		"app\\": "app/"

We need to add our own namespace and tell laravel where to look, so we will add the following line to the psr-4 array:

"Vendor\\package" : "packages/vendor/package"

Don’t forget to add a trailing comma to the “app” line, or you will end up with errors.
in my case it will look like:

"HazeDevelopment\\UserBehavior" : "packages/HazeDevelopment/UserBehavior"

Now you’re still in the laravel root, open your command prompt and type the following composer command:

composer dump-autoload


Now we need a service provider, so laravel knows that our package exists.
Go into the src folder, and create a file with the name “YourPackageServiceProvider.php”, in my case I ended up with UserBehaviorServiceProvider.php

In this file we will tell laravel what to do when it loads our package.
Add the following code to the service provider file.


namespace HazeDevelopment\UserBehavior;

use Illuminate\Support\ServiceProvider;

class UserBehaviorServiceProvider extends ServiceProvider
     * Bootstrap the application services.
     * @return void
    public function boot()

     * Register the application services.
     * @return void
    public function register()

the boot function is especially made for things that have to be done before all other code is executed.
the register function is made to add things to the service container, you will rarely use this, and we won’t in this tutorial.
More about the boot and register function can be found here:


In your service provider, the namespace has to match your package name and vendor, just like we assigned it in the composer.json.
In my case I used HazeDevelopment\UserBehavior.


Now we need to let laravel know the Service Provider exists, go to your laravel root and open the app.php in the config folder.
Add your service provider in the providers array like the following:


Now laravel knows our service provider exists, and it will execute the boot and register function.

For my package, I wanted an additional config file, so I created a config folder in my src folder of the package.
in this config folder I added a file called “UserBehavior.php” and added the following code:


return [

    'max_tracking' => 10,


This will become the config file for my package.

The config file isn’t loaded yet into laravel, so we open our service provider, and add the following code into our boot function:

    __DIR__.'/config/userbehavior.php' => config_path('userbehavior.php')
], 'config');

This will tell laravel to copy our config file, into the laravel config folder, so we can use the config variables in our package.

Go back to your laravel root, and open the command prompt to run the following code:

php artisan vendor:publish

This will execute the “publishes” function we assigned in the boot function, so our config file is actually copied to the config folder.

Now you can use your package src folder as it was a laravel app folder, you can add controlers, models, migrations etc..
but I’ll leave that to your imagination to create.


More information can be found in the following url’s:



Good luck with your package development, and for any questions, leave a comment and I will try to reply as soon as possible.