在部署期间执行 config:cache 命令

Executing config:cache command during deployment

我在 .env 文件中有一个支付网关的秘密测试密钥。

APP_TIMEZONE = 'Africa/Lagos'
APP_PAYSTACK_KEY = sk_test_b6c0b4925403blablabla

原因是从事该项目的其他人可以使用他们自己的测试密钥(如果有的话)。所以在支付控制器中我得到这个键的值是这样的:

"authorization: Bearer " .env('APP_PAYSTACK_KEY' , 'sk_test_b6c0b4925403blablabla')

在部署期间,我打算 运行 config:cache 这样 Laravel 就不会花很长时间来获取所需的配置设置。但是来自 Laravel 文档:

If you execute the config:cache command during your deployment process, you should be sure that you are only calling the env function from within your configuration files. Once the configuration has been cached, the .env file will not be loaded and all calls to the env function will return null.

所以我的问题是如何在配置文件中设置此 APP_PAYSTACK_KEY 以及如何在我的应用程序中的任何位置检索它?

您可以将 paystack 添加到您的 config/services.php 文件:

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Third Party Services
    |--------------------------------------------------------------------------
    |
    | This file is for storing the credentials for third party services such
    | as Stripe, Mailgun, SparkPost and others. This file provides a sane
    | default location for this type of information, allowing packages
    | to have a conventional place to find your various credentials.
    |
    */

    // ...

    'paystack' => [
        'key' => env('APP_PAYSTACK_KEY', 'sk_test_b6c0b4925403blablabla'),
    ],
];

然后在您的控制器上调用 config() 辅助方法而不是 env()

"authorization: Bearer " .config('services.paystack.key')

现在您可以安全地通过调用 php artisan config:cache.

来缓存您的配置了