Let’s implement profile registration using domain driven design in Laravel

Aug 23, 2020 PHP Laravel DDD domain driven design

#DDD flow for implementing profile registration

  1. Create Controller
  2. Create Model (Eloquent)
  3. Create View
  4. Edit Eloquent
  5. Create repository
  6. Register with Provider
  7. Create UseCase 8、Create Request 9, edit the Controller 10, edit the routing

Let’s do it~

##1, create Controller

php artisan make:controller ProfileController

##2, create a model (also create a migration file)

php artisan make:model Profile --migration

■ migrantion file

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateProfilesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('profiles', function (Blueprint $table) {
            $table->id();
            $table->string('nick_name');
            $table->string('introduce');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('profiles');
    }
}

■ DB migration and create profile table

php artisan migrate

##3, create View

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">Profile</div>

                <div class="card-body">
                    @if (session('status'))
                        <div class="alert alert-success" role="alert">
                            {{ session('status') }}
                        </div>
                    @endif

                </div>
                {{ Form::open() }}
                <div class="form-group row col-12">
                    <p class="d-flex align-items-center col-3">nickname</p>
                    {{Form::input('text','name', ``, ['class' =>'form-controller col-5'])}}
                </div>
                <div class="form-group row col-12">
                    <p class="d-flex align-items-center col-3">Introduction</p>
                    {{Form::textarea('introduce', ``, ['class' =>'form-controller col-5'])}}
                </div>
                {{ Form::close() }}
            </div>
        </div>
    </div>
</div>
@endsection

![Screenshot 2020-08-16 0.30.57.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/684968/e793d25d-e501-a87e-48cc-(fe8d44b25eb8.png)

##4, edit Eloquent

<?php

namespace App\Eloquent;

use Illuminate\Database\Eloquent\Model;

class EloquentProfile extends Model
{
    protected $table ='profiles';

}

##5, create a repository

■ Create Repository Interface

<?php

namespace App\Domain\RepositoryInterface;

use App\Eloquent\EloquentProfile;

interface ProfileRepositoryInterface
{
    /**
     * @param $post
     * @return EloquentProfile
     */
    public function store($post): ?EloquentProfile;

}

■ Create Repository

<?php

namespace App\Domain\Repository;

use App\Eloquent\EloquentProfile;
use App\Domain\RepositoryInterface\ProfileRepositoryInterface;

class ProfileRepository implements ProfileRepositoryInterface
{
 
  public function store($post): EloquentProfile
  {
      $eloquentProfile = EloquentProfile::findOrNew($post['id']);
      $eloquentProfile -> nick_name = $post['nick_name'];
      $eloquentProfile -> introduce = $post['introduce'];

      $eloquentProfile -> save();

      return $eloquentProfile;


  }
}

##6, register with Provider

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        $this->app->bind(
            \App\Domain\Repository\ProfileRepository::class,
            \App\Domain\RepositoryInterface\ProfileRepositoryInterface::class
        );
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
    
    }
}


##7, create UseCase


<?php

namespace App\Domain\UseCase\Profile;

use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use App\Domain\Model\Profile;
use App\Domain\Repository\ProfileRepository;
use App\Eloquent\EloquentProfile;

class RegisterProfile{

    private $profileRepo;

    public function __construct(ProfileRepository $profileRepo){
        $this->profileRepo = $profileRepo;
    }

    public function __invoke($post)
    {
        $post['id'] = 0;

        return $this->profileRepo->store($post);
    }

}

##8、Create Request```php:app/Http/Request/ProfileRequest.php

'required|string|max:255', 'introduce' =>'required|string|max:255', ]; } } ``` ##9、Controllerを編集する ```php:app/Domain/UseCase/registerProfile.php all()); return redirect()->route('profile.result'); } } ``` ##10、ルーティングを編集する ```php:web.php name('profile.edit'); Route::post('/profile/register', 'ProfileController@register')->name('profile.register'); Route::view('/profile/result', 'profile.result')->name('profile.result'); ``` #うごいた!!! ![スクリーンショット 2020-08-23 15.06.12.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/684968/bcfc5b6f-56fb-527c-d30c-cdf2b969d4e7.png) ###参考 [Laravel5を使ってドメイン駆動設計で作るサンプルアプリ。](https://qiita.com/niiyz/items/d80a9ffdfad3b431dcfd) [Laravelでドメイン駆動設計(DDD)を実践し、Eloquent Model依存の設計から脱却する](https://qiita.com/mejileben/items/302a9f502ca0801b1efb)