@eyad mohammed osama You only need to register services in a service provider if your service classes require anything in their constructors. If not, then you can just type-hint your service classes and Laravel will instantiate them as normal.
Services & Providers - Do i need to create a provider for each service ?
Greetings to all members
I define a service to be a class that belongs to the namespace App\Services and can be found inside the directory App/Services, and their main objective is to handle the business logic so that i don't have to crowd the controllers
However, i'm type hinting those services when needed in method parameters, and using a service provider for each service to do the injection for me, for example the following code defines App\Providers\NewsServiceProvider :
<?php
namespace App\Providers;
use App\Http\Controllers\Admin\NewsController as AdminNewsController;
use App\Http\Controllers\API\V1\NewsController as APINewsController;
use App\Http\Controllers\Website\NewsController as WebsiteNewsController;
use App\Services\NewsService;
use Illuminate\Support\ServiceProvider;
class NewsServiceProvider extends ServiceProvider
{
/**
* Register services.
*
* @return void
*/
public function register()
{
//
}
/**
* Bootstrap services.
*
* @return void
*/
public function boot()
{
$this->app->when([WebsiteNewsController::class, AdminNewsController::class, APINewsController::class])
->needs(NewsService::class)
->give(function () {
return new NewsService();
});
}
}
Is it a good idea to follow this practice ?, that is to create a separate service provider for each service ?
Any guidance on this topic would be highly appreciated
Please or to participate in this conversation.