How to set up different environments on Laravel 4?

I am trying to set up several environments in a Laravel 4 application and, of course, different databases, depending on which environment we are in.

For my local machine, I configured a virtual host for "local.elders.dev"

Unfortunately, for some reason, the following code does not work.

$env = $app->detectEnvironment(array( 'local' => array('http://local.elders.dev'), )); 

Maybe I need to run the artisan command or something else. I feel like I'm there, but not quite there yet!

Thanks everyone!

+4
source share
6 answers

OK! I just solved the problem ... The code really worked fine! The problem is that I used

 $_SERVER['DB1_HOST'] //for Pagodabox. 

Of course, this was not installed in my local environment, which pretty much violated the application ...

I fixed it simply:

 isset($_SERVER['DB1_HOST']) ? $_SERVER['DB1_HOST'] : ''; 

Thanks @jeroen and @theshiftexchange :)

+1
source

Another method is to use the name of the folder in which the project is located. It works in the console and on the Internet. I found this to be the only reliable way if different environments are hosted on the same server.

 $env = $app->detectEnvironment(array( 'staging' => strpos(getcwd(), '/staging')>-1, 'acceptance' => strpos(getcwd(), '/acceptance')>-1, 'production' => strpos(getcwd(), '/production')>-1, )); 

The big advantage is that you can move the statement to production by simply renaming or copying the project folder without changing the files, db records or environment variables.

+4
source

I know this is the answer, but for others looking for a solution ...

Setting up my discovery environment is as follows:

 $env = $app->detectEnvironment(array( // Development // any machine name with the term "local" will use the local environment 'local' => array('*local*'), // Stage // any machine name with the term "stage" will use the stage environment 'stage' => array('*stage*') // Production // production is default, so we don't need to specify any detection 

));

This is convenient because it will work on any project if I use "local" for development (for example, "localhost", "localhost: 8000", "my.app.local", etc.). The same goes for the "scene". And production is defaulted, so everything without a β€œlocal” or β€œstage” works for production.

+3
source

Handling multiple environments on one computer with Laravel 4x

What if you want to run several environments on the same computer with the same name, for example, intermediate and production and local environments?

The best solution for handling environments in Laravel 4x - and this can be done by adding one liner to you vhosts file- or .htaccess:

Set local environment variable

In vhost or .htaccess, add an intermediate for your local installation, for example:

 SetEnv LARAVEL_ENV staging 

and the same in producing .htaccess or vhost:

 SetEnv LARAVEL_ENV production 

Then the usual detectEnvironment () function in start.php.

 $env = $app->detectEnvironment(function() { // Default to local if LARAVEL_ENV is not set return getenv('LARAVEL_ENV') ?: 'local'; }); 

We have not forgotten local ... and the cool part is that your default setting will be local if no environment variables are found in vhost or .htaccess, as they will be found in other settings.

+3
source

Try replacing it with 'local.elders.dev' , I'm not 100% sure, but it probably matches the hostnames, not the full paths.

0
source

Laravel 4 discovers environments through the machine names specified in the file "bootstrap / start.php".

For example, in my case, config becomes:

 $env = $app->detectEnvironment(array( 'local' => array('Victor.local', 'Victor-PC'), )); 

This means that Laravel will use the "local" environment settings for both machines: "Victor.local" (Mac) and "Victor-PC" (Windows).

To find out the name of the current computer, you can use the following PHP code:

<?php echo gethostname(); ?>

For each environment, you can create a folder in the / config application and replace the necessary configuration files and properties.

Hello!

0
source

All Articles