2016-04-27 6 views
0

Question1の数:私はのようなコントローラの構造を持っている:Laravelコントローラ構造とコントローラ

-http 
-controllers 
    --admin 
    -controlle_1...n 
    --user 
    -controlle_1...n 
    --Front View 
    -controlle_1...n 

それは大丈夫ですか?良い習慣は何ですか?

質問2:私はadminのために、私はFront Viewのためのそれらのレコードと同じを取得していますUserのためのレコードを挿入していますcountroller CityControllerを持っているとしましょう。だから私はそれらのすべてのための1つのコントローラを持っている必要がありますか私はUserのような別のコントローラを持っている必要がありますAdminFront View

私は電子商取引アプリケーションに取り組んでいますので、私は15個のコントローラを持っています。

いい方法はありますか?

答えて

1

ビジネスロジックをコントローラから分離することをお勧めします。したがって、本質的にビジネスロジックを扱う別のクラスのクラスがあります。

./Service/ItemService.php

namespace Service; 

class ItemService 
{ 
    public function create($name, $description, $active = true) 
    { 
     $itemRepository = new ItemRepository(); 

     $item = $itemRepository->create([ 
      'name' => $name, 
      'description' => $description, 
      'active' => $active, 
     ]); 

     // Create audit log 
     $log = new AuditLogService; 
     $log->create("New product was created", $item); 

     // Update stock 

     // Send notification email, etc. 

     return $item; 
    } 
} 

./Repository/ItemRepository.php

namespace Repository; 

class ItemRepository 
{ 
    public function create($data) 
    { 
     $item = new Item; 
     $item->fill($data); 
     $item->save(); 

     return $item; 
    } 
} 

./Controller/FrontEnd/ItemController.php

namespace Controller\FrontEnd; 

class ItemController 
{ 
    public function store() 
    { 
     // Validate data 

     // Call service to create item 
     $itemService = new ItemService; 
     $item = $itemService->create($request->inputs); 

     // return view response 
    } 
} 

./Controller/Admin/ItemController.php

namespace Controller\Admin; 

class ItemController 
{ 
    public function store() 
    { 
     // Validate data 

     // Call service to create item 
     $itemService = new ItemService; 

     $item = $itemService->create($request->inputs); 

     // return JSON response 
    } 
} 

あなたがコアビジネスロジックを再利用することができ、複数のコントローラを見ることができるように。これは、コードの再利用とメンテナンスの面で大いに役立ちます。

+0

偉大な答え!ありがとうございます – Gammer

+0

@ゲーマーいつでも!また、私は同様のアプリケーションのための基本ライブラリに取り組んでいます。まだWIPですが、より幅広いアイデアを得ることができます。 https://github.com/devrtips/php-soa-library。 – blackpla9ue

+0

素晴らしい...連絡があります。私はどこに連絡することができますか? – Gammer

関連する問題