Laravel - 通过 app->bind 将参数传递给模型的构造函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21746687/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel - pass parameters through app->bind to model's constructor
提问by Omranic
Well, code describes it all. I've an Entity Service Provider, which pass an instance of Playlist Model, which supposed to get an array as it's first constructor parameter. How to pass that parameter through app->bind? Knowing that EntityServiceProvider is automagically injected when referenced in the controller.
好吧,代码描述了这一切。我有一个实体服务提供者,它传递一个播放列表模型的实例,它应该获取一个数组作为它的第一个构造函数参数。如何通过 app->bind 传递该参数?知道 EntityServiceProvider 在控制器中引用时自动注入。
// Current Code
/**
* Playlist Entity
*
* @return Playlist\PlaylistEntity
*/
$this->app->bind('Playlist\PlaylistEntity', function($app)
{
return new PlaylistEntity($app->make('Playlist\PlaylistRepositoryInterface'));
});
// Should be something like this:
/**
* Playlist Entity
*
* @return Playlist\PlaylistEntity
*/
$this->app->bind('Playlist\PlaylistEntity', function($app)
{
return new PlaylistEntity($app->make('Playlist\PlaylistRepositoryInterface', $parameters));
});
Similar case: Laravel 4: Passing data from make to the service provider
回答by Dustin Graham
Alex Russell's comment works for me as well.
Alex Russell 的评论也适用于我。
The answer in the 'similar case' post is correct as far as I can tell.
$this->app->bind('Whatever', function ($app, $params) { var_dump($params); });
followed byApp::make('Whatever', [1, 2, 3]);
var_dumps the[1, 2, 3]
array for me.
据我所知,“类似案例”帖子中的答案是正确的。
$this->app->bind('Whatever', function ($app, $params) { var_dump($params); });
其次是App::make('Whatever', [1, 2, 3]);
var_dumps[1, 2, 3]
数组给我。
回答by windsor
In Laravel 5.4 the ability to pass config parameters when resolving from the container using App::make() was removed and subsequently re-implemented as App::makeWith().
在 Laravel 5.4 中,当使用 App::make() 从容器解析时传递配置参数的能力被移除,随后重新实现为 App::makeWith()。
BTW I tried to make this a comment on the previous answer, but it didn't let me. Maybe due to not enough experience points?
顺便说一句,我试图对上一个答案发表评论,但它没有让我这样做。可能是因为经验值不够?
回答by Thomas Praxl
Thanks @yevgeniy-afanasyev for pointing out the problems when mocking. If you need to mock these instances, you could go for Taylor Otwell's early suggestion here: https://github.com/laravel/ideas/issues/391#issuecomment-285197048
感谢@yevgeniy-afanasyev 在嘲笑时指出问题。如果您需要模拟这些实例,您可以在这里寻求 Taylor Otwell 的早期建议:https: //github.com/laravel/ideas/issues/391#issuecomment-285197048
I just needed that and it worked well. Just return a closure and call it, when you ::make
.
我只是需要那个,而且效果很好。只需返回一个闭包并调用它,当你::make
.
// Service Provider
$this->app->bind(MyClass::class, function ($app) {
return function($param) : MyClass
{
return new MyClass($param);
}
}
// ::make
$myInstance = App::make(MyClass::class)($myParameter);
// mock
$myMock = Mockery::mock(new MyClass($myParameter));
$this->instance(MyClass::class, function($param) use ($myMock) { return $myMock; });