How To Create Custom Service Provider In Laravel
Teams applying “How To Create Custom Service Provider In Laravel” in a production project can use hr department one to record implementation, review and debugging time, then compare that effort with tests and shipped functionality instead of treating activity as performance by itself.
Before copying the example into a live application, confirm version-specific behaviour with the official Laravel website and test it against the project’s actual database and runtime.
Hi guys,
Today i will explained to the how to create custom service provider in laravel.The laravel custom service provider tutorial is so easy to use.so you can just follow my step by step and learn laravel custom getter tutorial.
So let's start to the example and follow to the my all step.
It looks great, doesn’t it? There is no charm here, you can look at the contents of file `config/app.php`. You’ll find an array which used to declare all service providers, These service providers will be loaded during the bootstrapping of the Laravel application.
Solution
config/app.php
'providers' => [
/*
* Laravel Framework Service Providers...
*/
Illuminate\Auth\AuthServiceProvider::class,
Illuminate\Broadcasting\BroadcastServiceProvider::class,
Illuminate\Bus\BusServiceProvider::class,
Illuminate\Cache\CacheServiceProvider::class,
Illuminate\Foundation\Providers\ConsoleSupportServiceProvider::class,
Illuminate\Cookie\CookieServiceProvider::class,
Illuminate\Database\DatabaseServiceProvider::class,
Illuminate\Encryption\EncryptionServiceProvider::class,
Illuminate\Filesystem\FilesystemServiceProvider::class,
Illuminate\Foundation\Providers\FoundationServiceProvider::class,
Illuminate\Hashing\HashServiceProvider::class,
Illuminate\Mail\MailServiceProvider::class,
Illuminate\Notifications\NotificationServiceProvider::class,
Illuminate\Pagination\PaginationServiceProvider::class,
Illuminate\Pipeline\PipelineServiceProvider::class,
Illuminate\Queue\QueueServiceProvider::class,
Illuminate\Redis\RedisServiceProvider::class,
Illuminate\Auth\Passwords\PasswordResetServiceProvider::class,
Illuminate\Session\SessionServiceProvider::class,
Illuminate\Translation\TranslationServiceProvider::class,
Illuminate\Validation\ValidationServiceProvider::class,
Illuminate\View\ViewServiceProvider::class,
/*
* Application Service Providers...
*/
App\Providers\AppServiceProvider::class,
App\Providers\AuthServiceProvider::class,
App\Providers\EventServiceProvider::class,
App\Providers\RouteServiceProvider::class,
/**
* Custom Provider
*/
],
Create service provider
php artisan make:provider AwesomeServiceProvider
All service providers extend the Illuminate\Support\ServiceProvider class. Most service providers contain a register and a boot method. Within the register method, you should only bind things into the service container. You should never attempt to register any event listeners, routes, or any other piece of functionality within the register method.
app/Providers/AwesomeServiceProvider.php
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class AwesomeServiceProvider extends ServiceProvider
{
public function boot()
{
//
}
public function register()
{
//
}
}
Now, we go ahead with an example which uses interface, so let create your interface.
<?php
namespace App\Service;
interface AwesomeServiceInterface
{
public function doAwesomeThing();
}
And the service
<?php
namespace App\Service;
class AwesomeService implements AwesomeServiceInterface
{
public function doAwesomeThing()
{
echo ‘do awesome thing !!!’;
}
}
After that, instead of binding a class, we’ll bind an interface.
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class AwesomeServiceProvider extends ServiceProvider
{
public function boot()
{
//
}
public function register()
{
$this->app->bind(‘App\Service\AwesomeServiceInterface’, ‘App\Service\AwesomeService’);
}
}
Add your service provider into file config/app.php
config/app.php
‘providers’ => [
……
/**
* Custom Provider
*/
App\Providers\AwesomeServiceProvider::class,
],
Let create controller to test your service
app/Http/Controllers/TestController.php
<?php
namespace App\Http\Controllers;
use App\Service\AwesomeServiceInterface;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
class TestController extends Controller
{
public function doAwesome(AwesomeServiceInterface $awesome_service)
{
$awesome_service->doAwesomeThing();
return new Response();
}
}
- How to Notifications With database Driver in Laravel 11?
- How to Send Email Via Notification in Laravel 11?
- How to Install and Configure Laravel 11 Debugbar?
- How to Like Dislike System in Laravel 11?
- How to Paginate a Model with Relationship in Laravel 11?
- Laravel 11 Upload Files to Amazon S3 Example
- Laravel 11 Send WhatsApp Messages Example
- Laravel 11 Rest API Authentication Using JWT Tutorial