Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

builtin server read environment variables file #16828

Open
JohnRDOrazio opened this issue Nov 15, 2024 · 4 comments
Open

builtin server read environment variables file #16828

JohnRDOrazio opened this issue Nov 15, 2024 · 4 comments

Comments

@JohnRDOrazio
Copy link

Description

If the builtin server (php -S localhost:PORT) could automatically detect environment variable files such as .env, .env.development and load the key => value pairs such that they are then available in the $_ENV magic variable array, it would make local development much easier and more similar to other languages such as node or ruby.

Say I spawn the bultin server for a local API backend on port 8000. Then I want to spawn another builtin server for a frontend, which however knows nothing about which port the API backend is being served on. If I could place an environment variable file in my frontend project .env.development with API_PORT=8000, the frontend could then have $_ENV['API_PORT'] with a value of 8000 and I could use that in my frontend logic to determine how the frontend should interface with the API backend.

I might add, that spawning a localhost instance with the builtin server could also automatically set an environment variable of $_ENV['development']=true.

Docker, React and NextJS all follow similar patterns of loading environment variables from .env files:

It would be great if the PHP builtin server could follow a similar pattern, making PHP development more user / development friendly.

@JohnRDOrazio
Copy link
Author

Ruby has a similar mechanism with the dotenv ruby gem https://github.com/bkeepers/dotenv .

@JohnRDOrazio
Copy link
Author

JohnRDOrazio commented Nov 16, 2024

I'm seeing there is a composer package that can be used to obtain this (https://github.com/vlucas/phpdotenv - https://packagist.org/packages/vlucas/phpdotenv), however it would be a nice convenience if at least a base environment variable along the lines of $_ENV['APP_ENV'] with a value along the lines of development were automatically created by the builtin server.

@iluuu1994
Copy link
Member

Hi @JohnRDOrazio!

I'm not sure it makes sense to implement a feature the application depends on solely in the built-in web server. When you're ready to push to production, you will need to remember that the environment that .env parsing will not work there, and to bundle a library that will do it for you. But if that's necessary, why not just do that in the first place? There's https://symfony.com/components/Dotenv, https://github.com/vlucas/phpdotenv and many more. I could see a stronger argument to implement this as a general feature of PHP, but I'd consider this a solved problem.

If you still think that this is something PHP would benefit from, please send an e-mail to the internals mailing list to pitch your idea, so we can see if that's something people would like to see. You'll also need to elaborate in a bit more detail how this feature would work (i.e. with which SAPIs, when they are loaded, whether they are cached, etc.).

@JohnRDOrazio
Copy link
Author

in fact I don't think that the environment files are quite as necessary for production, since in production you would generally be using a full fledged server such as Apache or Nginx. But since the built-in server is quite useful for localhost development and testing, without requiring Apache or Nginx, I see it more as a feature than a bug to make local development more user friendly, if the built-in server automatically creates a "development" environment by automatically creating a development environment variable, similar to how other frameworks work. I already have logic in place in my application to detect whether we are on localhost or in production, so I'm not worried about .env files not working in production (I don't currently use any .env files in production). In any case I will send an email on the mailing list to see if anyone else is interested in such a feature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants