Laravel 4.2 Environments - php

I've set 2 environments for my app, a local development on my machine and the production environment on the server. I set them in bootstrap/start.phplike this:
$env = $app->detectEnvironment(array(
'ariel_dev' => array('Ariels-MacBook-Pro.local'),
'production' => array('my.server.hostname'),
));
I've also set a folder under app/config/ariel_dev where I put the config files which I want to overwrite, like database.php.
I've test them, and they work pretty good. The problem is that my hostname changes when I'm home and when I'm at the office (I'm using a Mac). So the app doesn't match my dev environment and defaults to the production environment, connecting to the database of the server.
I'm doing something wrong? Doesn't it suppose to throw an error or something? Do I've to create a production folder under config?
Hope someone helps!

The default environment is always production, so you can leave it out of the array. For the other problem of having two different hostnames for you local dev, you can add them as array values of for the array local environment key.
$env = $app->detectEnvironment(array(
'ariel_dev' => array('Ariels-MacBook-Pro.local', 'myHomeHostname'),
));

Remember that production is also used as a fallback--it's pretty dangerous as you've configured it.
http://laravel.com/docs/configuration#environment-configuration states:
The default environment is always production
So try something like this:
$env = $app->detectEnvironment(array(
'productionserver' => array('my.server.hostname'),
'ariel_dev' => array('Ariels-MacBook-Pro.local'),
));
and then use productionserver as the folder instead of ariel_dev

Related

ZF3 Development Mode VS Production Mode

I use ZF3 and code in the development mode. I configured it like the tutorial suggests:
composer development-enable
So everything works fine if this mode is enabled. If I disable it I get a database connection error, like this one:
Connect Error: SQLSTATE[HY000] [1044] Access denied for user
''#'localhost' to database 'xyz'
I still work on the same computer.
So what error it might be?
The main topic would be, how is the right way to change between development and production, does the composer statement also make clear to use the production configfiles?
If I have changed the mode via composer, what do I have to do additional? I really blueeyed thought, it would be enough to just disable:
composer development-disable
Do I have to rename the development config files also? Of which files do we talk about? Is it just application-config.php and development-config.php?
Where and how should I place the different database connections? I now use the files you see above.
And last, how to change the mode on the production server? I now just disabled the mode on my developmentsystem and then uploaded the hole project. Afterwards I only upload the changed files.
EDIT1: Here additional a screensot, which configuration files I use in which folders:
In my application.config.php the configuration links to:
'config_glob_paths' => [
realpath(__DIR__) . '/autoload/{{,*.}global,{,*.}local}.php',
],
and in my development.config.php the configuration links to
'module_listener_options' => [
'config_glob_paths' => [realpath(__DIR__) . '/autoload/{,*.}{global,local}-development.php'],
'config_cache_enabled' => false,
'module_map_cache_enabled' => false,
],
for me it looks correct. My database connection is in local.php (for the production) and in local-development.php (for the development mode).
Enabling/Disabling the mode is just the same as having/not having the config/development.config.php file.
If you look closely, you'll see that the development mode disables the cache.
Your problem is that the cache files have been created (non dev mode) while the configuration wasn't fine for the environment. Remove data/cache/application.config.cache and application.module.cache as configured in config/application.config.php.
If you use development-mode enable (Development) it mean config_cache_enabled set to false. So your new configuration like module, services, controllers, etc will load by ZF3, because ZF3 will not read the configuration from cache (in data/cache/*).
If development-mode disable (Production) configuration will be cached, so when you deploy your code with new configuration like I mention above, will not read by ZF3. Because ZF3 still read the configuration in cache.
I usually remove the cache when deploying to Production. Here the sample shell script I used for deploying
#/bin/bash
rsync --exclude data --exclude .git -av temp_example.com/. /var/www/example.com/.
echo -e "Removing cache..."
rm -f /var/www/example.com/data/cache/*.php
So, the main key, if you used development-mode disable, just remove the cache after deploying the code.

Laravel 4 - environment configuration

I am working on a Laravel 4 project and I need to be able to switch between multiple configurations. So as far as I know Laravel enables me to configure envs based on URL like this in the start.php
$env = $app->detectEnvironment(array(
'local' => array('localhost'),
'stage' => array('project.stage.com'),
'prod' => array('project.production.com'),
));
And each of this configs consists of separate database connections and other configuration files. What I want is on my local environment to be able to switch between local,stage and prod, so for example if I want to connect to the prod database from my local project to test something. As far as I can understand if I want to do this I need to manualy switch the database connection strings in the local configuration. Is there any other way for switching between configurations on local level ? Hope my question was clear.
You can pass a closure to the function to determine set the environment more dynamically. You can either replicate the logic laravel uses and only use the closure in combination with gethostname() or just comment the part out and add this for testing:
$app->detectEnvironment(function(){
return 'stage';
});

Laravel doesn't detect my local (development) database config file

My Laravel application is running on my local computer. When I run my app, Laravel uses my production database configuration and throws an Exception:
SQLSTATE[HY000] [1045] Access denied for user 'production_admin'#'localhost' (using password: YES)
This is strange because when I check current environment using artisan it says :
Current application environment: development
and my development database config file has a different username and password.
also I have stated in bootstrap/start.php that :
$env = $app->detectEnvironment(array(
'development' => array('*.dev', gethostname()),
'production' => array('*.com', '*.net', '*.org', '*.ir')
));
I don't know why Laravel insist on using production configuration although I'm in development environment.
Thanks for your help.
The best way to avoid this issue is to do the following:
in /bootstrap/start.php
Replace the default environment detection with:
$env = $app->detectEnvironment(function()
{
return getenv('APP_ENV') ?: 'local';
});
This will check for the presence of an environmental variable which is set by apache/nginx assuming you are running Homestead or similar.

Laravel Unable to Detect Application Environment

I have three environment folders in the config directory in Laravel: development, testing, and local. When I change the environment and hostname within the start.php file, only the production environment is recognized. Any thoughts on what I might be doing wrong here?
app/routes.php
//using this as a test to confirm that the correct environment is being used
Route::get('/', function()
{
var_dump(App::environment());
});
bootstrap/start.php:
$env = $app->detectEnvironment(array(
'development' => array('localhost'),
));
Here, the result should be "string(11) "development". Instead, it's "string(10) "production".
localhost probably isn't the real hostname of your machine.
You can find out your hostname by echoing gethostname(). Then use that instead of localhost

Laravel 4.2 says my application is in production. How do I turn this off?

I have a fresh install of Laravel. When running php artisan migrate:refresh I get a message saying Application In Production! Do you really wish to run this command?'
I know this is an update in 4.2, however I can't figure out how to turn it off.
I found in the source that it comes from Illuminate\Console\ConfirmableTrait and runs if this if test passes : if ($this->getLaravel()->environment() == 'production')
I'm not sure why it thinks I'm in production. I never setup any environments. This is the default environment detection, which I'm still currently using.
$env = $app->detectEnvironment(array(
'local' => array('homestead')
));
Also, if I set a production environment to a hostname that isn't my machine, I still have the same problem.
Just specify a machine name for the host that matches a given environment, then laravel will automatically detect the environment (default is production), for example:
$env = $app->detectEnvironment(array(
//'local' => array('homestead'),
'local' => array('*.dev', gethostname()),
'production' => array('*.com', '*.net', 'www.somedomain.com')
));
Read the documentation and this answer as well.
Setting your environment to something other than production is The Right Way. See the accepted answer.
But, if you're looking for A Quick Fix you can use (in UNIXoid environments):
yes | php artisan migrate:refresh
All this does is send a stream of "y" to the program, which acts like you pressed "y" when prompted.
I find this to be a little better than --force, as not all the artisan commands support force.
In case if anyone stumbled upon this question while searching for similar problem in a lumen installation I'd suggest to check the .env file and add APP_ENV=local if its not already there. It solved my problem.
Hopefully this will help someone else. I suddenly had an issue where my dev site I was building stopped connecting to the DB saying:
PDOException SQLSTATE[HY000] [1049] Unknown database 'forge' failed
I was also receiving errors such as the OP when trying to run artisan migrate:refresh etc, the error was stating that i was in production etc etc.
After much head scratching (!) I found that my hostname value set inside the /bootstrap/start.php was wrong, because my hostname had changed on my macbook pro!? I have no idea how but it changed from something like RobMacbookPro2.local to RobMacbookPro.local. This meant it fell back to production thus loading the incorrect database.php file with the standard DB=forge (which was wrong)
Check this guide:
http://laravel.com/docs/4.2/configuration
Pay particular attention to the code:
<?php
$env = $app->detectEnvironment(array(
'local' => array('your-machine-name'),
));
On a mac and probably linux? you can determine your hostname by typing # hostname in terminal.
Hope that saves someone some time!

Categories