Laravelでクリーンアーキテクチャを取り入れた実装のサンプルコード

Laravel

このサンプルでは、ユーザー情報を取得する機能を実装しています

ディレクトリ構造

app
├── Console
├── Entities
│   └── User.php
├── Exceptions
├── Http
│   ├── Controllers
│   │   └── UserController.php
│   ├── Middleware
│   └── Requests
├── Providers
├── Repositories
│   ├── Contracts
│   │   └── UserRepositoryInterface.php
│   └── Eloquent
│       └── UserRepository.php
└── UseCases
    └── GetUser
        ├── GetUserRequest.php
        ├── GetUserResponse.php
        └── GetUserUseCase.php

<?php

namespace App\Entities;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    protected $fillable = [
        'name',
        'email',
    ];
}

<?php

namespace App\Repositories\Contracts;

use App\Entities\User;

interface UserRepositoryInterface
{
    public function find(int $id): ?User;
}

<?php

namespace App\Repositories\Eloquent;

use App\Entities\User;
use App\Repositories\Contracts\UserRepositoryInterface;

class UserRepository implements UserRepositoryInterface
{
    public function find(int $id): ?User
    {
        return User::find($id);
    }
}

<?php

namespace App\UseCases\GetUser;

use App\Entities\User;

class GetUserResponse
{
    public User $user;

    public function __construct(User $user)
    {
        $this->user = $user;
    }
}

<?php

namespace App\UseCases\GetUser;

use App\Repositories\Contracts\UserRepositoryInterface;

class GetUserUseCase
{
    private UserRepositoryInterface $userRepository;

    public function __construct(UserRepositoryInterface $userRepository)
    {
        $this->userRepository = $userRepository;
    }

    public function handle(GetUserRequest $request): GetUserResponse
    {
        $user = $this->userRepository->find($request->id);
        return new GetUserResponse($user);
    }
}

<?php

namespace App\Http\Controllers;

use App\UseCases\GetUser\GetUserRequest;
use App\UseCases\GetUser\GetUserUseCase;
use Illuminate\Http\Request;

class UserController extends Controller
{
    private GetUserUseCase $getUser;

    public function __construct(GetUserUseCase $getUserUseCase)
    {
        $this->getUserUseCase = $getUserUseCase;
    }

    public function show(int $id)
    {
        $getUserRequest = new GetUserRequest($id);
        $getUserResponse = $this->getUserUseCase->handle($getUserRequest);

        return response()->json([
            'user' => $getUserResponse->user,
        ]);
    }
}

<?php

namespace App\Providers;

use App\Repositories\Contracts\UserRepositoryInterface;
use App\Repositories\Eloquent\UserRepository;
use App\UseCases\GetUser\GetUserUseCase;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    public function register()
    {
        $this->app->bind(UserRepositoryInterface::class, UserRepository::class);
        $this->app->bind(GetUserUseCase::class, GetUserUseCase::class);
    }

    public function boot()
    {
        //
    }
}

<?php

use App\Http\Controllers\UserController;
use Illuminate\Support\Facades\Route;

Route::get('/users/{id}', [UserController::class, 'show']);

コメント

タイトルとURLをコピーしました