在 Laravel 中存储和检索常量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12183772/
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-09-13 15:48:12  来源:igfitidea点击:

Storing and retrieving constants in laravel

phplaravel

提问by SwiftD

I have just started playing with laravel haveing come from codeigniter and I am trying to work out the best way to define a series of constants. CI uses a constants folder under app/config and I was largely happy with this approach for most things but wanted advice as to the best way to do this in Laravel.

我刚刚开始使用来自 codeigniter 的 laravel,我正在尝试找出定义一系列常量的最佳方法。CI 在 app/config 下使用了一个常量文件夹,我对大多数情况下的这种方法非常满意,但希望就在 Laravel 中执行此操作的最佳方法提供建议。

My constants fall into 3 categories and I would like advice if possible on how best to store and retrieve each (baring in mind I'm completely new to Laravel.

我的常量分为 3 类,如果可能的话,我想就如何最好地存储和检索每个类提供建议(请记住,我对 Laravel 完全陌生。

Type 1: Constants which need to be loaded everytime a controller is called: e.g. I would like to start by defining a constant to tell me if the user is requesting content via ajax, this is what I used to do in CI constants file: define('IS_AJAX', isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')

类型 1:每次调用控制器时都需要加载的常量:例如,我想首先定义一个常量来告诉我用户是否通过 ajax 请求内容,这就是我过去在 CI 常量文件中所做的: define('IS_AJAX', isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')

Type 2: Constants which can be changed by the user: Are these best stored in a database or would writing to a config file be a better option? Again looking for advice on how to store and retrieve

类型 2:用户可以更改的常量:这些常量是存储在数据库中还是写入配置文件是更好的选择?再次寻求有关如何存储和检索的建议

Type 3: Constants which are only needed in specific controllers: Ideally I would like to be able to group constants into arrays or separate files and pull them out in groups as required. e.g. I could just retrieve my upload settings for my upload controller.

类型 3:仅在特定控制器中需要的常量:理想情况下,我希望能够将常量分组到数组或单独的文件中,并根据需要将它们按组拉出。例如,我可以检索上传控制器的上传设置。

Thanks for any help/advice in advance - examples would be greatly appreciated

感谢您提前提供任何帮助/建议-示例将不胜感激

回答by crynobone

Type 1You can add any constant in Bundle (or application) start.php file.

类型 1您可以在 Bundle(或应用程序)start.php 文件中添加任何常量。

Type 2 and 3I would suggest using Config for both these requirement.

类型 2 和 3我建议使用 Config 来满足这两个要求。

回答by Sebastian Gomez

My solution is create a file inside app/config folder called "constants.php" app/config/constants.php

我的解决方案是在 app/config 文件夹中创建一个名为“constants.php”的文件 app/config/constants.php

The code inside is in array way.

里面的代码是数组方式。

"某个值", 'c2'=>"某个值" ); ?>

then inside each controller you can retrieve using Config::get('constants.c1');

然后在每个控制器中,您可以使用 Config::get('constants.c1'); 检索;

I think is an easy way to do it, it's more scalable.

我认为这是一种简单的方法,它更具可扩展性。

回答by Khang

<?php
//file : app/config/constants.php

return [
    'IS_AJAX' => isset($_SERVER['HTTP_X_REQUESTED_WITH']
];

in anywhere:

在任何地方:

echo Config::get('constants.IS_AJAX');