我在控制器中使用存储库/接口时遇到问题。我的应用程序正在使用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这个级别/领域的新手,但感觉不对,我是不是错过了一些明显的东西?
FrontendController
的构造函数(Bro提示:一个好的IDE应该在这里对您有很大帮助;>)。Laravel通过其IoC容器解析所有控制器构造函数依赖关系。只需添加
App::bind('SystemRepositoryInterface', function() {
return new EloquentSystemRepository();
});
对于应用程序的某个引导文件中的每个存储库。该框架将为您进行注入。