{"action":"created","issue":{"url":"https://api.github.com/repos/cakephp/app/issues/224","labels_url":"https://api.github.com/repos/cakephp/app/issues/224/labels{/name}","comments_url":"https://api.github.com/repos/cakephp/app/issues/224/comments","events_url":"https://api.github.com/repos/cakephp/app/issues/224/events","html_url":"https://github.com/cakephp/app/issues/224","id":59609857,"number":224,"title":"RFC: Adding support for `.env` files","user":{"login":"josegonzalez","id":65675,"avatar_url":"https://avatars.githubusercontent.com/u/65675?v=3","gravatar_id":"","url":"https://api.github.com/users/josegonzalez","html_url":"https://github.com/josegonzalez","followers_url":"https://api.github.com/users/josegonzalez/followers","following_url":"https://api.github.com/users/josegonzalez/following{/other_user}","gists_url":"https://api.github.com/users/josegonzalez/gists{/gist_id}","starred_url":"https://api.github.com/users/josegonzalez/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/josegonzalez/subscriptions","organizations_url":"https://api.github.com/users/josegonzalez/orgs","repos_url":"https://api.github.com/users/josegonzalez/repos","events_url":"https://api.github.com/users/josegonzalez/events{/privacy}","received_events_url":"https://api.github.com/users/josegonzalez/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":{"url":"https://api.github.com/repos/cakephp/app/milestones/1","html_url":"https://github.com/cakephp/app/milestones/3.0.0","labels_url":"https://api.github.com/repos/cakephp/app/milestones/1/labels","id":527023,"number":1,"title":"3.0.0","description":"Stable release paired with cakephp/cakephp.","creator":{"login":"markstory","id":24086,"avatar_url":"https://avatars.githubusercontent.com/u/24086?v=3","gravatar_id":"","url":"https://api.github.com/users/markstory","html_url":"https://github.com/markstory","followers_url":"https://api.github.com/users/markstory/followers","following_url":"https://api.github.com/users/markstory/following{/other_user}","gists_url":"https://api.github.com/users/markstory/gists{/gist_id}","starred_url":"https://api.github.com/users/markstory/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/markstory/subscriptions","organizations_url":"https://api.github.com/users/markstory/orgs","repos_url":"https://api.github.com/users/markstory/repos","events_url":"https://api.github.com/users/markstory/events{/privacy}","received_events_url":"https://api.github.com/users/markstory/received_events","type":"User","site_admin":false},"open_issues":1,"closed_issues":166,"state":"open","created_at":"2014-01-05T21:59:52Z","updated_at":"2015-03-03T13:46:53Z","due_on":null,"closed_at":null},"comments":3,"created_at":"2015-03-03T08:28:18Z","updated_at":"2015-03-03T16:13:43Z","closed_at":null,"body":"This is something that could help local developers configure their setups much quicker. With the added support for DSNs in the core, it is now quite easy to get setup. \r\n\r\n## Why\r\n\r\nThis isn't a new way to configure applications. Many of us are already taking advantage of this in production through process managers such as Circus, SupervisorD, and Upstart. As well, all popular webservers support the usage of environment variables (`SetEnv` in Apache, and various ways with Nginx). In production at least, environment variables allow the changing of configuration *without* a deploy, which can be a godsend if, for example, your database goes away and you need to swap to a backup.\r\n\r\nThe other issue I'd like to tackle is different development environments for teams. It's annoying to have to change hardcoded config when moving from one database config to another (we don't all have `my_app:secret` setup), and having a `.env` file can obviate the need for people stepping over each other while changing config.\r\n\r\n## How\r\n\r\nWe can use [josegonzalez/php-dotenv](https://github.com/josegonzalez/php-dotenv), a library I maintain, or [vlucas/phpdotenv](https://github.com/vlucas/phpdotenv), which is used in Laravel. Both solve similar problems in different ways. The advantage with the former is that we have more control over features we want/need, though to be honest both would work adequately.\r\n\r\nUsing my library, we can add the following to our `bootstrap.php` file:\r\n\r\n```php\r\ntry {\r\n    josegonzalez\\Dotenv\\Loader::load([\r\n        'filepath' => __DIR__ . DS . '.env',\r\n        'toServer' => true,\r\n        'skipExisting' => ['toServer'],\r\n        'raiseExceptions' => true\r\n    ]);\r\n} catch (InvalidArgumentException $e) {\r\n    // do nothing in case the file doesn't exist\r\n}\r\n```\r\n\r\nHere is a sample `.env` file that I use for `vagrant` based installations:\r\n\r\n```shell\r\nexport APP_NAME=app\r\n\r\nexport DEBUG=2\r\nexport SECURITY_SALT=\"DYhG93b0qyJfIxfs2guVoUubWwvniR2G0FgaC9mi\"\r\nexport SECURITY_CIPHER_SEED=\"76859309657453542496749683645\"\r\n\r\nexport DATABASE_URL=\"mysql://my_app:secret@localhost/my_app?encoding=utf8&timezone=UTC&cacheMetadata=true&quoteIdentifiers=false&persistent=false\"\r\nexport DATABASE_TEST_URL=\"mysql://my_app:secret@localhost/my_app?encoding=utf8&timezone=UTC&cacheMetadata=true&quoteIdentifiers=false&persistent=false\"\r\n\r\nexport CACHE_URL=\"file:///CACHE/?prefix=APP_NAME_&duration=DURATION\"\r\nexport CACHE_CAKE_CORE_URL=\"file:///CACHE/persistent/?prefix=myapp_cake_core_&duration=DURATION&serialize=true\"\r\nexport CACHE_CAKE_MODEL_URL=\"file:///CACHE/models/?prefix=myapp_cake_model_&duration=DURATION&serialize=true\"\r\n\r\nexport LOG_DEBUG_URL=file:///LOGS/?levels[]=notice&levels[]=info&levels=[]=debug\r\nexport LOG_ERROR_URL=file:///LOGS/?levels[]=warning&levels[]=error&levels[]=critical&levels[]=alert&levels[]=emergency\r\n\r\nexport EMAIL_URL=\"mail://user:secret@localhost:25/?client=null&timeout=30&tls=null\"\r\n```\r\n\r\nI do a few replacements in my `app.php` file like so:\r\n\r\n```php\r\n'_cake_model_' => [\r\n    'url' => str_replace(\r\n        ['/CACHE/', 'DURATION'],\r\n        [CACHE, '+2 minutes'],\r\n        env('CACHE_CAKE_MODEL_URL')\r\n    ),\r\n],\r\n```\r\n\r\nI can likely make the library smarter and have it do it's replacements not only from `bash` but also from PHP constants or existing environment variables.\r\n\r\n## Errata\r\n\r\nWe'd need to add documentation on how `.env` files work. I have quite a bit of it in the readme for my project, but we'd want to add this to the main docs as well.\r\n\r\nNote that this would maybe increase the complexity of setting up an app. We don't want to promote a `.env` file in production, but we do want to show users how to configure apps in production. Something to consider.\r\n\r\nDevelopers might think this is something we've stolen from other frameworks, though this sort of thing has been around for quite a while. Rails has had automatic support for DSNs in environment variables since at least 3.x, and the `friendsofcake/app-template` project has used them for at least a year. As well, most/all of the core team has used this sort of functionality in the past, though maybe not with a php library to support their dev cycle.\r\n\r\nDoes adding this break BC? We've not yet made a release of the `app` composer project. Should we wait until a 3.1 CakePHP release before doing this? Or are we allowed to make changes like this without waiting for a major release.\r\n\r\nFinally, does everyone desire this change? I know this makes it easy for me to write my book, and probably makes tutorial writing slightly easier, but maybe this isn't right for our users. Thoughts?"},"comment":{"url":"https://api.github.com/repos/cakephp/app/issues/comments/76976253","html_url":"https://github.com/cakephp/app/issues/224#issuecomment-76976253","issue_url":"https://api.github.com/repos/cakephp/app/issues/224","id":76976253,"user":{"login":"jadb","id":33527,"avatar_url":"https://avatars.githubusercontent.com/u/33527?v=3","gravatar_id":"","url":"https://api.github.com/users/jadb","html_url":"https://github.com/jadb","followers_url":"https://api.github.com/users/jadb/followers","following_url":"https://api.github.com/users/jadb/following{/other_user}","gists_url":"https://api.github.com/users/jadb/gists{/gist_id}","starred_url":"https://api.github.com/users/jadb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jadb/subscriptions","organizations_url":"https://api.github.com/users/jadb/orgs","repos_url":"https://api.github.com/users/jadb/repos","events_url":"https://api.github.com/users/jadb/events{/privacy}","received_events_url":"https://api.github.com/users/jadb/received_events","type":"User","site_admin":false},"created_at":"2015-03-03T16:13:43Z","updated_at":"2015-03-03T16:13:43Z","body":"To outline the reasons I prefer it is conditional to being explicitly required by the end-user:\r\n\r\n1 - if not, a lot of users will end up having it but not knowing what it does or not using it.\r\n2 - keeps documentation to the package instead of adding more documentation to the already extensive one that cake has (the bigger the less translations will be complete).\r\n"}}