space97
space97

Reputation: 303

What is the right way to control a Symfony 3.4 Framework to go into production or development mode?

So I have set up a Symfony 3.4 application, which I downloaded following the instructions here using the Symfony Installer on my local machine:

https://symfony.com/download

I have gotten a good deal of code up and running in it locally on my laptop.

My intention with this application is to get it to run along side other older projects for my employer on the same server. These older projects have to be developed on remote development servers which means that a development version of my Symfony app will have to sit on a remote development server as well.

When I access my project on my local machine Symfony automatically bootstraps through /web/app_dev.php. But when I put it on the remote development server it automatically bootstraps with /web/app.php, as if I was in production.

I see in both of these files where symfony declares the kernel and selects development vs production mode, which I guess I could write logic around to make work differently:

// line 24 in /web/app_dev.php
$kernel = new AppKernel('dev', true);

// line 10 in /web/app.php
$kernel = new AppKernel('prod', false);

But it seems like the right way to do this would be to make traffic go to /web/app_dev.php on the development server, rather than messing with the logic. I still plan to use /web/app.php for production, which will be on another server.

What is the right way to my development server to dev mode? I do not see where symfony is determining this.

Thanks for your help.

Upvotes: 0

Views: 454

Answers (2)

Chip Dean
Chip Dean

Reputation: 4302

You have a several options for this. What you decide is based on personal preference and your requirements. In my case I modified the app.php file to read an environmental variable and then set the appropriate environment. I set the environmental variable in the virtual host for the application.

You can get the environmental variable like this:

$env = strtolower(getenv('SYMFONY__ENV'));

Then you can just create the kernel like this:

$kernel = new AppKernel($env, $env === 'dev');

By the way routing traffic to app_dev.php will not work by default on a remote server because there is a conditional that checks to make sure traffic is coming from localhost. So if you decide to go that route you will need to modify the app_dev.php file to remove that check otherwise you will get 403 forbidden errors. I recently encountered the same issue.

Hope that helps!

Upvotes: 1

yourcomputergenius
yourcomputergenius

Reputation: 333

Typically you would set your environment variable in a .yml (or .php, or .xml depending on your file preference choice) file and leave it fixed for the particular deployment.

Then you have multiple deployments that are either in production or development.

An alternative method would be to have the environment determined by the url used to access, which you are seeing with the local access determining development mode automatically, and the "remote" access determining production mode automatically. But it makes more sense in most cases to have each deployment fixed.

Upvotes: 1

Related Questions