Laravel Package Development from start to end

Laravel Package Development from start to end

If you are a Laravel developer and you really like to make an application with Laravel then probably you are eagerly seeking for how to create laravel package. Laravel packages are a really great opportunity to extend the power of the Laravel framework. In this post, I'll teach you how to make a laravel package from scratch and I hope after finishing this post you'll be able to make your first laravel package. Let's start.

 

Step 01: Package setup

First, make a packages directory in your project root. Inside the packages directory, make another folder for your package vendor name. For example, laravearticle is our vendor name. You can use your name or website name as a vendor name.

In vendor folder make a directory with your package name. Inside package name folder add an src folder. All our package code will be there.

Directory Hierarchy

app
bootstrap
config
database
packages
  laravelarticle
    my-package
      src
      composer.json 

 

Step 02: Ready composer.json

Now write composer.json file like below. You have to change some of the information according to your information.

{
    "name": "laravelarticle/my-package",
    "description": "First laravel package",
    "license": "MIT",
    "authors": [
        {
            "name": "Harun",
            "email": "info@example.com"
        }
    ],
    "minimum-stability": "dev",
    "prefer-stable" : true,
    "require": {},
    "extra": {
        "laravel": {
            "providers": [
                "laravelarticle\\MyPackage\\MyPackageServiceProvider"
            ]
        }
    }
} 

Look, here in JSON file, name key's value formation is vendor-name/your-package-name. Here, our package name is my-package. You have to change your own package name. In the extra section, we have to write our package service provider which we'll create later. The extra section needed for laravel > 5.5 version auto-discovery features. With the help of auto-discovery features, our package user doesn't need to manually put the service provider in their app.php file.

 

Step 03: Service Provider

Make service provider inside src directory. In my side, I have created MyPackageServiceProvider.php in the src folder.

<?php namespace Laravelarticle\MyPackage;

use Illuminate\Support\ServiceProvider;

class MyPackageServiceProvider extends ServiceProvider
{

    public function boot()
    {
        
    }

    
    public function register()
    {
        $this->app->make('LaravelArticle\MyPackage\MyPackage');
    }

} 

 

Step 04: Make package class

<?php

namespace LaravelArticle\MyPackage;

class MyPackage
{
    public static function hi(){
        echo "I am your first laravel package";
    }

}

Done! our first laravel package now ready to use.

 

How to use my package in laravel project?

Open your project composer.json file put the package location the psr-4 section.

"psr-4": {
    "LaravelArticle\\MyPackage\\": "packages/laravelarticle/my-pacakge/src"
}

Now run composer dumpautoload

Now we can use our package in our project like.

<?php

use LaravelArticle\MyPacakge\MyPackage;

Route::get('test-pacakge',function(){
   
   MyPacakge::hi();
   // output: I am your first laravel package

}); 

 

How to install my package with composer command? 

Now you are thinking about how you can install your package like composer require laravelarticle/my-package. Don't worry I am telling the process.

  • Add your package in GitHub account with a repository.
  • Release your initial version v1.0.0
  • Create an account in https://packagist.org/
  • Click on submit button in packagist account.
  • In submit package form, enter the GitHub repository URL and submit.

Wait minimum 1 day after submit your package in packagist for available in the composer search index. After that, you can install your won project composer require command.

Hope this post will help you to make your first laravel package. If you think this is helpful then please share with others.

 

 


Share


Related Post


Laravel Simple Captcha

Laravel Livewire - run php like javascript!

Laravel Log Reader

Laravel Airlock - API authentication

Laravel Dusk - automatic app testing package