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

后端 未结 11 738
猫巷女王i
猫巷女王i 2021-01-30 06:21

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 mad

相关标签:
11条回答
  • 2021-01-30 06:51

    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"
         ]
      }
      
    0 讨论(0)
  • i think best way to define constant using a helper file. check my solution.

    Define file path in composer.json

       "extra": {
            "laravel": {
                "dont-discover": []
            }
        },
        "autoload": {
            "files": [
                "app/helpers.php",
                "app/Helper/function.php"  // constant defined here
            ],
    

    app/Helper/function.php

    define("assetPath","UI/");
    define("viewPath","UI/");
    

    use this constant anywhere in project. i am using in blade file.

      <script src="{{asset(assetPath.'js/jquery.min.js')}}"></script>
      <script src="{{asset(assetPath.'js/popper.min.js')}}"></script>
      <script src="{{asset(assetPath.'js/bootstrap.min.js')}}"></script>
    

    my approach is better than this

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

    here another problem is this , you have to run cache:clear or cache command for this. but my approach not required this.

    0 讨论(0)
  • 2021-01-30 06:53

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

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

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

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

    0 讨论(0)
  • 2021-01-30 06:55

    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') }} 
      
    0 讨论(0)
  • 2021-01-30 06:56
    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');
    
    0 讨论(0)
  • 2021-01-30 06:58

    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

    define('OPTION_ATTACHMENT', 13);
    define('OPTION_EMAIL', 14);
    define('OPTION_MONETERY', 15);
    define('OPTION_RATINGS', 16);
    define('OPTION_TEXTAREA', 17);
    
    0 讨论(0)
提交回复
热议问题