2013-08-01 7 views
1

컨트롤러에서 리포지토리/인터페이스를 사용하는 데 문제가 있습니다. 내 응용 프로그램은 Laravel 4Laravel 4에서 컨트롤러 상속이있는 리포지토리/인터페이스를 처리하는 방법은 무엇입니까?

나의 현재 컨트롤러 상속 트리를 사용하는 것은 다음 FrontendController에서

+- BaseController 
    +- FrontendController 
     +- ProductController 

나는 나의 컨트롤러에서 전반적으로 사용하는 몇 가지 설정/얻고, 그래서 인터페이스 설정 그래서 같은 생성자에서 :

그러나

class FrontendController extends BaseController 
{ 
    /** 
    * Constructor 
    */ 
    public function __construct(SystemRepositoryInterface $system, 
           BrandRepositoryInterface $brands, 
           CategoryRepositoryInterface $categories) 

이 지금은 그렇게처럼 내 아이의 모든 컨트롤러에 (다시) 인터페이스를 통해 전송하는 데 의미3210

class ProductController extends FrontendController 
{ 
    /** 
    * Constructor 
    */ 
    public function __construct(SystemRepositoryInterface $system, 
           BrandRepositoryInterface $brands, 
           CategoryRepositoryInterface $categories, 
           ProductRepositoryInterface $products) 
    { 
     parent::__construct($system, $brands, $categories); 

필자는이 수준/PHP 영역을 처음 사용했지만 잘못된 느낌이 들었습니다. 분명히 분명한 사실이 있습니까?

+1

IoC 컨테이너를 [자동 해결] (http://laravel.com/docs/ioc#automatic-resolution) –

+0

과 함께 사용하는 것이 좋습니다. 'ProductRepositoryInterface '를 FrontendController 생성자로 보내고 ProductController에서 생성자를 건너 뛰겠습니까? –

+0

@RobGordijn 기술적으로 당신이'FrontendController'를 통해 모든 저장소를 '미리로드'할 수 있기 때문에 궁금합니다. –

답변

1

아니요, 틀리지 않습니다. PHP는 다른 언어와 마찬가지로 메소드 오버로딩을 지원하지 않습니다. 따라서 항상 매번 FrontendController의 생성자를 다시 작성해야합니다 (Bro 팁 : 좋은 IDE가 도움이 될 것입니다.>). Laravel은 IoC-Container를 통해 모든 컨트롤러 생성자 종속성을 해결합니다. 애플리케이션의 부트 스트랩 파일 중 하나에있는 모든 저장소에

App::bind('SystemRepositoryInterface', function() { 
    return new EloquentSystemRepository(); 
}); 

을 추가하면됩니다. 그리고 프레임 워크는 당신을 위해 주사를 할 것입니다.

+0

PHPStorm을 열어야 할 수도 있습니다! 나는 인터페이스를 나의 저장소에 바인딩하고있다 :'App :: bind ('\ Models \ Interfaces \ ProductRepositoryInterface', '\ Models \ Repositories \ DbProductRepository'); '- 내가보기에 당신이 보여준 것과 비슷하다고 생각합니까? –

+0

그래, 거의 같지만 닫는 방법은 그다지 반영 할 필요가 없기 때문에 약간 더 효과적이다. –

관련 문제