Create New Package in Laravel

1. New package directory

Create a directory with a name of the package. I will call it laravel-log-enhancer.

2. composer.json

Every package starts with a composer.json file. Let’s create it. The easiest way to create it is to fire the init command.

Or you can just copy it from this stub and make changes accordingly.

Commit the change. Not compulsory but we will do it after each unique step as a good habit.

4. Namespace and autoloading

We will put the main code of our package in src directory.

5. Update composer.json file

6. Create new file in src dir packagedirServiceProvider.php and update this

We will use it in our case to load the default configuration options of the package as well as offer an option to export those config options to the parent Laravel web app.

The code is quite simple here. In the boot method, we put the code to allow the developer to export the config options by simply writing:

And in the register method, we tell the Laravel app to add the config options from our file into the web app config. Commit the update and let’s create our config file next.

Config options:

As a part of the convention, we will put our config file in config directory inside root folder next to src directory.

Put the following code in the file.

Leave a reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">