So, for my application I see 4 types of environments it can run under:
Local on my machine
Local on my VM where I am using Homestead
Staging
Production
Each environment differs in the database connectivity (type of DB, connectivity to the DB credentials, etc).
In my bootstrap/start.php I have set up this:
$env = $app->detectEnvironment(array(
'local' => array('localhost'),
'localStaging' => array('mylocalStage.dev'),
'staging' => array('myapp-stage.com'),
'production' => array('myapp.com'),
));
How do I enforce those settings? where do I set up that when I'm on 'local' I will use some local settings? Currently when I use
php artisan env
on my root application folder I get:
Current application environment: production
why isn't it picking up I'm 'local'?
After discussing the issue in the comments and in the chat, the conclusion was to use the new .ENV approach which have been introduced in laravel 5.
Which means, you should have an .env file in your root directory with the current environment's settings.
So for local env, your .env file would have different settings than your production's env .env file.
As default you should have a .env.example file, in case you don't have one - you can copy the default content from the git (.env.example#git).
Please notice that when you're updating (pushing) your local version to staging (for instance), that you don't overwrite the .env file. In order to prevent those kind of issues you can use git ignore (so it will skip that file).
Related
In my PHP Laravel 5.3 app I have my config settings in my .env file with the
APP_ENV=local which can be changed to APP_ENV=production when my app is in production/live mode.
In that .env file I also have a MAIL_DRIVER=preview setting which gets pulled into my config/mail.php config file with env('MAIL_DRIVER', 'smtp') like this:
return [
'driver' => env('MAIL_DRIVER', 'smtp'),
]
So now my question is, when I change my .env setting APP_ENV=local into APP_ENV=production
How can I make it load a different env('MAIL_DRIVER') setting based on that env('APP_ENV') setting?
is there a way to load different .env files for each environment or different config files or how do you handle this in Laravel 5.3.
I remember in older versions of Laravel you simply created a new folder in the config folder for each environment but the whole config system is different from those older versions.
With Laravel 5, you have a different copy of your .env file in each environment.
This file is NOT committed into your repository. Rather, your .env.example file is, and that is what you make a copy of and name as .env in the environment.
In previous versions of Laravel (i.e. <= 4) you could have separate environment files in the same checked out copy of the project and switch between environments, but that doesn't actually make much sense.
Keep your .env.example file up to date with all the options you will need in your app, and initialize them to empty values. When you deploy to a new environment, you copy it as a new file, which keeps you from accidentally committing credentials to your repo and keeps things simple:
cp .env.example .env
Then edit the file and set the values to be appropriate in that specific environment. For example, instead of using test API keys, you may use the production keys for some service. Editing, for example:
sudo vim .env # If you like VIM
or
sudo nano .env # If you like NANO
An exception to what I just said above is with testing. Per the documentation:
You may also create a .env.testing file. This file will override values from the .env file when running PHPUnit tests or executing Artisan commands with the --env=testing option.
In that case though, having your .env.testing file in your repo is most likely acceptable assuming it doesn't contain any sensitive production values, which it probably shouldn't anyway.
I'm trying to figure out how I can get my environment set to development mode inside my Laravel application.
bootstrap/start.php
$env = $app->detectEnvironment(array(
'development' => array('blog.app'),
'production' => array('')
));
Inside my routes file I have the following:
Route::get('/', function()
{
dd(App::environment());
});
When I go to blog.app it says its set to production and I'm not sure why. The purpose of figuring out why its giving me production instead of development is because I am attempt to run the command "php artisan migrate" and its giving me the following.
My-iMac:blog my$ php artisan migrate
**************************************
* Application In Production! *
**************************************
Do you really wish to run this command?
So I need help understanding why its still saying application is in production mode when I'm attempting to be in development mode.
Go inside your project directory. Then navigate to bootstrap/start.php.
Edit the local array specified in $app->detectEnvironment with your machine name.
Laravel will automatically check you machine name every time and know that it is a local environment and use the local configuration files instead. To use the local config files you can navigate to app/config/local and edit the files there. The files in app/config belong to the production environment. You can copy these files in the local folder and then edit them accordingly, for example you database configuration probably will differ on your local machine and the server.
You can simply edit the files in the config folders and Laravel will detect automatically the environment according to the array in the start.php and use the config files related to the machine on which you are working. You can also specify more environments and create their entries in the start.php file and then go and create a folder by the same name in the app/config folder and create config files for that environment in that folder.
Example: If you want to have two local environments you can edit you start.php file like so
$env = $app->detectEnvironment(array(
'local' => array('Machine1', 'Machine2'),.
'local2' => array('Machine3', 'Machine4'),
));
And then navigate to the app/config folder and create two folders like local and local2 and have separate files for each of them. Now the machines with names Machine1 and Machine2 will automatically use config files present in local folder and Machine3 and Machine4 will use config files in local2 folder. This way Laravel provides great flexibility in setting environments.
For more information check out the Environments and Configuration Episode on Laracasts.
You need to figure out your machine name. In order to do that, run this code anywhere:
echo gethostname();
exit;
More info about the function: http://php.net/manual/en/function.gethostname.php
It will output your machine name, then you need to paste it in the 'development' array.
My laravel 4 project has multiple environments set up.
The environments setup are: development is called 'development' and production is called 'production'.
In my app/config folder, I have two extra folders. One for each name of the environment.
See screenshot:
Inside these folders I have separate config files for database.php and app.php.
These are working as expected. Laravel uses my local database configuration when running locally and production when running live.
Now, I've added a new package which requires a config file. This resides in /app/config/packages/greggilbert/config.php.
I need to be able to set different config files the same way I do for the database. So, I assumed creating the same file structure in /development or /production would work, but this does not seem to be the case.
Here is a new screenshot:
Package-related, environment-specific configuration files have to be placed in the package's directory within a subdirectory named after the environment. So in your case this would be
app
- config
- packages
- greggilbert
- recaptcha
- development
- config.php
- production
- config.php
But it can get really ugly and cumbersome in some cases. This can be solved in a (in my opinion) cleaner and more elegant way through enviroment files in your application root directory:
/.env.development.php
<?php
return [
// any configuration settings for your local environment
'RECAPTCHA_TEMPLATE' => 'customCaptcha',
'RECAPTCHA_LANGUAGE' => 'en',
];
That way, you could leave the package directory as it is:
app
- config
- packages
- greggilbert
- recaptcha
- config.php
Laravel will load the environment file according to the environment and make the environment variables globally available through getenv(). So now you could just grab the specified template for the current environment with getenv('RECAPTCHA_TEMPLATE') from the config.php.
I am trying to set up multiple environments on a Laravel 4 app, and naturally different Databases depending on which environment we are on.
For my local machine, I set up a virtual host for "local.elders.dev"
Unfortunately, for some reason the following code is not working.
$env = $app->detectEnvironment(array(
'local' => array('http://local.elders.dev'),
));
Maybe I need to run an artisan command or something else. I feel I am close, but not quite there yet !
Thanks to all !
Another method is to use the name of the folder the project is in. This works in console and web. 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,
));
A big advantage is that you can move staging to production by simply renaming or copying the project folder without changing files, db records or environment variables.
I know this is answered but for others looking for a solution...
My environment detection setting looks like this:
$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 handy because it will work on any project as long as I use "local" for development (like "localhost", "localhost:8000", "my.app.local", etc.). Same goes for "stage". And production is default so anything without "local" or "stage" works for production.
Handling multiple environments on the same machine with Laravel 4x
What if you wanted to run multiple environments on the same machine with the same name- for example, a staging and production AND local environment?
There is a better solution for handling environments in Laravel 4x- and it can be done by adding a one liner to you vhosts file- or .htaccess:
Set local environment variable
In vhost or .htaccess add for your local installation, for staging, for example add:
SetEnv LARAVEL_ENV staging
and the same in your production .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 didn't forget local... and that's the the cool part-- your installation will default to local if neither environment variable is found in vhost or .htaccess, as they would be found in the other installations.
OK ! I just solved the issue... The code was actually working ok ! The problem is that I was using
$_SERVER['DB1_HOST'] //for Pagodabox.
Of course this was not set on my local environment, which pretty much broke the app...
I fixed by simply doing :
isset($_SERVER['DB1_HOST']) ? $_SERVER['DB1_HOST'] : '';
Thanks to #jeroen and #theshiftexchange :)
Try replacing it with 'local.elders.dev', I'm not 100% sure but it's probably matching hostnames, not full paths.
Laravel 4 detects the environments through the machine names specified in the "bootstrap/start.php" file.
For example, in my case the 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' (a Mac) and 'Victor-PC' (Windows).
In order to know the current machine name, you can use the following PHP code:
<?php echo gethostname(); ?>
For each environment you can create a folder in app/config and replace the desired configuration files and properties.
Regards!
I sometimes work with Codeigniter, and after I'm done developing on the local site, I need to migrate the files to the server. All the files in the /config/ folder need to be changed to match the Server settings. It's not right to make an entire commit for these changes, do I simply make Git ignore these files altogether, or is there a way to track these changes and apply them with Git at the right moment?
You could keep versioned:
two "value config files", with the right values for each environment
a template config file, with value placeholder in it (for instance, ##PORT_NUMBER##)
a script able to generate the actual config file depending on the current environment
a content filter driver which, on checkout, will trigger the script in order to generate the right config file.
Note: that supposes your template config file has a recognizable content (the filter doesn't have a name or path of the file). See "Git equivalent of subversion's $URL$ keyword expansion" for more on the limitation of git content filter driver).
It depends on Your needs. In my company we use other approach.
We've created several environments (where the asterix is internal project name):
devel - site runs on domain **.local.com*
test - site run on domain test.*.company.com
beta - beta.*.company.com
production - every other domain.
Based on the domain name we switch automatically configurations.
Basicly config file looks like:
<?php
return array(
'_env' => array(
'devel' => array(
// config for devel
),
'production' => array(
// config for production
)
)
);
?>
Some frameworks (AFAIR Zend) set the environment name in Virtual Host config (or .htaccess). You should look at: zend framework auto switch production staging test .. etc
Have You looked at CI documentation? There's a section about it.
Create two folders in the config folder. One is called development and the other is production. Now copy config.php, database.php etc to each of these folders. Now when you are on production server, CodeIgniter will first check the production folder for the files. If it is not there, then it uses the default file in the config folder. And if you are on development environment, CodeIgniter will first check the development folder.
If you want to keep any config file identical to the production and development environment, keep it in config folder.
If you want to set the environment then add the following code in .htaccess file:
#This code for Development Environment
SetEnv CI_ENV development
and
#This code for Production Environment
SetEnv CI_ENV production