This website uses cookies to ensure you get the best possible experience. See our Cookies Policy.
Forget app_dev.php In Your Symfony Application
I’m not a big fan of the typical app.php and app_dev.php setup in the Symfony Standard Edition. While it’s not a big deal if the app_dev.php file gets deployed, it shouldn’t be done anyway. It’s too risky and complicates the deploy process a bit.
The alternative: environment variables that default to production mode.
Rather than doing new AppKernel('prod', false) in a single index.php entrypoint, we’ll create a named constructor that fetches a few environment variables. The key here is to default to production mode so a botched deployment doesn’t ever expose debug information.
AppKernel.php
<?php
use Symfony\Component\HttpKernel\Kernel; class AppKernel extends Kernel { public static function fromEnvironment() { $env = getenv('APP_ENVIRONMENT') ?: 'prod'; $debug = filter_var(getenv('APP_DEBUG'), FILTER_VALIDATE_BOOLEAN); return new self($env, $debug); } // ... }
Now use that named constructor as your main entrypoint — app.php in Symfony standard, though I like to just use index.php. And remove the app_dev.php file.
index.php
To me, this method is easier to understand and removes a step from the deployment. Two fairly good things.<?php
use Symfony\Component\HttpFoundation\Request; // require autoloader or bootstrap require __DIR__ . '/../app/autoload.php'; // from the environment! $app = AppKernel::fromEnvironment(); // just like normal $request = Request::createFromGlobals(); $response = $app->handle($request); $response->send(); $app->terminate($request, $response);
Image credit Flickr
Interested in working with us? See our open engineering roles here.
Stay in touch
Subscribe to our newsletter
Posted by: Christopher Davis