How to Create Interface in Laravel 11

In this article, we'll create an interface in laravel 11. In laravel 11 introduced new Artisan commands. An interface in programming acts like a contract, defining a set of methods that a class must implement.  Put simply, it ensures that different classes share common behaviors, promoting consistency and interoperability within the codebase.

So, let's see laravel 11 creates an interface, what is an interface in laravel, laravel creates an interface command, and php artisan make interface.

Laravel 11 new artisan command to create an interface.

php artisan make:interface {interfaceName}

 

Laravel Create Interface:

First, we'll create ArticleInterface using the following command.

php artisan make:interface Interfaces/ArticleInterface

Next, we will define the publishArticle() and getArticleDetails() functions in the ArticleInterface.php file. So, let's update the following code in the ArticleInterface.php file.

app/Interfaces/ArticleInterface.php

<?php
  
namespace App\Interfaces;
  
interface ArticleInterface
{
    public function publishArticle($title, $content);
    public function getArticleDetails($articleId);
}

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

php artisan make:class Services/ShopifyService
php artisan make:class Services/LaravelService

app/Services/ShopifyService.php

<?php
  
namespace App\Services;
  
use App\Interfaces\ArticleInterface;
   
class ShopifyService implements ArticleInterface
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function publishArticle($title, $content) {
        info("Publish article on Shopify");
    }
      
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function getArticleDetails($articleId) {
        info("Get Article details from Shopify");
    }
}

app/Services/LaravelService.php

<?php
  
namespace App\Services;
  
use App\Interfaces\ArticleInterface;
   
class LaravelService implements ArticleInterface
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function publishArticle($title, $content) {
        info("Publish article on Laravel");
    }
      
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function getArticleDetails($articleId) {
        info("Get Article details from Laravel");
    }
}

Now, we'll create a controller using the following command and implement the service into the controller.

php artisan make:controller ShopifyArticleController
php artisan make:controller LaravelArticleController

app/Http/Controllers/ShopifyArticleController.php

<?php
  
namespace App\Http\Controllers;
   
use Illuminate\Http\Request;
use App\Services\ShopifyService;
   
class ShopifyArticleController extends Controller
{
    protected $shopifyService;
      
    /**
     * Constructor to inject ShopifyService instance
     *
     * @param ShopifyService $shopifyService
     * @return void
     */
    public function __construct(ShopifyService $shopifyService) {
        $this->shopifyService = $shopifyService;
    }
      
    /**
     * Publish an article
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(Request $request) {
          
        $this->shopifyService->publishArticle('This is title.', 'This is body.');
           
        return response()->json(['message' => 'Article published successfully']);
    }
}

app/Http/Controllers/LaravelArticleController.php

<?php
  
namespace App\Http\Controllers;
   
use Illuminate\Http\Request;
use App\Services\LaravelService;
   
class LaravelArticleController extends Controller
{
    protected $laravelService;
      
    /**
     * Constructor to inject ShopifyService instance
     *
     * @param LaravelService $laravelService
     * @return void
     */
    public function __construct(LaravelService $laravelService) {
        $this->laravelService = $laravelService;
    }
      
    /**
     * Publish an article
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(Request $request) {
          
        $this->laravelService->publishArticle('This is title.', 'This is body.');
           
        return response()->json(['message' => 'Article published successfully']);
    }
}

After that, we'll define routes in the web.php file

routes/web.php

<?php
  
use Illuminate\Support\Facades\Route;
  
use App\Http\Controllers\ShopifyArticleController;
use App\Http\Controllers\LaravelArticleController;
  
Route::get('shofipy/post', [ShopifyArticleController::class, 'index']);
Route::get('laravel/post', [LaravelArticleController::class, 'index']);

 

You might also like:

techsolutionstuff

Techsolutionstuff | The Complete Guide

I'm a software engineer and the founder of techsolutionstuff.com. Hailing from India, I craft articles, tutorials, tricks, and tips to aid developers. Explore Laravel, PHP, MySQL, jQuery, Bootstrap, Node.js, Vue.js, and AngularJS in our tech stack.

RECOMMENDED POSTS

FEATURE POSTS