php 在 Laravel 中添加常量的最佳实践是什么?(长名单)

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

What is the best practice for adding constants in laravel? (Long List)

phplaravellaravel-5constantslaravel-5.3

提问by Faran Khan

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:

我对 Laravel 比较陌生。我有一个基本问题,在 Laravel 中添加常量的最佳方法是什么。我知道我们用来添加常量的 .env 方法。我还制作了一个常量文件来将它们用于我的项目。例如:

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

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?

等等。它最多可以达到 100 条或更多条记录。那么编写常量的最佳方法应该是什么。.env 方法。或者添加constant.php文件?

Thanks

谢谢

回答by K Arun Singh

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

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

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

config目录中创建一个新文件。让我们称之为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');

回答by Neekobus

I use aliased class constants :

我使用别名类常量:

First, create your class that contain your constants : App/MyApp.phpfor exemple

首先,创建包含常量的类:App/MyApp.php例如

namespace App;

class MyApp {
   const MYCONST = 'val';
}

Then add it to the aliased classes in the config/app.php

然后将其添加到别名类中 config/app.php

'aliases' => [
  //...
  'MyApp' => App\MyApp::class,

Finally use them wherever you like (controllers or even blades) :

最后在任何你喜欢的地方使用它们(控制器甚至刀片):

MyApp::MYCONST

回答by redcenter

Your question was about the 'best practices' and you asked about the '.env method'.

您的问题是关于“最佳实践”,而您问的是“.env 方法”。

.env is only for variables that change because the environmentchanges. Examples of different environments: test, acceptance, production.

.env 仅适用于因环境变化而变化的变量。不同环境的示例:测试、验收、生产。

So the .env contains database credentials, API keys, etc.

因此 .env 包含数据库凭据、API 密钥等。

The .env should (imho) never contain constants which are the same over all environments. Just use the suggested config files for that.

.env 应该(恕我直言)永远不会包含在所有环境中都相同的常量。只需使用建议的配置文件即可。

回答by Hax0r

First you make Constantsfolder inside your app directory.

首先,您Constants在应用程序目录中创建文件夹。

And then you make Constants.php. Define your constants in this file

然后你让Constants.php. 在此文件中定义常量

For Example :

例如 :

define('ONE', '1');
define('TWO', '2');

And you modify the composer.json

然后你修改 composer.json

Alternatively, you can use composer.json to load the bootstrap/constants.php file by adding the following code to the “autoload” section, like so:

或者,您可以使用 composer.json 通过将以下代码添加到“自动加载”部分来加载 bootstrap/constants.php 文件,如下所示:

"autoload": {
    "files": [
        "bootstrap/constants.php"
    ]
}

And update your composer !

并更新您的作曲家!

回答by yangwendaxia

Another way as following:

另一种方式如下:

  1. create the constant.php file in app/config directory
  2. in composer.json file, add the directives like this:

    "autoload": {
       "classmap": [
           "database/seeds",
           "database/factories"
       ],
       "psr-4": {
           "App\": "app/"
       },
       "files": [
           "app/helpers.php",
           "app/config/constants.php"
       ]
    }
    
  1. 在 app/config 目录下创建 constant.php 文件
  2. 在 composer.json 文件中,添加如下指令:

    "autoload": {
       "classmap": [
           "database/seeds",
           "database/factories"
       ],
       "psr-4": {
           "App\": "app/"
       },
       "files": [
           "app/helpers.php",
           "app/config/constants.php"
       ]
    }
    

回答by Dev Semicolon

You can create a file named paths.phpin root directory/config/paths.php

您可以在根目录/config/paths.php中创建一个名为paths.php的文件

Insert this data into paths.php

将此数据插入到paths.php

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

Note : make sure to run command : php artisan config:clear

注意:确保运行命令: php artisan config:clear

回答by Adam

In addition to Arun Singh's answer I would recommend you to use helpers.

除了 Arun Singh 的回答之外,我还建议您使用helpers

Inside your helper.phpyou may define

在你的内部helper.php你可以定义

if ( ! function_exists('constants'))
{
    function constants($key)
    {
       return config('constants.' . $key);
    }
}

Thus instead of

因此,而不是

Config::get('constants.options');
Config::get('constants.options.option_attachment');

you may call

你可以打电话

constants('options');
constants('options.option_attachment');

回答by Mehran Nasr

You can simply do this:

你可以简单地这样做:

  1. Put your constants to 'config/app.php' on main array, like:

    'CONSTANT_NAME' => 'CONSTANT_VALUE',
    
  2. Use them where ever you want with:

    {{ Config::get('CONSTANT_NAME') }} 
    
  1. 将常量放在主数组上的 'config/app.php' 中,例如:

    'CONSTANT_NAME' => 'CONSTANT_VALUE',
    
  2. 随时随地使用它们:

    {{ Config::get('CONSTANT_NAME') }} 
    

回答by Yahya Ayyoub

require app_path().'/constants.php';

define('ADMIN',  'administrator');

or -

或者 -

You can also move more sensitive info

您还可以移动更敏感的信息

return [
   'hash_salt' => env('HASH_SALT'),
 ];

And use it like before:

并像以前一样使用它:

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

回答by Naeem Ijaz

You can define constants at the top of the web.php file located in routes and can be access the constants anywhere in project with just constant name

您可以在位于路由中的 web.php 文件的顶部定义常量,并且可以使用常量名称访问项目中任何位置的常量

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