In Laravel application, you have noticed different ServiceProviders class in app/Providers. Service Providers are the central points of all Laravel application bootstrapping. In general, bootstrapping means registering all routes, middlewares, events etc.
If you have seen providers class, these are extending to Illuminate\Support\ServiceProvider
class. Most service providers contains register and boot method.
In this article. we will go through how to write your own service providers and register them with your Laravel application.
Run the below artisan command in Terminal to create a new provider.
php artisan make:provider SiteServiceProvider
This will create new provider class at app/Providers/SiteServiceProvider.php
file.
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class SiteServiceProvider extends ServiceProvider
{
/**
* Register services.
*
* @return void
*/
public function register()
{
//
}
/**
* Bootstrap services.
*
* @return void
*/
public function boot()
{
//
}
}
As we have new created provider class, but the application doesn't know about it. So we need to tell application about new provider. If you see config/app.php
file, you will see $providers
array with list of all provider class. We have to add new provider class in this array.
<?php
return [
'providers' => [
...
App\Providers\SiteServiceProvider::class,
...
]
]
The class generally includes two methods, register method and boot method. In register method, we can implementation of App\Repo\SiteRepo
in the service container. In the register method, you will always have access of $app property which provides access for service container.
<?php
namespace App\Providers;
use App\Repo\SiteRepo;
use Illuminate\Support\ServiceProvider;
class SiteServiceProvider extends ServiceProvider
{
/**
* Register services.
*
* @return void
*/
public function register()
{
$this->app->bind('SiteRepo', function($app) {
return new SiteRepo();
});
}
}
So we have registered SiteRepo class, so now we can use this class. So we need to create this class. A details method will return server details for the website.
<?php
namespace App\Repo;
use App\Repo\SiteRepo;
class SiteRepo
{
/**
* return site details
*
* @return void
*/
public function details()
{
return [
'server' => 'AWS',
'type' => 'dedicated',
'disk' => '1250Mb',
];
}
}
So we can now use SiteRepo
class in controller file like this.
<?php
namespace App\Http\Controllers;
use App\Repo\SiteRepo;
use App\Http\Controllers\Controller;
class ServerController extends Controller
{
public function index(SiteRepo $siterepo)
{
$server_details = $siterepo->details();
dd($server_details);
}
}
You should have created route in routes/web.php
file to access details.
<?php
use App\Http\Controllers\ServerController;
Route::get('server-details', [ServerController::class, 'index'])->name('index');
So now when you visit this route, you will get server details as defined in SiteRepo class. This was basic knowledge on how to create and register custom provider in Laravel application.
Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]
Run Two Skype Account in Ubuntu System
In this article, i will share with you h...PHP Parse and Process XML data
Sometimes working with 3rd party API, yo...How to Verify Aadhar Card Number without any API?
In this article, i will share with you h...How to get the text inside an element using jQuery
Use the jQuery text() method...Laravel 5.5 - Login With Only Mobile Number Using Laravel Custom Auth
Today, we are share with you how to modi...