[英]Laravel 6 - Pass a parameter to $app->when()->needs()->give()
I am trying to use this package to push notifications to users via OneSignal.我正在尝试使用此程序包通过 OneSignal 向用户推送通知。 However I needed to make a little change.
但是我需要做一点改变。 My API serves two (related) apps and I have two OneSignal configs.
我的 API 提供两个(相关)应用程序,并且我有两个 OneSignal 配置。 I am trying to override its ServiceProvider (using this technique ).
我正在尝试覆盖其 ServiceProvider(使用此技术)。
The ServiceProvider presents itself as follows ServiceProvider 如下所示
<?php
namespace NotificationChannels\OneSignal;
use Berkayk\OneSignal\OneSignalClient;
use Illuminate\Support\ServiceProvider;
use NotificationChannels\OneSignal\Exceptions\InvalidConfiguration;
class OneSignalServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*/
public function boot()
{
$this->app->when(OneSignalChannel::class)
->needs(OneSignalClient::class)
->give(function () {
$oneSignalConfig = config('services.onesignal');
if (is_null($oneSignalConfig)) {
throw InvalidConfiguration::configurationNotSet();
}
return new OneSignalClient(
$oneSignalConfig['app_id'],
$oneSignalConfig['rest_api_key'],
''
);
});
}
}
The behavior that I want to change is located in the line我想改变的行为位于行中
$oneSignalConfig = config('services.onesignal');
As it assumes that my config/services.php
has the following entry (stated in the doc) :因为它假设我的
config/services.php
有以下条目(在文档中说明):
// config/services.php
...
'onesignal' => [
'app_id' => env('ONESIGNAL_APP_ID'),
'rest_api_key' => env('ONESIGNAL_REST_API_KEY')
],
...
Whereas I want to set my config/services.php
as follows而我想设置我的
config/services.php
如下
// config/services.php
...
'onesignal' => [
'app1' => [
'app_id' => env('ONESIGNAL_1_APP_ID'),
'rest_api_key' => env('ONESIGNAL_1_REST_API_KEY')
],
'app2' => [
'app_id' => env('ONESIGNAL_2_APP_ID'),
'rest_api_key' => env('ONESIGNAL_2_REST_API_KEY')
],
],
...
And I want somehow to tell my ServiceProvider (through some kind of parameter) to either do我想以某种方式告诉我的 ServiceProvider(通过某种参数)要么做
$oneSignalConfig = config('services.onesignal.app1');
OR或者
$oneSignalConfig = config('services.onesignal.app2');
But I didn't find any way to pass a parameter to the class, the boot
function or the give
method (and if I understood well I shouldn't even be doing that).但是我没有找到任何方法将参数传递给类、
boot
函数或give
方法(如果我理解得很好,我什至不应该这样做)。
The only way I could think of is to create two classes that extend the OneSignalChannel::class
and duplicate code in the boot
function so it becomes as follows :我能想到的唯一方法是创建两个类来扩展
OneSignalChannel::class
并在boot
函数中复制代码,因此它变成如下:
public function boot()
{
$this->app->when(FirstOneSignalChannel::class)
->needs(OneSignalClient::class)
->give(function () {
$oneSignalConfig = config('services.onesignal.app1');
if (is_null($oneSignalConfig)) {
throw InvalidConfiguration::configurationNotSet();
}
return new OneSignalClient(
$oneSignalConfig['app_id'],
$oneSignalConfig['rest_api_key'],
''
);
});
$this->app->when(SecondOneSignalChannel::class)
->needs(OneSignalClient::class)
->give(function () {
$oneSignalConfig = config('services.onesignal.app2');
if (is_null($oneSignalConfig)) {
throw InvalidConfiguration::configurationNotSet();
}
return new OneSignalClient(
$oneSignalConfig['app_id'],
$oneSignalConfig['rest_api_key'],
''
);
});
}
The difference in the when
provoking a difference in the config
but it seems a lot of duplication and not extensible (what if I had three apps).引起
config
差异when
的差异,但似乎有很多重复且不可扩展(如果我有三个应用程序会怎样)。
Should I use this method, or is there a way to pass a parameter to this ServiceProvider or is there another solution ?我应该使用这种方法,还是有办法将参数传递给这个 ServiceProvider 还是有其他解决方案?
https://stackoverflow.com/a/34224082/10371024 https://stackoverflow.com/a/34224082/10371024
I could see what you need, but to pass parameter to boot method is not a good idea according to Laravel architecture.我可以看到您需要什么,但是根据 Laravel 架构,将参数传递给 boot 方法并不是一个好主意。 You may try to get what you want with using events as Vladislav suggested.
您可以尝试使用 Vladislav 建议的事件来获得您想要的东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.