Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

laravel / lumen access .env values in middleware

Tags:

php

laravel

lumen

Is there any way to access .env vals from inside of a middleware script?

I have tried to do so by env('KEY') but this seems to return null most of the time.

Does any one know of a better way to do this inside of middleware or a way to insure the .env file has been loaded before the middleware runs?

like image 734
Aaron Avatar asked Apr 12 '16 17:04

Aaron


1 Answers

You can use config() to access .env variables. For example, if you want to get MySQL port, use this:

$mysqlPort = config()['database']['connections']['mysql']['port'];

To get all available variables, you can do dd(config());

If you want to use custom variables in .env, you also can do this:

CUSTOM=hello

And to get this variable, use env() helper:

echo env('CUSTOM'); // Will output 'hello'
like image 135
Alexey Mezenin Avatar answered Sep 19 '22 18:09

Alexey Mezenin