【问题标题】:How to bind configuration value Laravel service provider on run-time?如何在运行时绑定配置值 Laravel 服务提供者?
【发布时间】:2018-09-13 07:07:32
【问题描述】:

我创建了扩展 XeroServiceProvide 的自定义服务提供程序,基本上,我有多个 Xero 帐户,我想更改两个配置参数值运行时 consumer_keyconsumer_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


【解决方案1】:

这就是我最终的做法。我创建了一个在运行时设置值的自定义函数。

/**
 * connect to XERO by center
 * @param $center
 * @return mixed
 */
public static function bindXeroPrivateApplication($center){
    $configPath = config_path('xero/config.php');
    $config = include $configPath;
    config(['xero.config.oauth.consumer_key' => $center->consumer_key]);
    config(['xero.config.oauth.consumer_secret' => $center->consumer_secret]);

    
    return \App::bind('XeroPrivate', function () use ($config,$center) {
        $config['oauth']['consumer_key']    = $center->consumer_key;
        $config['oauth']['consumer_secret'] = $center->consumer_secret;

        return new \XeroPHP\Application\PrivateApplication($config);
    });
}

我有一个名为 Center.php 的模型,我正在从与下面相同的模型调用上面的函数。

$center = Center::find(1);
self::bindXeroPrivateApplication($center);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-19
    • 2016-08-05
    • 2018-12-03
    • 1970-01-01
    • 2018-10-20
    • 2014-12-25
    • 1970-01-01
    相关资源
    最近更新 更多