How can I define some constants for the whole of project? How can I define some constants for the whole of project? laravel laravel

How can I define some constants for the whole of project?


The Laravel way is using config files. Create my.php and put it in /config directory and then access constant values globally:

config('my.variable')

Here's an example of my.php:

<?phpreturn [    'variable' => 15,    'some-array' => [1, 2, 5],];


You can create a file like @Alexey Mezenim has suggested - say admin-settings.php in your /config directory and populate it with an array containing the constants/values you need globally

//admin-config.php<?php    return[        'db' => [                   'name' => 'mydb',                   'username' => 'root',                   'password' =>''                ],         'login' => [                       'email' =>'some@one.com',                       'password' => 'mypas'                    ],         'sleep_on_error' => 1    ];  

Then you can access the values anywhere in your application as

config('admin-config.db.name')config('admin-config.login.email')config('admin-config/sleep_on_error')  //and so on


This is very simple, just in two steps:

1) Create a file say constants.php under config/app and place value in an form of array like:

return [    'APP_NAMEIS' => 'myappname'];

After above step,
2) use that code at anywhere by using Config Facade like:

Config::get('constants.APP_NAMEIS');

Hope this clear that how can use the constant anywhere. See attached reference for more detail.