Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 915
Faran Khan Avatar asked Feb 10 '17 09:02

Faran Khan


People also ask

What is a correct way of defining constants in PHP?

A constant is an identifier (name) for a simple value. The value cannot be changed during the script. A valid constant name starts with a letter or underscore (no $ sign before the constant name). Note: Unlike variables, constants are automatically global across the entire script.

Which PHP built in function will help you to create constants?

Use the define() function to create a constant. It defines constant at run time. Let's see the syntax of define() function in PHP. name: It specifies the constant name.

Which keyword function is used to create a constant in PHP?

The const keyword is used to create constants. Unlike with the define() function, constants created using the const keyword must be declared in the global scope.


3 Answers

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 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('constants.options');
// or if you want a specific one
config('constants.options.option_attachment');
like image 91
K Arun Singh Avatar answered Oct 21 '22 18:10

K Arun Singh


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
like image 29
Neekobus Avatar answered Oct 21 '22 18:10

Neekobus


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.

like image 18
redcenter Avatar answered Oct 21 '22 16:10

redcenter