【发布时间】:2018-09-13 07:07:32
【问题描述】:
我创建了扩展 XeroServiceProvide 的自定义服务提供程序,基本上,我有多个 Xero 帐户,我想更改两个配置参数值运行时 consumer_key 和 consumer_secret。有没有快速的方法。我检查了Service Container 上下文绑定,但不知道如何使用。
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use DrawMyAttention\XeroLaravel\Providers\XeroServiceProvider;
class CustomXeroServiceProvider extends XeroServiceProvider
{
private $config = 'xero/config.php';
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
//
parent::boot();
}
/**
* Register the application services.
*
* @return void
*/
public function register($configParams = [])
{
parent::register();
if(file_exists(config_path($this->config))) {
$configPath = config_path($this->config);
$config = include $configPath;
}
$this->app->bind('XeroPrivate', function () use ($config,$configParams) {
if(is_array($configParams) && count($configParams) > 0){
if(isset($configParams['consumer_key'])){
$config['oauth']['consumer_key'] = $configParams['consumer_key'];
}
if(isset($configParams['consumer_secret'])){
$config['oauth']['consumer_secret'] = $configParams['consumer_secret'];
}
}
return new \XeroPHP\Application\PrivateApplication($config);
});
}
}
从控制器我尝试像这样更改值,但绑定参数不会动态更改
foreach($centers as $center) {
config(['xero.config.oauth.consumer_key' => $center->consumer_key]);
config(['xero.config.oauth.consumer_secret' => $center->consumer_secret]);
}
更新 2
有没有办法在更新配置文件值后重新绑定服务容器,或者我可以刷新服务提供者绑定?
config(['xero.config.oauth.consumer_key' => 'XXXXXXX']);
config(['xero.config.oauth.consumer_secret' => 'XXXXXX']);
// rebind Service provider after update
【问题讨论】:
-
您好,您找到解决方案了吗? :-)
-
是的@Ratto,请在下面找到答案。
标签: php symfony laravel-5 laravel-5.4 xero-api