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
1 |
composer create-project laravel/laravel newProject --prefer-dist |
Change directory into newProject
1 |
cd 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:
1 |
vendor/package-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:
1 |
packages/busayo/laravel-devstatus/src |
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:
1 |
composer init |
After following the various prompts you’ll end up with a composer.json file that looks similar to this:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
{ "name": "busayo/laravel-devstatus", "description": "Displays your Developer Evangelist Status based on Github's Repo", "license": "MIT", "authors": [ { "name": "busayo", "email": "prosperotemuyiwa@gmail.com" } ], "minimum-stability": "dev", "require": {} } |
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:
1 2 3 4 |
"psr-4": { "App\": "app/", "Busayo\DevStatus\": "packages/busayo/laravel-devstatus/src" } |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<?php namespace BusayoDevStatus; use IlluminateSupportServiceProvider; class DevStatusServiceProvider extends ServiceProvider { /** * Bootstrap the application services. * * @return void */ public function boot() { // } /** * Register the application services. * * @return void */ public function register() { // } } |
Finally we need to add our Service Provider to the array providers array in config/app like so:
1 |
'BusayoDevStatusDevStatusServiceProvider' |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
<?php namespace BusayoDevStatus; use AppHttpControllersController; use AppUser; class DevStatusController extends Controller { /** * Display an object with a Welcome Status * * @return Response */ public function home(){ return response(['Developer Status' => 'Welcome to Dev Status Laravel 5 package' ]); } /** * Display developer status based on the number of Github Repos * * @return Response */ public function index($username) { $options = array('http' => array('user_agent'=> config('DevStatus.user-agent'))); $context = stream_context_create($options); $url = config('DevStatus.url') . "/users/" . urlencode($username); $result = json_decode(file_get_contents($url, true, $context)); $name = $result->name; $public_repos = $result->public_repos; $status = ""; if($public_repos <= 10){ $status = "Rookie"; } if( $public_repos > 10 && $public_repos <= 25){ $status = "Intermediate"; } if( $public_repos > 25){ $status = "Ninja"; } $finalStatus = $name . " is a " . $status . " Open Source Evangelist"; return response([ 'Developer Status' => $finalStatus ]); } } |
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.
1 |
/src/config/DevStatus.php |
This config file contains key values/information that we’ll require in our package for use.
1 2 3 4 5 6 7 8 9 10 11 |
<?php return [ 'url' => env('GITHUB_API_URL'), 'user-agent' => $_SERVER['HTTP_USER_AGENT'] ]; ?> |
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:
1 |
GITHUB_API_URL=https://api.github.com |
Let’s go back to our “DevStatusServiceProvider.php” file. Add the following line to the boot method:
1 2 |
//Publishes package config file to applications config folder $this->publishes([__DIR__.'/config/DevStatus.php' => config_path('DevStatus.php')]); |
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:
1 |
php artisan vendor:publish |
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:
1 |
include __DIR__.'/routes.php'; |
Our routes file contains two routes to the controller that we created earlier:
1 2 |
Route::get('devstatus', 'BusayoDevStatusDevStatusController@home'); Route::get('devstatus/{username}', 'BusayoDevStatusDevStatusController@index'); |
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:
1 2 |
// Let Laravel Ioc Container know about our Controller $this->app->make('BusayoDevStatusDevStatusController'); |
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.

- How to build your own Youtube – Part 10 - August 1, 2016
- How to build your own Youtube – Part 9 - July 25, 2016
- How to build your own Youtube – Part 8 - July 23, 2016
- How to build your own Youtube – Part 6 - July 6, 2016
- Introducing Laravel Password v1.0 - July 3, 2016
- How to build your own Youtube – Part 5 - June 28, 2016
- How to build your own Youtube – Part 4 - June 23, 2016
- How to build your own Youtube – Part 3 - June 15, 2016
- How to build your own Youtube – Part 2 - June 8, 2016
- How to build your own Youtube – Part 1 - June 1, 2016