This website uses cookies to ensure you get the best possible experience. See our Cookies Policy.

PMG Digital Made for Humans

Forget app_dev.php In Your Symfony Application

2 MINUTE READ | October 7, 2016

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

<?phpuse 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

<?phpuse Symfony\Component\HttpFoundation\Request;// require autoloader or bootstraprequire __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);
To me, this method is easier to understand and removes a step from the deployment. Two fairly good things.

Image credit Flickr

Interested in working with us? See our open engineering roles here.

Stay in touch

Bringing news to you

Subscribe to our newsletter


Posted by Christopher Davis

Related Content

thumbnail image

Get Informed

PMG Innovation Challenge Inspires New Alli Technology Solutions

4 MINUTES READ | November 2, 2021

Get Informed

Applying Function Options to Domain Entities in Go

11 MINUTES READ | October 21, 2019

thumbnail image

Get Informed

My Experience Teaching Through Jupyter Notebooks

4 MINUTES READ | September 21, 2019

Get Informed

Trading Symfony’s Form Component for Data Transfer Objects

8 MINUTES READ | September 3, 2019

Get Inspired

Working with an Automation Mindset

5 MINUTES READ | August 22, 2019

Get Informed

Parsing Redshift Logs to Understand Data Usage

7 MINUTES READ | May 6, 2019

Get Inspired

3 Tips for Showing Value in the Tech You Build

5 MINUTES READ | April 24, 2019

thumbnail image

Get Informed

Testing React

13 MINUTES READ | March 12, 2019

Get Inspired

Tips for Designing & Testing Software Without a UX Specialist

4 MINUTES READ | March 6, 2019

Get Informed

A Beginner’s Experience with Terraform

4 MINUTES READ | December 20, 2018

ALL POSTS