Create a service class
-
Hello, I got this error when creating a new service class:
php artisan make:service APIService
ERROR Command "make:service" is not defined.
I follow the instructions at
https://bagisto.com/en/how-to-integrate-third-party-apis-in-laravel/Any advice please? Thanks.
-
Hello @kninh
Kindly let us know in which version you are trying to add this service class ?
Regards
Team Bagisto -
Step 1: Create a New Package
First, create a new package if you haven't already. You can use the Bagisto package generator for this:
php artisan package:make VendorName PackageName
Step 2: Define the Service Class
Navigate to your package directory and create a new directory for services if it doesn't exist:
packages/VendorName/PackageName/src/Services
Step 3: Implement the Service Class
Create a new PHP file for your service class MyService.php and add the following code for example<?php namespace VendorName\PackageName\Services; class MyService { public function performAction() { // Your service logic here } }
Step 4: Use the Service Class
You can now use your service class in your controllers:<?php namespace VendorName\PackageName\Http\Controllers; use VendorName\PackageName\Services\MyService; class MyController extends Controller { protected $myService; public function __construct(MyService $myService) { $this->myService = $myService; } public function index() { $this->myService->performAction(); } }