Laravel如何将Nexmo配置从.env迁移至数据库?
Absolutely! You can totally migrate your Nexmo (now rebranded as Vonage) settings from the .env file to your Laravel app’s database. Since the official docs don’t spell this out directly, let’s walk through a straightforward approach to make this work.
Step 1: Create a Database Table for Nexmo Settings
First, we need a dedicated place to store the configuration values. Generate a migration file to set up the table:
php artisan make:migration create_nexmo_settings_table
Open the generated migration file and update the up() method to define the fields you need (adjust based on your actual Nexmo usage—api key, secret, signature secret, etc.):
public function up() { Schema::create('nexmo_settings', function (Blueprint $table) { $table->id(); $table->string('api_key'); $table->string('api_secret'); $table->string('signature_secret')->nullable(); $table->string('application_id')->nullable(); $table->text('private_key')->nullable(); $table->timestamps(); }); }
Run the migration to create the table:
php artisan migrate
Then add your Nexmo credentials to this table—you can use a seeder or manually insert the row via your database management tool.
Step 2: Create a Model for the Settings
Make a model to easily interact with the settings table:
php artisan make:model NexmoSetting
Add a static method to fetch the active settings (since we’ll likely only need one set of credentials):
class NexmoSetting extends Model { protected $fillable = [ 'api_key', 'api_secret', 'signature_secret', 'application_id', 'private_key' ]; public static function getActive() { // Assuming you only have one row of settings; adjust if needed return self::first(); } }
Step 3: Override Nexmo’s Default Configuration
Laravel loads Nexmo settings from config/nexmo.php, which pulls from .env by default. We’ll override this using a service provider to pull values from the database instead.
You can create a custom service provider or use an existing one like AppServiceProvider:
php artisan make:provider NexmoConfigServiceProvider
Open the provider’s boot() method and add code to overwrite the config values:
use App\Models\NexmoSetting; use Illuminate\Support\Facades\Config; public function boot() { $nexmoSettings = NexmoSetting::getActive(); if ($nexmoSettings) { Config::set('nexmo.api_key', $nexmoSettings->api_key); Config::set('nexmo.api_secret', $nexmoSettings->api_secret); Config::set('nexmo.signature_secret', $nexmoSettings->signature_secret); Config::set('nexmo.application_id', $nexmoSettings->application_id); Config::set('nexmo.private_key', $nexmoSettings->private_key); } }
Don’t forget to register this provider in config/app.php under the providers array:
App\Providers\NexmoConfigServiceProvider::class,
Step 4: (Optional) Cache Settings for Better Performance
Fetching from the database on every request adds unnecessary overhead. To optimize, cache the settings. Update the getActive() method in your model:
use Illuminate\Support\Facades\Cache; public static function getActive() { return Cache::rememberForever('nexmo_settings', function () { return self::first(); }); }
Just remember to clear the cache whenever you update the settings in the database:
php artisan cache:clear
Step 5: Test It Out
Now you can remove the Nexmo-related entries from your .env file and test sending a message or using any Nexmo/Vonage feature in your app. It should pull credentials directly from the database!
A quick note: If you’re using the newer Vonage package (since Nexmo rebranded), the config file is config/vonage.php instead—just adjust the Config::set() calls to target that file’s keys.
内容的提问来源于stack exchange,提问作者Billy Adelphia

