Reputation: 1269
I have gone through the Laravel documentation and found that every request follows the Middle layer -> Controller layer -> Resource Layer flow. But for my project I have a huge business processing logic which needs to be written. So, I am looking for a service layer option where execution control will be passed from Controller and then service layer will do the processing logic along with database fetch. But I didn't find anything linked with service layer part in artisan.
So, can you suggest me how can I implement a service layer in my project?
Upvotes: 3
Views: 4607
Reputation: 1031
What about creating a Services
folder under app/
, and use Controllers dependency injections?
It would be something like this:
MyService.php
<?php
namespace App\Services;
use App\Models\Bar;
class MyService
{
public function foo(Bar $bar)
{
// do things
}
}
MyController.php
<?php
namespace App\Http\Controllers;
use App\Services\MyService;
use App\Models\Bar;
class MyController extends Controller
{
protected $myService;
public function __construct(MyService $myService)
{
$this->myService = $myService;
}
public function handleRequest(Bar $bar)
{
$this->myService->foo($bar);
}
}
Upvotes: 16