-1

我可以在调度作业开始时加载配置文件吗?

我尝试customerName在 Schedule Class 中使用一个局部变量,它已经在 Config 文件夹中定义为 named customerInfo

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
use Config;

class Checkout extends Command
{
   ***
   public function handle()
   { 
      ***

      $customerName = Config::get('customerInfo.customer_name'); //test code
      \Log::info($customerName); // for error check in log file

      ***
   }

}

但它没有奏效。

我必须在构造函数中声明它还是必须使用'\'as'\Config'即使已经将别名声明为use Config;

当计划作业开始运行时,在 Config 中使用自定义变量的最佳简单解决方案是什么?

4

2 回答 2

2

您收到此错误是因为您尚未定义 PHP 可以在哪个命名空间中找到Config该类。

您需要Config在类顶部的 usings 中包含外观:

use Config;

或者使用配置助手功能

config('customerInfo.customer_name');
于 2019-02-11T08:56:24.603 回答
1

config()helper 或ConfigFacade 用于从configdir 获取值。

在 config 文件夹中创建一个名为customerInfo.

return [
   'customer_name' => 'A name'
];

现在您可以访问该名称

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;

class Checkout extends Command
{
   ***
   public function handle()
   { 
      ***

      $customerName = Config::get('customerInfo.customer_name'); //test code
      \Log::info($customerName); // for error check in log file

      ***
   }

}
于 2019-02-11T08:56:53.910 回答