Skip to content

Commit a75636d

Browse files
authored
Fix #417: Add .env for development without Docker (#458)
Co-authored-by: samdark <47294+samdark@users.noreply.github.com>
1 parent 59f7c2f commit a75636d

File tree

12 files changed

+203
-23
lines changed

12 files changed

+203
-23
lines changed

.env.example

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Local environment configuration.
2+
# Copy this file to .env and adjust as needed.
3+
# In production, set environment variables via server or container configuration instead.
4+
APP_ENV=dev
5+
APP_DEBUG=true

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,6 @@ Thumbs.db
2424

2525
# Codeception C3
2626
c3.php
27+
28+
# Local environment configuration
29+
.env

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- Chg #449: Remove `yiisoft/data-response` dependency (@vjik)
1010
- Chg #443: Do not write logs to file since that's not needed for both Docker and `./yii serve` (@samdark)
1111
- Enh #456: Add "service update paused" case for swarm deployment log parsing (@samdark)
12+
- Enh #417: Add `.env` for development without Docker (@samdark)
1213

1314
## 1.2.0 February 20, 2026
1415

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,25 @@ cd myproject
4747
> [!NOTE]
4848
> Ensure that Composer is executed with the same PHP version that will be used to run the application.
4949
50+
Copy the example environment file and adjust as needed:
51+
52+
```shell
53+
cp .env.example .env
54+
```
55+
5056
To run the app:
5157

5258
```shell
53-
APP_ENV=dev ./yii serve
59+
./yii serve
5460
```
5561

5662
Now you should be able to access the application through the URL printed to console.
5763
Usually it is `http://localhost:8080`.
5864

65+
> [!TIP]
66+
> The `.env` file is for local development only and is excluded from version control.
67+
> In production, configure environment variables via your server or container instead.
68+
5969
### Installation with Docker
6070

6171
> [!WARNING]
@@ -110,6 +120,7 @@ src/ Application source code.
110120
Web/ Web-specific code (actions, handlers, layout).
111121
Shared/ Shared web components.
112122
Layout/ Layout components and templates.
123+
bootstrap.php Application bootstrap (autoloading, environment setup).
113124
Environment.php Environment configuration class.
114125
tests/ A set of Codeception tests for the application.
115126
Console/ Console command tests.

composer-dependency-analyser.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
->disableComposerAutoloadPathScan()
1111
->setFileExtensions(['php'])
1212
->addPathToScan($root . '/src', isDev: false)
13+
->addPathToExclude($root . '/src/bootstrap.php')
14+
->addPathToScan($root . '/src/bootstrap.php', isDev: true)
1315
->addPathToScan($root . '/config', isDev: false)
1416
->addPathToScan($root . '/public/index.php', isDev: false)
1517
->addPathToScan($root . '/yii', isDev: false)

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
"yiisoft/yii-view-renderer": "^7.4.1"
6767
},
6868
"require-dev": {
69+
"vlucas/phpdotenv": "^5.6",
6970
"codeception/c3": "^2.9",
7071
"codeception/codeception": "^5.3.5",
7172
"codeception/module-asserts": "^3.3.0",

public/index.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
$root = dirname(__DIR__);
1414

15-
require_once $root . '/src/autoload.php';
15+
require_once $root . '/src/bootstrap.php';
1616

1717
if (Environment::appC3()) {
1818
$c3 = $root . '/c3.php';

src/Environment.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,19 +78,18 @@ public static function appDebug(): bool
7878

7979
private static function setEnvironment(): void
8080
{
81-
$environment = self::getRawValue('APP_ENV');
81+
$environment = self::getRawValue('APP_ENV') ?: self::PROD;
8282

8383
if (!in_array($environment, self::ENVIRONMENTS, true)) {
84-
if ($environment === null) {
85-
$message = 'APP_ENV environment variable is empty.';
86-
} else {
87-
$message = sprintf('APP_ENV="%s" environment is invalid.', $environment);
88-
}
89-
90-
$message .= sprintf(' Valid values are "%s".', implode('", "', self::ENVIRONMENTS));
91-
92-
throw new RuntimeException($message);
84+
throw new RuntimeException(
85+
sprintf(
86+
'APP_ENV="%s" is invalid. Valid values are "%s".',
87+
$environment,
88+
implode('", "', self::ENVIRONMENTS),
89+
),
90+
);
9391
}
92+
9493
self::$values['APP_ENV'] = $environment;
9594
}
9695

src/autoload.php

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/bootstrap.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use App\Environment;
6+
7+
require_once dirname(__DIR__) . '/vendor/autoload.php';
8+
9+
// Load .env for non-Docker/non-container environments.
10+
// Existing process environment variables take precedence (Docker, CI, server config).
11+
// phpdotenv is a dev dependency — not available in production (composer install --no-dev).
12+
if (empty($_ENV['APP_ENV']) && class_exists(\Dotenv\Dotenv::class)) {
13+
\Dotenv\Dotenv::createImmutable(dirname(__DIR__))->safeLoad();
14+
}
15+
16+
Environment::prepare();

0 commit comments

Comments
 (0)