How to Create Interface in Laravel 11?

By Hardik Savani September 4, 2024 Category : Laravel

Hello,

In this post, we will show you how to create an interface in laravel 11 framework.

An interface in programming is like a contract. It defines a set of methods that a class must implement. In simpler terms, it's a way to ensure that different classes have certain common behaviors.

Laravel 11 comes with a slimmer application skeleton. Laravel 11 introduce streamlined application structure, per-second rate limiting, health routing etc.

Laravel 11 added a new artisan command to create a interface. we can use the following command to create a new interface in laravel 11.

php artisan make:interface {interfaceName}

In this example, we will create a "PostInterface" interface with defined "publishPost()" and "getPostDetails()" methods on it. Then, we will create two new services, WordpressService and ShopifyService classes, to publish new posts. Next, we will create two simple routes methods to call the services method. So, let's follow the following steps to complete this example.

Laravel Create Interface Example:

First, you need to run the following command to create the "PostInterface" interface. Let's run it:

php artisan make:interface Interfaces/PostInterface

Next, we will define the publishPost() and getPostDetails() functions in the PostInterface.php file. So, let's update the following code in the PostInterface.php file.

app/Interfaces/PostInterface.php

<?php
  
namespace App\Interfaces;
  
interface PostInterface
{
    public function publishPost($title, $content);
    public function getPostDetails($postId);
}

Next, we will create two new service classes and implement "PostInterface" on them. Run the following commands now:

php artisan make:class Services/WordpressService

php artisan make:class Services/ShopifyService

Now, simply update the following code.

app/Services/WordpressService.php

<?php
  
namespace App\Services;
  
use App\Interfaces\PostInterface;
  
class WordpressService implements PostInterface
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function publishPost($title, $content) {
        info("Publish post in Wordpress");
    }
      
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function getPostDetails($postId) {
        info("Get post details from Wordpress");
    }
}

app/Services/ShopifyService.php

<?php
  
namespace App\Services;
  
use App\Interfaces\PostInterface;
   
class ShopifyService implements PostInterface
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function publishPost($title, $content) {
        info("Publish post in Shopify");
    }
      
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function getPostDetails($postId) {
        info("Get post details from Shopify");
    }
}

Next, we will create two controllers with the index method.

So, let's run the following command:

php artisan make:controller WordpressPostController

php artisan make:controller ShopifyPostController

Now, update the code in that controller:

app/Http/Controllers/WordpressPostController.php

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\Services\WordpressService;
  
class WordpressPostController extends Controller
{
    protected $wordpressService;
    
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function __construct(WordpressService $wordpressService) {
        $this->wordpressService = $wordpressService;
    }
      
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index(Request $request) {
          
        $this->wordpressService->publishPost('This is title.', 'This is body.');
          
        return response()->json(['message' => 'Post published successfully']);
    }
}

app/Http/Controllers/ShopifyPostController.php

<?php
  
namespace App\Http\Controllers;
   
use Illuminate\Http\Request;
use App\Services\ShopifyService;
   
class ShopifyPostController extends Controller
{
    protected $shopifyService;
      
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function __construct(ShopifyService $shopifyService) {
        $this->shopifyService = $shopifyService;
    }
      
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index(Request $request) {
          
        $this->shopifyService->publishPost('This is title.', 'This is body.');
           
        return response()->json(['message' => 'Post published successfully']);
    }
}

Now, we will define two routes to call controller methods. So, let's add them.

routes/web.php

<?php
  
use Illuminate\Support\Facades\Route;
  
use App\Http\Controllers\WordpressPostController;
use App\Http\Controllers\ShopifyPostController;
  
Route::get('/post-wordpress', [WordpressPostController::class, 'index']);
Route::get('/post-shopify', [ShopifyPostController::class, 'index']);

Output:

You will see the following output:

[2024-03-03 17:05:09] local.INFO: Publish post in Wordpress  
[2024-03-03 17:05:14] local.INFO: Publish post in Shopify  

You can write more interfaces and use them.

I hope it can help you...

Shares