Accepted answer not effective anymore after Laravel 5.2
version:
To get environment values in controllers or (any php file in laravel project) you have tow cases:
Case 1 (Predefined keys): If you need to get value from .env
file which created by Laravel project as (default .env file), or by third party plugin you already installed it and follow the correct installation procedure of installation, See this answer for explain: https://stackoverflow.com/a/69707775/2877427
Case 2 (Not predefined keys - if you need generate new custom environment key): to do that follow these instructions:
1 -Add your custom key in .env
file, for example i will add this key:
UNIFONIC_APP_ID=xx2Erdasd7Ik7XfdesfaV9HX47
2- Add new php file in laravel_root/config
folder, for example i will add this file config/custom.php
and its contents like this:
return [
'unifonic_id' => env('UNIFONIC_APP_ID' , 'Here Default value will used of UNIFONIC_APP_ID not defined in .env file'),
];
3- Run these tow commands in terminal:
php artisan config:clear
php artisan config:cache
4 - Now in any php file (Controllers, Models, Views, ... etc) you can call this function:
config('custom.unifonic_id');
Where custom
is the name of php file which generated in step 2
, and unifonic_id
is the key of array which defined in step 2
This will return value if UNIFONIC_APP_ID
which defined in .env
, if not exists will return default value which defined in config file in step 2
Note 1: config file can return multi dimensions array, take look for
structers of config files which located in laravel_root/config
folder.
Note 2: you can modify file laravel_root/config/services.php
insted
of create new config file...