php 如何在 Laravel 5 中向 app/config 添加自定义配置文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38665907/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:40:08  来源:igfitidea点击:

How to add custom config file to app/config in Laravel 5?

phplaravellaravel-5configuration

提问by Alexander Lomia

I want to add custom_config.phpto app/configdirectory of my Laravel 5 project, but can't find any article explaining this process. How is this done?

我想添加custom_config.php到我的 Laravel 5 项目的app/config目录中,但找不到任何解释此过程的文章。这是怎么做的?

回答by Bharat Geleda

You can easily add a new file to the config folder. This file should return configuration values. Check other config files for reference. Say constants.phpis like so

您可以轻松地将新文件添加到 config 文件夹。此文件应返回配置值。检查其他配置文件以供参考。说constants.php是这样

<?php

return array(
    'pagination' => array(
           'items_per_page' => 10
    ),
);

You can now access this config file from anywhere by either using the ConfigFacade or the config()global function like so

您现在可以通过使用ConfigFacade 或config()像这样的全局函数从任何地方访问此配置文件

Config::get('constants.pagination.items_per_page');

or

或者

config('constants.pagination.items_per_page');

i.e.

IE

config('file_name.variable_name');

回答by jotaelesalinas

I don't know how many times one might need to create a config file, but here you have an Artisan command for it: https://gist.github.com/jotaelesalinas/eafd831048ca5fb5fba970afd1921f70

我不知道一个人可能需要创建多少次配置文件,但这里有一个 Artisan 命令:https: //gist.github.com/jotaelesalinas/eafd831048ca5fb5fba970afd1921f70