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

后端 未结 11 759
猫巷女王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:59

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

    Inside your helper.php you may define

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

提交回复
热议问题