I tried to get the environment variable from the .env in my root with
Route::get('/test', function () {
return "value is". getenv('APP_ENV');
});
and
Route::get('/test', function () {
return "it is". env('APP_ENV');
});
It is in .env
APP_NAME=Laravel
APP_ENV=local
How can I get access to it?
With Laravel, you should avoid environmental variables outside of your configuration files.
In your config files, you can use environmental variables, example in config/app.php:
'env' => env('APP_ENV', 'production'),
Then you can access this using the config helper: config('app.env').
This allows you to cache your configuration and still access these values, since env('APP_ENV') will no longer work once your config is cached.
Route::get('/test', function () {
return "it is".config('app.name');
});
use env('ENVKEY')
Don't forget to clear cache sometime it cause because of cache.
php artisan config:clear
php artisan cache:clear
composer dump-autoload
For more info look at the doc
just run this commands in cmd.
php artisan config:cache
then
php artisan config:clear
then
php artisan cache:clear
As per the Laravel Documentation on Environment Configuration,
All of the variables listed in this file will be loaded into the $_ENV
PHP super-global when your application receives a request. You may use
the env helper to retrieve values from these variables.
So, it is possible to access the variable as
$_ENV['envKey'];
laravel provides a global helper function for this kind of task
$val = config('app.something');
you can also set new values with the following method
config(['app.something' => 'cat']);
reference
for your particular task it would be
$val = config('app.env');
or to determine the env globally
$environment = App::environment();
i hope this helps, have a nice one!
App::environment()
try this please
Related
Here the code
Controller :
public function index()
{
dump($_ENV);
}
Route :
Route::get('/', 'HomeController#index');
I can't display env file in the screen
Try:
php artisan config:clear
php artisan cache:clear
composer dump-autoload
chek:
{{ env('APP.ENV') }}
your doing things in wrong way try this
env('APP_ENV');
getenv('APP_ENV');
if your using cache then you can access the value from config like this .
config('app.env');
or using facede
Config::get('app.env');
If your $_ENV array is mysteriously empty, but you still see the variables when calling getenv() or in your phpinfo(), check your http://us.php.net/manual/en/ini.core.php#ini.variables-order ini setting to ensure it includes "E" in the string.
I'm upgrading Laravel to 5.2 from 5.1. When i refere to variable API_DOMAIN in .env file using $_ENV
$_ENV['API_DOMAIN']
I get an error saying Undefined index: API_DOMAIN". Am I missing something here? should i do something after composer update?
Try using the env helper instead, env('API_DOMAIN')
Run the below commands after making changes in env file. It will flush and repopulate all the env variable.
php artisan config:cache //flush all cached env variable
php artisan config:clear //repopulate all the env variable
php artisan cache:clear //flush all the cached content
You should not directly work with environment values in your application, I would create a new configuration file or use an existing one and add the value there, this way you can cache the configuration.
Lets use your API_DOMAIN as an example:
.env file
API_DOMAIN=www.mydomain.com
Config file
config/api.php for example
<?php
return [
'domain' => env('API_DOMAIN'),
];
Now you can use the config helper to use this variable in your application:
$value = config('api.domain');
You should never use the env() helper outside of config files, because when you cache your config, env() will return null.
I have these values in my .env file:
APP_IMG_PATH="/img/"
APP_IMG_LOGO="sclogo.png"
Then in my header I do this:
<img src="{!! env('APP_IMG_PATH') . env('APP_IMG_LOGO') !!}" class="navbar-logo"/>
But the log does not show and in Chrome Inspect I get
<img src(unknown) class="navbar-logo">
I tried clearing config cache and recreating it:
php artisan config:cache;
But the result is the same. Any help is appreciated.
Config caching disables env() calls. Any call to env() will return null once the config is cached.
Perhaps a poor choice by the Laravel team, but the idea is to encourage you to use config(), not env() within your code.
Instead, add new lines to your config/app.php (if that's where you choose):
'img_path' => env('APP_IMG_PATH'),
'img_logo' => env('APP_IMG_LOGO'),
Then re-create your config cache and use config('app.img_path') and config('app.img_logo') within your application.
After addig new things to env file, you need to clear your cache.
php artisan config:clear
php artisan config:cache
In staging and production environment, when I try to get my custom defined variable from .env file, it returns null. I have tried creating new Key for APP and cleared all sort of caches, but result is same.
APP_NAME="App - Staging"
APP_ENV=staging
APP_KEY=<HIDDEN_KEY>
APP_DEBUG=true
APP_LOG_LEVEL=none
APP_URL=https://staging.app.com
APP_BASE_URL="https://app-staging.app.com"
Clearing Cache
php artisan config:clear
php artisan cache:clear
php artisan route:clear
php artisan view:clear
php artisan config:cache
But when using following in blade view gets nothing
env('APP_BASE_URL') returns null
It is because you have run php artisan config:cache. If you are using config:cache, your env() calls should only be made in your config files.
See here: https://laravel.com/docs/5.6/configuration#configuration-caching
This post solved my issue, I defined custom config in config/app.php
https://laracasts.com/discuss/channels/laravel/accessing-custom-environment-variable
/*
|-------------------------------------------------------------------------
| Custom config variables
|-----------
|
*/
'base_url' => env('APP_BASE_URL', 'http://localhost'),
Then in .env I definded:
APP_BASE_URL="https://app-staging.app.com"
Finally cleared the cache, worked.
In blade view
{{ config('app.base_url') }}
Yes it will return only null
If you execute the config:cache command during your deployment process, you should be sure that you are only calling the env function from within your configuration files. Once the configuration has been cached, the .env file will not be loaded and all calls to the env function will return null.
See the laravel documentation https://laravel.com/docs/5.6/configuration#configuration-caching
When I am using latest Laravel 5.2.2 and Entrust ("zizaco/entrust": "5.2.x-dev") I face this error and not sure how to solve this:
Call to undefined method Zizaco\Entrust\EntrustServiceProvider::hasRole()
I tested this code on HomeController.php
use Entrust;
class HomeController extends Controller
{
public function index()
{
if (Entrust::hasRole('admin')) {
echo "string";
}
return view('home');
}
}
This is my config/app.php service provider
Zizaco\Entrust\EntrustServiceProvider::class
config/app.php facade alias
'Entrust' => Zizaco\Entrust\EntrustFacade::class
I also already generate the model needed
Did I miss something here?
I have same issue, here are the steps I have taken to solve the issue
In your .env file change to cache array
CACHE_DRIVER=array
and dont forget to run
php artisan config:cache
It seems all the steps is correct, and I just need to clear the cache with php artisan config:cache
And if you face an error like below
BadMethodCallException in vendor\laravel\framework\src\Illuminate\Cache\Repository.php line 380:
This cache store does not support tagging.
You need to change in .env this line to array
CACHE_DRIVER=array
Try this:
Open environment file of your laravel change CACHE_DRIVER=file to CACHE_DRIVER=array and save.
Now try your CLI command.
Laravel drivers does not support tagging. To solve this, go to your .env file and change
Cache_driver=file
to
Cache_driver=array
and run
php artisan config:cache