The Service Layer is a design pattern that will help you to abstract your logic when you need to use different front-end on your application, for your domain logic
Laravel Service Layer is a package for Laravel 5 which is used to abstract the bussiness logic in service layer. This makes applications much easier to maintain., (*1)
Run the following command from you terminal:, (*2)
bash
composer require kesmenenver/servicelayer
, (*3)
or add this to require section in your composer.json file:, (*4)
"kesmenenver/servicelayer": "dev-master"
, (*5)
then run composer update
, (*6)
Imagine an application where users create products. You can't do this process in contoller because controllers have to be glue code for all layers so you need a service layer for that. It's easy with this package., (*7)
First, (*8)
php artisan make:service CreateProduct ``` This command will created one interface and one class in app/Services folder. You have to implement make() method in ```app/services/CreateProduct```. We will use Interface for DI in controller later. ```app/services/CreateProduct``` looks like this. ```php <?php namespace App\Services; use App\Services\Contracts\CreateProductServiceInterface; class CreateProductService implements CreateProductServiceInterface { public function make(array $request) { // TODO: Implement make() method. // put all the logic in this class } }
Wee need to implement make()
method. All your logic must be in your make()
methods.
By implementing make()
method you telling what does this service do., (*9)
Now, implement make()
method:
```php
<?php, (*10)
namespace App\Services; use App\Services\Contracts\CreateProductServiceInterface;, (*11)
class CreateProductService implements CreateProductServiceInterface { public function make(array $request) { $product = \App\Product::create([ 'name' => $request['name'], 'amount' => $request['amount'], 'quantity' => $request['quantity'] ]); return $product; } }, (*12)
And finally, use the service in the controller: ```php public function store(CreateProductRequest $request, CreateProductServiceInterface $createProductService) { $product = $createProductService->make($request->toArray()); return response()->json($product); }
We use Interface in controller for DI. Laravel needs to know where this interface is implemented. Just add this code snippet to the register()
method in the /app/Providers/AppServiceProvider.php
class., (*13)
$this->app->bind( 'App\Services\Contracts\CreateProductServiceInterface', 'App\Services\CreateProductService' );
Consider creating products in different locations. The only thing you need to do is maintain your services., (*14)