PHP

CREATING YOUR FIRST LARAVEL 5 PACKAGE


Hello, Today we’ll be getting started with creating packages in laravel 5.

Are you excited?

We created a micro service in our last post and it was a simple but lovely and fun micro-service. Let’s assume someone needs that exact same functionality in his Laravel app, the simplest way of making that functionality available to that person and other persons that might find it useful is by exporting it in form of a Laravel Package.

Let’s get Started.

Create a new install of Laravel

Change directory into newProject

Now, we’re in the newProject Directory that contains our newly installed laravel framework.

Create a packages folder in the root directory.

Namespacing the structure of our package is very important and it follows a standard known as PSR-4. Most packages that have this structure use the following path name:

“vendor” is the name of the person or company that makes the package and “package-name” is the name of the package. I’ll use my name as the vendor (common practice), and I’ll call my package “laravel-devstatus”. I like to place all my custom packages within the root directory of my Laravel app inside a “packages” folder. Remember the “packages” folder we created earlier on. We also need a src folder where all our code will go, therefore my folder structure for my custom package looks like this:

Now that we have our folder structure the first thing we need to do is create a composer.json file for our package. This is required so that we can make our package available to download to the public as well as define any dependencies the package requires.

Navigate to the “laravel-devstatus” directory and run the following command:

After following the various prompts you’ll end up with a composer.json file that looks similar to this:

Before we move on from composer we still need to do one thing, autoload our package so that Laravel can use it. To do that open “newProject” folder’s composer.json file which is found in the root of your Laravel application. Within the “psr-4″ section of this file you’ll already see a namespace which autoloads the app directory, underneath add a name-space which points to the package we are building. In my case I am doing something like this:

Time to Create a Service Provider

Service Providers tell Laravel all about our custom package and let Laravel know what dependencies we need to bind and resolve to and from the IoC container.

Create “DevStatusServiceProvider.php” file inside the “src” folder of our package update the name-space to reflect the one you added to your root Composer file. Your Service Provider should look like this:

 

Finally we need to add our Service Provider to the array providers array in config/app like so:

Ok, now that we’ve set up our package it’s time to write some code.

Creating a controller for our package

I’m going to create a basic controller and place it in the “src” folder of our package directory it looks like this:

As you can see it’s nothing magical, just two methods. The “home” method returns a json like response. The second method does the main logic of our package,it retrieves the user’s github username, sends a request to github’s api and compares the result returned to determine if the user is Ninja, Intermediate or Rookie Open Source Evangelist.

Creating A Config File

Let’s create a folder called config inside the “src” folder and also create a config file named “DevStatus.php” like so inside the “config” folder.

This config file contains key values/information that we’ll require in our package for use.

Next, create a GITHUB_API_URL key inside your .env file in the root directory of “newProject” folder and assign “https://api.github.com” to it like so:

Let’s go back to our “DevStatusServiceProvider.php” file. Add the following line to the boot method:

This one line tells Laravel to publish our “DevStatus.php” file to the config folder so that whoever downloads our package can edit them without touching the code in your package directly.

After someone downloads your package if they run:

your “Devstatus.php” config file will be copied into the root config folder.

Creating a custom routes file

A package needs to be able to route and thankfully this is very easy to set up. You can simply create a routes file in the “src” directory and then add the following line to the register method of your Service Provider:

Our routes file contains two routes to the controller that we created earlier:

We’ve already autoloaded the files we need but how does Laravel’s IoC container know about our controller? At the moment it doesn’t so let’s fix that! Add the following to the register method of your Service Provider:

This line of code makes sure our controller get’s resolved out of the IoC container along with any dependencies it may have.

At this point if you want to test if our package works completely, Do the following:

1.Copy the DevStatus.php file from the config folder in our package to the root config folder.
2. Verify and make sure in config/app.php you have ‘BusayoDevStatusDevStatusServiceProvider’ listed in the providers array.

Now run your laravel app. Go to /devstatus in your browser you should see a json reponse welcoming you to use the package.

Then go to /devstatus/busayo, busayo should be replaced with your github username and you’ll receive an appropriate json response stating your Developer Evangelist Status. Package complete!

Package Summary

We can summarize our package in the following steps:

1.We set up a folder structure for our package.
2.We created a composer file for our package and autoloaded it
3.We created a Service Provider for our package and added it to the existing list in config/app.php
4.We created a controller that contains the logic to get the Developer Status
5.We created a config file that contains our packageconfigurations.
6.We created a routes file to point to our controller
7.We updated our Service Provider to resolve our Controller

Deploy to Packagist

Firstly you need to upload your package to a repo on Github. Then follow the instructions here to post your package on Packagist.

Now, if you want your package to be available on Packalyst( Repository of Laravel packages Only), then add “laravel” tag to the keywords in your package’s composer.json file.

This is the url to the github repo of our newly created package https://github.com/busayo/laravel-devstatus

Thanks for reading, if you have any questions simply comment below.

PROSPER OTEMUYIWA

About PROSPER OTEMUYIWA

Food Ninja, Code Slinger, Technical Trainer, Accidental Writer, Open Source Advocate and Developer Evangelist.