Creating a package in Laravel 5.8

Mubasheer Shaik
May 24 · 4 min read
Laravel Package

In this article, we will discuss “Laravel 5.8 Package Development from Scratch”. Here we describe the package development steps creating and using packages. You can also check the official Laravel doc for package development. I’m creating an example package details given below:

List of contents:

  1. Prerequisites
  2. Creating package skeleton and structure and some basic folders and files including Service providers, classes and composer.json.
  3. Creating a Service Provider
  4. Creating Controller, Model, Migrations and views in the package
  5. Publish config file

Creating Package Skeleton

We are using “laravel/packager”, it will generate all the necessary files.

Using composer — run the below command in the terminal

composer require jeroen-g/laravel-packager

After executing the above composer command, It will add all the necessary dependencies into your Laravel application.

You can check the newly generated artisan commands in your terminal by running this artisan command.

php artisan list 

Packager commands:

Now, lets run “packager:new” artisan command to create a new package.

// Syntax - php artisan packager:new vendor_name package_name

php artisan packager:new gkblabs subscription

After executing the “packager:new” command, new directory “packages” created on the root of the Laravel app. The “packages” directory contains the newly created package within the folder name “gkblabs” and this directory contains the new package files named “subscription”.

You need to add more details regarding author name, email, package homepage if any, and license info for your package.

Time to adjust your composer.json extra section to enable automatic package discovery.

“extra”: {  “laravel”: {     “providers”: [       “vendor_name\\package_name\\service_provider_name”       ]      }  },

All other autoload configuration update automatically. As you can check your “composer.json”.

"autoload": {"psr-4": {"gkblabs\\susbscription\\": "packages/gkblabs/susbscription/src","App\\": "app/"},"classmap": ["database/seeds","database/factories"]},"autoload-dev": {"psr-4": {"gkblabs\\susbscription\\": "packages/gkblabs/susbscription/src","Tests\\": "tests/"}},

Now lets test and see if our package is being loaded correctly. Inside the boot method of your SubscriptionServiceProvider.php, let’s add a route and load it:

// Register the routes in Service provider
$this->loadRoutesFrom(__DIR__.'/routes/web.php');

Please note that:

  • __DIR__ refers to the current directory where the file is.
  • routes/web.php refers to the routes folder we are to create for our package, which will live in our srcfolder, not the default Laravel routes.

Also, you need to add your service provider to “config/app.php” file. It is necessary when your package will be autoloaded via composer and automatic package discovery. But we are using the ” jeroen-g/laravel-packager” package. This will update the provider when we create a package.

Illuminate\Validation\ValidationServiceProvider::class,
Illuminate\View\ViewServiceProvider::class,
/*
* Package Service Providers...
*/
gkblabs\susbscription\susbscriptionServiceProvider::class,
/*
* Application Service Providers...
*/
App\Providers\AppServiceProvider::class,
App\Providers\AuthServiceProvider::class,

Note: If your package wants another package to complete the functionality. Then you can easily add them. You just need to add the required package into the package “composer.json”.

After that on your terminal in the root of your app run:

composer dump-autoload

In our package routes folder add the web.php file and add the following code to it:

<?phpRoute::get('subscription', function(){
return '<h1>Welcome to GKB Labs and Hai form subscription package</h1';
});
?>

Now start your Laravel app using:

php artisan serve

Now open http://localhost:8000/subscription

Service Providers

Service providers are the connection points between your package and Laravel. A service provider is responsible for binding things into Laravel’s service container and informing Laravel where to load package resources such as views, configuration, and localization files.

A service provider extends the Illuminate\Support\ServiceProvider class and contains two methods: register and boot.

-to be conituned…

Mubasheer Shaik

Written by

Tech lead, GKB LABS.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade