Bagisto Forum

    Bagisto

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups

    Create a service class

    General Discussion
    3
    3
    159
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • K
      kninh last edited by

      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.

      1 Reply Last reply Reply Quote 0
      • Rishabh-Webkul
        Rishabh-Webkul last edited by

        Hello @kninh

        Kindly let us know in which version you are trying to add this service class ?

        Regards
        Team Bagisto

        1 Reply Last reply Reply Quote 0
        • ghermans
          ghermans last edited by ghermans

          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();
                  
              }
          }
          

          Kind regards,
          Glenn Hermans

          Manager Bagisto Europe
          info@bagisto.eu

          1 Reply Last reply Reply Quote 0
          • First post
            Last post