简体   繁体   中英

Setting up production/development environment in Laravel

I was collaborating with a colleague of mine on GitHub, it's a project on Laravel. Now I made some commits and sent this guy the Git link, he had a good look and had the following to say:

The migrations worked fine once I set the environment to development instead of production. What I'll often do, and you certainly don't have to, is set the default environment to development and set it to production only if a variable has been set in the virtual host file or in .htaccess.

Now I understood the part of setting up the default environment to development but what throws me totally off the hook is the word virtual host.

I read the documentation on Laravel about production environment, but what this person has said is a bit confusing to me. Can somebody clarify?

The same guy also sent me the below function :

$env = $app->detectEnvironment(function() {
        return getenv('APP_ENV') ?: 'local';
});

I am new to Laravel, and would really appreciate any help.

Environments

When your friend showed you the code:

$env = $app->detectEnvironment(array(
    'local' => array('homestead'),
)); 

It is referring to the portion of the framework that allows you to tie a host name ( homestead ) to a particular environment name ( local ).

You can find the file that has this code in your project at bootstrap/start.php . or look at it here on GitHub .

When you give an environment name to a particular hostname the framework will load configuration settings differently. If you look at the folder:

app/config/

You'll notice that there is a folder named local . That folder contains some configuration files that match those in the root of your app/config/ folder. When you launch the framework on a computer that has the host name that you specified in the start.php file, it uses any settings you give in the local/ version of the files before applying the regular config version.

This means if you use different database credentials for your local environment you can put those in the config/local/database.php config and they will only be used when launching the framework in your local environment.

The config items in your local directory cascade so you only need to add the configurations that are different from your production environment.

You can set up different environments by adding a new environment name and the hostname of the computer that it should run on:

$env = $app->detectEnvironment(array(
    'local'   => array('homestead'),
    'testing' => array('testserver')
)); 

Now I can create a folder called app/config/testing , add in a new database.php config file and specific the credentials for my testing database. When the framework is launched from the testing server any details from the testing config files will be used and then the remaining config details will come from the default (production) environment config files.

Also, Laravel 4.2 assumes app/config as your production environment, so when you launch the framework on your production server it will use all of the credentials in the config files at app/config

You can read more on how Laravel handles configurations here .

The Virtual Machine (Homestead)

The virtual machine your friend was talking about, homestead , is the official virtual development environment for Laravel. It's basically a virtual machine that is tuned specifically for working with laravel (though you can use it for non-Laravel projects too). Laracasts has a fee video talking about homestead that will help fill you in.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM