Tetravalence's avatar

How to grab error code when .env file does not exist?

Hello, I would like my package to handle the Laravel request when .env file does not exist in the application directory and the web server sends the 500 error. Should I use both controller and view for this purpose or I have to register an Exception Handler?

0 likes
5 replies
Sergiu17's avatar

You could wrap into try catch block, or use if file_exists

Cronix's avatar

You'd probably have to do it in /public/index.php or something before laravel bootstraps itself, since laravel pulls data from .env if the /config file values aren't hardcoded and referencing env(). That would most likely error out before your package even gets loaded. Like, let's say it was a new laravel install and they didn't even have a .env. Laravel would error out because it can't connect to the db and stuff.

Snapey's avatar

Who says that an .env has to exist? This is an unsafe assumption since ideally you would never use .env in production. It is only a substitute for web server environment variables during development.

You should really be checking values in config.

1 like
Tetravalence's avatar

@Snapey: OK, now if I run the command php artisan config:cache for deploying my application to production I still get the 500 error message from the server when .env file does not exist in the root of the application directory. So I guess I have overlooked some basic information about the deployment procedure, but what? Thanks for all the replies

Snapey's avatar

Well you cannot just leave .env out, things like the application key and database path are critical.

Its not critical that they are in the .env file though

Please or to participate in this conversation.