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.
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
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.
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');
}
}
<?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.
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
});
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.
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.