umutcankarce
app/Services/ProductServiceInterface.php
interface ProductServiceInterface
{
public function productVariantSave(array $data): Variant;
public function productMultiImageSave();
}
app/Services/ProductService.php
class ProductService implements ProductServiceInterface
{
public function productVariantSave(array $data): Variant
{
//...
}
public function productMultiImageSave()
{
//...
}
}
AppServiceProvider::register() yöntemi içinde:
// ProductServiceInterface çözümlendiğinde ProductService ver:
$this->app
->bind(ProductServiceInterface::class, ProductService::class);
Mesela artık controller içinde şunu yapabilirsiniz:
class ProductController extends Controller
{
public function __construct(
protected ProductServiceInterface $productService,
)
{}
public function create(StoreProductRequest $request)
{
$variant = $this->productService
->productVariantSave($request->validated());
//...
}
}
ya da enjekte etmeden şu şekilde çözümleyebilirsiniz:
$variant = resolve(ProductServiceInterface::class)->productVariantSave($data);
https://laravel.com/docs/10.x/container