1

コントローラーでのリポジトリー/インターフェースの使用に問題があります。私のアプリは Laravel 4 を使用しています。

私の現在のコントローラー継承ツリーは次のとおりです。

 +- BaseController
     +- FrontendController
         +- ProductController

ではFrontendController、コントローラーでボード全体で使用するものを取得/設定しているため、コンストラクターでインターフェイスを次のように設定しました。

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

ただし、これは、次のように、すべての子コントローラーでインターフェイスを介して (再度) 送信する必要があることを意味します。

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

私はこのレベル/領域の PHP に慣れていませんが、間違っているように感じます。明らかな何かが欠けていますか?

4

1 に答える 1

1

いいえ、あなたは間違っていません。PHP は、他の言語のようにメソッドのオーバーロードをサポートしていません。FrontendControllerそのため、毎回コンストラクターを書き直す必要があります(仲間のヒント: 優れた IDE は、ここで大いに役立つはずです ;>)。Laravel は、その IoC-Container を介してすべてのコントローラー コンストラクターの依存関係を解決します。追加するだけ

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

アプリケーションのブートストラップ ファイルの 1 つにあるすべてのリポジトリに対して。フレームワークがインジェクションを行います。

于 2013-08-02T05:23:06.437 に答える