sch*_*rht 5 ioc-container laravel laravel-5
我在接口和实现之间的服务提供程序中使用绑定:
public function register()
{
$this->app->bind('MyInterface', MyImplementation::class);
}
Run Code Online (Sandbox Code Playgroud)
在我的中间件中,我向请求添加了一个属性:
public function handle($request, Closure $next)
{
$request->attributes->add(['foo' => 'bar]);
return $next($request);
}
Run Code Online (Sandbox Code Playgroud)
foo我的服务提供商public function register()
{
$this->app->bind('MyInterface', new MyImplementation($this->request->attributes->get('foo')); // Request is not available
}
Run Code Online (Sandbox Code Playgroud)
如果设置了request-> attributes-> get('foo'),我正在寻找一种"重新绑定"的技术
Fil*_*ski 17
试试这样:
public function register()
{
$this->app->bind('MyInterface', function () {
$request = app(\Illuminate\Http\Request::class);
return app(MyImplementation::class, [$request->foo]);
}
}
Run Code Online (Sandbox Code Playgroud)
绑定元素的工作原理是只有在调用它们时才会触发它们.
在service provider您还可以Request Object通过以下方式访问:
public function register()
{
$request = $this->app->request;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6664 次 |
| 最近记录: |