This guide shows how to create services on the system.
- Create the service on app/Services, for example, create the file HistoryService.php
<?php
namespace App\Services;
class HistoryService
{
public function foo() {
}
}
- Create the Service Provider in app/Providers, for example:
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Services\HistoryService;
class HistoryServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
//
}
/**
* Register the application services.
*
* @return void
*/
public function register()
{
$this->app->bind('HistoryService', function($app)
{
return new HistoryService();
});
}
}
- Create the Facade to access that service in app/Facades, for example:
<?php
namespace App\Facades;
use Illuminate\Support\Facades\Facade;
class HistoryFacade extends Facade
{
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
{
return 'HistoryService';
}
}
- Register the service provider on config/app.php, for example:
<?php
return [
// ... a lot of things
'providers' => [
// ... a lot of things
App\Providers\HistoryServiceProvider::class,
],
// ... a lot of things
'aliases' => [
// ... a lot of things
'HistoryService' => App\Facades\HistoryFacade::class,
]
]
<?php
namespace App;
use App\Facades\HistoryFacade; // Important!
class Something {
public function bar() {
HistoryFacade::foo(); // Important!
}
}