私は自分の問題について多くの質問を見つけ、すべての解決策を試しました (と思います) が、うまくいきません。おそらく、非常に簡単なものを見落としています。
Laravel 5 を使用しています。リポジトリ パターンを実装しようとしています。Eloquent モデル '\Notos\Kind' があります。
それから私はこのインターフェースを持っています:
namespace Notos\Contracts;
interface Kind
{
public function findByName($name);
}
私のリポジトリは次のようになります。
namespace Notos\Repository;
use Illuminate\Database\Eloquent\Model;
use Notos\Contracts\Kind as KindContract;
class Kind implements KindContract
{
protected $kind;
public function __construct(Model $kind)
{
$this->kind = $kind;
}
public function findByName($name)
{
return $this->kind->firstOrCreate(array('name' => $name));
}
}
私のサービスプロバイダー:
namespace Notos\Providers;
use Illuminate\Support\ServiceProvider;
use Notos\Kind;
use Notos\Repository\Kind as KindRepository;
class RepoServiceProvider extends ServiceProvider
{
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->app->bind('Notos\Contracts\Kind', function ($app) {
return new KindRepository(new Kind);
});
}
}
そして、リポジトリを使用する私のコントローラー:
namespace Notos\Http\Controllers;
use Notos\Repository\Kind as KindRepository;
class KindController extends Controller
{
protected $kind;
public function __construct(KindRepository $kind)
{
$this->kind = $kind;
}
public function find($name)
{
return $this->kind->findByName($name);
}
}
このプロバイダは config/app.php に登録されています
KindController@find を実行しようとすると、次のエラーが発生します。
BindingResolutionException in Container.php line 785:
Target [Illuminate\Database\Eloquent\Model] is not instantiable.
私が間違っていることを見つけることができません。__construct(Model $kind) を __construct(Kind $kind) に変更すると完璧に動作します。
何か案は?ありがとう