Upgrade to Pro — share decks privately, control downloads, hide ads and more …

実運用におけるLaravelとNuxtでのRepositoryのレイヤ分割の話

Shohei Kondo
January 31, 2019

 実運用におけるLaravelとNuxtでのRepositoryのレイヤ分割の話

Laravel/Vue.js勉強会#7 資料

Twitter: https://twitter.com/korn_shonery
Blog: https://blog.kon-shou.com/

Shohei Kondo

January 31, 2019
Tweet

More Decks by Shohei Kondo

Other Decks in Programming

Transcript

  1. ⾃⼰&会社紹介 ⾃⼰ Laravel: 1 年7 ヶ⽉ Nuxt: 11 ヶ⽉ 会社

    会社名: オープンロジ 業種: 物流アウトソーシング Laravel+react x1 Laravel+Nuxt.js x2
  2. pages/warehousings/_id.vue pages ├── home ├── items │ └── _id ├──

    schedules │ └── warehousings │ └── _id │ └── fix ├── shippings │ └── _id └── warehousings └── _id ブラウザから https://openlogi.web.example.com/warehousings/${warehousingId} にアクセス
  3. warehousings/_id/index のasyncData() が呼ばれる async asyncData({ app, params, query }) {

    const warehousingRepository = app.$serviceProvider.get('WarehousingRepository'); const warehousingId = _.get(params, 'id'); const warehousing = await warehousingRepository.find(warehousingId); return { warehousing, }; } 真っ先に呼ばれる asyncData() で warehousingRepository. nd(warehousingId) が実⾏される
  4. warehousingRepository.find() async find(warehousingId): Promise<Warehousing> { const response = await this.axios.get(`/warehousings/${warehousingId}`);

    return new Warehousing(response.data); } nuxt.con g.js で this.axios.get をapi サーバーに向けている => https://openlogi.api.example.com/warehousings/${warehousingId} が叩かれる
  5. H p/Controllers/WarehousingController routing を経て show() に到達 public function show($warehousingId): Warehousing

    { $warehousing = $this->warehousingRepository->find($warehousingId); if (!$warehousing) { throw new HttpNotFoundException(' 指定された⼊荷が⾒つかりませんでした'); } return $warehousing; } Laravel 側でも、再度 warehousingRepository を呼ぶ
  6. En tyPresenters/Domains/Warehousing/ WarehousingPresenter public function toApi($warehousing): array { $array =

    $warehousing->toArray(); $array['warehousing_items'] = $warehousing->warehousingItems ->map(function (WarehousingItem $warehousingItem) { return $this->warehousingItemPresenter->toApi($warehousingItem); } ); return $array; } 帰ってきたモデルをいい感じに 整形 する
  7. 再掲 warehousingRepository.find() async find(warehousingId): Promise<Warehousing> { const response = await

    this.axios.get(`/warehousings/${warehousingId}`); return new Warehousing(response.data); } Laravel の WarehousingPresenter で整形したデータ (=response.data) からモデルを作る => そのモデルを pages/warehousings/_id/index に返す
  8. 再掲 warehousings/_id/index.vue async asyncData({ app, params, query }) { const

    warehousingRepository = app.$serviceProvider.get('WarehousingRepository'); const warehousingId = _.get(params, 'id'); const warehousing = await warehousingRepository.find(warehousingId); return { warehousing, }; } ここで帰るwarehousing を template タグ内で描写する。完!