在laravel中添加常量的最佳实践是什么? (长列表) [英] What is the best practice for adding constants in laravel? (Long List)

查看:85
本文介绍了在laravel中添加常量的最佳实践是什么? (长列表)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对laravel很陌生.我有一个基本的问题,在laravel中添加常量的最佳方法是什么. 我知道我们用来添加常量的.env方法. 另外,我制作了一个常量文件以将其用于我的项目. 例如:

I am rather new to laravel. I have a basic question, What is the best way to add constants in laravel. I know the .env method that we use to add the constants. Also I have made one constants file to use them for my project. For example:

define('OPTION_ATTACHMENT', 13);
define('OPTION_EMAIL', 14);
define('OPTION_MONETERY', 15);
define('OPTION_RATINGS', 16);
define('OPTION_TEXTAREA', 17);

以此类推.它最多可以达到100条或更多记录.那么写常数的最佳方法应该是什么. .env方法.还是添加constant.php文件?

And so on. It can reach upto 100 or more records. So What should be the best approach to write the constants. The .env method. Or adding the constant.php file?

谢谢

推荐答案

对于在整个应用程序中全局使用的大多数常量,将它们存储在配置文件中就足够了.这也很简单

For most constants used globally across the application, storing them in config files is sufficient. It is also pretty simple

config目录中创建一个新文件.我们称之为constants.php

Create a new file in the config directory. Let's call it constants.php

您必须在其中返回一个配置值数组.

In there you have to return an array of config values.

return [
    'options' => [
        'option_attachment' => '13',
        'option_email' => '14',
        'option_monetery' => '15',
        'option_ratings' => '16',
        'option_textarea' => '17',
    ]
];

您可以按以下方式访问它们

And you can access them as follows

Config::get('constants.options');
// or if you want a specific one
Config::get('constants.options.option_attachment');

这篇关于在laravel中添加常量的最佳实践是什么? (长列表)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆