DEV Community

Techsolutionstuff
Techsolutionstuff

Posted on • Originally published at techsolutionstuff.com

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}
Enter fullscreen mode Exit fullscreen mode

Laravel Create Interface:

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

php artisan make:interface Interfaces/ArticleInterface
Enter fullscreen mode Exit fullscreen mode

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);
}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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");
    }
}
Enter fullscreen mode Exit fullscreen mode

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");
    }
}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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']);
    }
}
Enter fullscreen mode Exit fullscreen mode

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']);
    }
}
Enter fullscreen mode Exit fullscreen mode

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']);
Enter fullscreen mode Exit fullscreen mode

You might also like:

Read Also: How to Get Current URL in Laravel

Top comments (0)