What is the best practice for adding constants in

2019-03-09 13:13发布

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);

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?

Thanks

7条回答
疯言疯语
2楼-- · 2019-03-09 14:06

I use aliased class constants :

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

namespace App;

class MyApp {
   const MYCONST = 'val';
}

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

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

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

MyApp::MYCONST
查看更多
登录 后发表回答