Singleton
In laravel we usually use IoC to build our application, when we call one Class, all dependency in this class will involed.
For testing this class, we can defined depencency in service container as singleton.
Create a service provider:
php artisan make:provider WeatherServiceProvider
In here, using service container register a singleton for decoupling
Prepare a singleton
WeatherServiceProvider.php
<?php
namespace App\Providers;
use App\Http\Services\WeatherServices;
use Illuminate\Support\ServiceProvider;
class WeatherServiceProvider extends ServiceProvider
{
/**
* Register services.
*
* @return void
*/
public function register()
{
$this->app->singleton(WeatherServices::class, function($app) : weatherservices {
return new WeatherServices('defalt from singleton');
});
}
/**
* Bootstrap services.
*
* @return void
*/
public function boot()
{
//
}
}
Register your provider on config/app.php
Continue Reading