> For the complete documentation index, see [llms.txt](https://liseen315.gitbook.io/laravel/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://liseen315.gitbook.io/laravel/rong-qi/bang-ding-lei-dao-rong-qi/shang-xia-wen-bang-ding.md).

# 上下文绑定

有时需要根据上下文更改如何解析接口。 你可能希望将log事件从一个地方记录到本地syslog，从其他地方记录到外部服务。 所以，让我们告诉容器如何区分 - 查看例11-11。

{% code title="Example 11-11. Contextual binding" %}

```php
// In a service provider
public function register()
{
    $this->app->when(FileWrangler::class)
        ->needs(Interfaces\Logger::class)
        ->give(Loggers\Syslog::class);
    $this->app->when(Jobs\SendWelcomeEmail::class)
        ->needs(Interfaces\Logger::class)
        ->give(Loggers\PaperTrail::class);
}
```

{% endcode %}
