How To Create Laravel Basic Package

Laravel Basic Package

Here we are discussing and learn about ‘How to create Laravel basic package’. Hope you are aware of Laravel, don’t worry if not, you can learn the basic of Laravel from Laravel official site.

I tried to explain these things in a basic term so anybody can easily implement the basic skeleton of Laravel package.
 

Let’s we start point by point:

Every laravel package have common folder structure like: <vendorname>/<packagename>. You can create own folder ‘packages’ at root level OR you can create your folder under ‘vendor’ folder. I’ll use ‘vendor’ folder for this explanation.  

  • Create package folder under ‘vendor’ directory(Folder name format: <vendor>/<vendorname>/<packagename>)

Here,  <vendorname> could be your name OR your company name. (I.e. addweb/sample)

  • Run composer init Command at after navigating to this folder in CLI and answer the composer questions for the first setup and create src folder like vendor/addweb/sample/src

At this point, a composer will gather basic require information about your package, create composer.json and put that information into it.

  • For local testing add this package path into app root composer.json under autoload[] like:
"psr-4": {
                "App\\": "app/",
                "Addweb\\Sample\\": "vendor/addweb/sample/src"
}


With this step, your package will get loaded using PSR-4 autoloading but for that, we need tell composer using the command, we will check it with next step.
NOTE: Step for package submit to packagist.org. We will discuss in next blog on How to deploy our package at packagist.org.

  • Autoload this package using composer dump-autoload at root folder CLI

By run above autoload command, we actually tell Composer to autoload the all require things from composer.json and looks for all of the classes it needs to include.

Laravel official document say’s: “Service providers are the central place of all Laravel application bootstrapping. Your own application, as well as all of Laravel's core services, are bootstrapped via service providers”. 

Means that service provider is we can say the heart of the application. So lets we move forward with creating our package service provider.

  • Create service provider using PHP artisan make: provide sample service provider at root folder CLI 

As per this point, your package provider is generated with the basic skeleton, you can put some extra ingredient as per your requirement. But before it we need to move this provider file into our package folder: vendor/addweb/sample/src/sampleServiceProvider.php 

  • Open that moved file and change namespace like namespace addweb\sample;

By default created skeleton need to change namespace with actual one so that composer will hook our provider class using the PSR-4 autoloading standard 

Now let’s register our service provider with Laravel application.

  • Register our service provider into config/app.php into providers[] array. Here we need to add our service provider into providers[] like:
'providers' => [
    .
    .
    .

    /*
   * My Package Service Providers...
   */
    Addweb\Sample\SampleServiceProvider::class,

    .
    .
    .

]; 

 

Now, let create and register our testing route:

  • Create new routes.php into our package src folder to handle our package used the route and register that routes.php into our service provider boot() to initiate our package used routes:
routes.php
 
	<?php

	Route::get(‘firstroute’, function(){
	echo ‘Print from package’;
});

sampleServiceProvider.php
Public function boot(){
	//
	Include __DIR__.’/routes.php’;
}

 

Now our basic package is ready to server. Just Run PHP artisan serv at the root of your application folder and open that URL into your browser. 

 

If you are using localhost then you can check link like localhost:8000/firstroute at your browser address bar.  Hope this helps you well, feel free to add your comments/feedbacks. Need more assistance regarding Laravel Development Service Get in touch today!