エンジニアのひよこ_level10

毎日更新してた人。たまに記事書きます。

Laravelでリポジトリを書くメモ【397日目】

注意

メモです。

こんなふうにコード書いていきたい(願望)

慣れたらサイト作って、それをgithubに上げたい(願望)

リポジトリ

class XxxRepository implements XxxRepositoryInterface
{
    
}

リポジトリを呼び出すサービス

class XxxService
{
    private $xxx_repository;

    function __construct(XxxRepositoryInterface $xxx_repository)
    {
        $this->xxx_repository = $xxx_repository;
    }
}

XxxRepositoryInterfaceの実装をbind

XxxServiceProvider.php

    $this->app->bind(
        'App\Repository\XxxRepositoryInterface',
        'App\Repository\XxxRepository'
    );

app.phpにサービスプロバイダを登録するのを忘れずに

忘れそうなよくある質問

Q.バインド?なにこれ?

サービスコンテナ 5.3 Laravel

Q.なんでわざわざインターフェースでやるん?

あとで、インターフェースを使ってテストコードかけるじゃろ。Stub書けるじゃろ。