在 Laravel 文档中,我看到了示例:
$this->app->when(PhotoController::class)
->needs(Filesystem::class)
->give(function () {
return Storage::disk('local');
});
$this->app->when([VideoController::class, UploadController::class])
->needs(Filesystem::class)
->give(function () {
return Storage::disk('s3');
});
https://laravel.com/docs/8.x/container#contextual-binding
我想在存储磁盘中使用不同的日志通道。
https://laravel.com/docs/8.x/logging#writing-to-specific-channels
我尝试:
public function __construct(LoggerInterface $logger) {
$this->logger = $logger;
}
$this->app->when(PhotoController::class)
->needs('log')
->give(function () {
return \Log::channel('telegram');
});
$this->app->when([VideoController::class, UploadController::class])
->needs('log')
->give(function () {
return \Log::channel('slack');
});
但我得到错误:
NOTICE: PHP message: PHP Fatal error: Uncaught Error: Maximum function nesting level of '256' reached, aborting! in /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:792
Stack trace:
#0 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/Container.php(646): Illuminate\Foundation\Application->resolve('Illuminate\\Log\\...')
#1 /var/www/html/app/Providers/AppServiceProvider.php(106): Illuminate\Container\Container->get('Illuminate\\Log\\...')
#2 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/Container.php(805): App\Providers\AppServiceProvider->App\Providers\{closure}(Object(Illuminate\Foundation\Application), Array)
#3 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/Container.php(691): Illuminate\Container\Container->build(Object(Closure))
#4 /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(796): Illuminate\Container\Container->resolve('log', Array, true)
#5 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/Container.php(646): in /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Application.php on line 792
我也试过:
->needs(LoggerInterface::class)
和同样的错误。
我没有在文档中看到如何正确执行此操作的示例。没什么。