【问题标题】:Laravel 5 how to include config file?Laravel 5 如何包含配置文件?
【发布时间】:2017-12-06 07:45:25
【问题描述】:

我正在使用 Laravel 5.5,并且我在 dir app\Json\Schemas\TestSchema.php 中有一个配置文件,其中包含一个具有如下配置的数组:

return [
  'value' => 'string'
];

动态包含文件的最佳方式是什么?

在我的模型中

namespace App;

use App\Json\Traits\JsonModel;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{ 
    public function getJsonSchema($schema)
    {
        return  'App\\Json\\Schemas\\'.$schema; 
    }
}

【问题讨论】:

    标签: laravel-5 php-7.2


    【解决方案1】:

    最好的方法是将您的配置文件移动到config 文件夹。假设你称它为schema.php

    您可以使用(其中schema 是不带.php 的配置文件的名称)在代码中加载配置文件:

    config('schema');
    

    如果你想要value,你可以使用点表示法(其中schema 是配置文件的名称,value 是配置数组中的一个键):

    $value = config('schema.value');
    

    因此getJsonSchema 函数可以更新为(其中$schema 是配置文件的名称):

    public function getJsonSchema($schema)
    {
        return  'App\\Json\\Schemas\\'.config($schema.'.value'); 
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-27
      • 2015-04-14
      • 1970-01-01
      • 2011-04-07
      • 2015-03-22
      • 2015-02-22
      相关资源
      最近更新 更多