Skip to content

Commit

Permalink
Changes required for a Symfony project to run on ZEIT now.
Browse files Browse the repository at this point in the history
* Add a `now.json` file with the required fields. Also add the environment variables here.
* Add a `.nowignore` file to ignore the `var` and `vendor` directories among others (.env files also recommended). This will increase the upload/deployment time.
* Change the cache and log directories in the Kernel: Edit the two methods in `src/Kernel.php`.
* Add an `elseif` statement to `config/bootstrap.php` to do nothing if `NOW_REGION` exists and `NOW_REGION!=="dev1"`. This will prevent errors from the Dotenv package.
  • Loading branch information
ties-v committed Jul 20, 2019
1 parent 04e5990 commit e3d5cd4
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 1 deletion.
7 changes: 7 additions & 0 deletions .nowignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
vendor
var
.git
tests
appveyor.yml
.env*

3 changes: 3 additions & 0 deletions config/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
// Run "composer dump-env prod" to create it (requires symfony/flex >=1.2)
if (is_array($env = @include dirname(__DIR__).'/.env.local.php')) {
$_ENV += $env;
} elseif (isset($_ENV['NOW_REGION']) && $_ENV['NOW_REGION'] !== 'dev1') {
// Load env variables from the now environment, so no operation needed.
error_log('Symfony notice: Use environment variables from Now platform.');
} elseif (!class_exists(Dotenv::class)) {
throw new RuntimeException('Please run "composer require symfony/dotenv" to load the ".env" files configuring the application.');
} else {
Expand Down
2 changes: 1 addition & 1 deletion config/packages/prod/monolog.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ monolog:
excluded_http_codes: [404]
nested:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log"
path: "php://stderr"
level: debug
console:
type: console
Expand Down
25 changes: 25 additions & 0 deletions now.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "symfony-demo",
"version": 2,
"env": {
"APP_ENV": "prod",
"APP_DEBUG": "0",
"APP_SECRET": "secret123",
"DATABASE_URL": "sqlite:///%kernel.project_dir%/data/database.sqlite",
"MAILER_URL": "null://localhost"
},
"builds": [
{ "src": "public/index.php", "use": "@juicyfx/php@canary", "config": { "mode": "cgi", "composer": true } },
{ "src": "public/!(*.php)", "use": "@now/static" },
{ "src": "public/**/!(*.php)", "use": "@now/static" }
],
"routes": [
{ "src": "/apple-touch-icon.png", "dest": "public/apple-touch-icon.png"},
{ "src": "/favicon.ico", "dest": "public/favicon.ico"},
{ "src": "/robots.txt", "dest": "public/robots.txt"},
{ "src": "/build/(.*)", "dest": "public/build/$1"},
{ "src": "/(.*)", "dest": "public/index.php" }
],
"public": true,
"scope": "ties"
}
6 changes: 6 additions & 0 deletions src/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,17 @@ class Kernel extends BaseKernel

public function getCacheDir()
{
if (isset($_SERVER['NOW_REGION']) && $_SERVER['NOW_REGION'] !== 'dev1') {
return '/tmp/symfony/cache/'.$this->environment;
}
return $this->getProjectDir().'/var/cache/'.$this->environment;
}

public function getLogDir()
{
if (isset($_SERVER['NOW_REGION']) && $_SERVER['NOW_REGION'] !== 'dev1') {
return '/tmp/symfony/log';
}
return $this->getProjectDir().'/var/log';
}

Expand Down

0 comments on commit e3d5cd4

Please sign in to comment.