Laravel Unable to Detect Application Environment - php

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

Related

How to set up a staging environment based on a symlink to prod configuration, without showing debug information?

I would like to setup a staging environment with the same configuration as the prod environment.
According to the docs, I proceed as follow:
I create a staging symlink that points on prod
configure the env in .env: APP_ENV=staging
clear the cache: php bin/console cache:clear
ask for an URL that does not exist to trigger a 404 error: http://localhost:8080/an-url-that-does-not-exists
When the APP_ENV=prod, my custom error page is render properly, but when APP_ENV=staging, the debug message NotFoundHttpException is rendered?
The profiler is not displayed.
What am I missing?
tldr;
Crate an .env.staging file and use it to set APP_DEBUG to 0. Debug mode and environment are set independently.
By default, unless you set it explicitly debug mode (APP_DEBUG) is set from the environment automagically.
This happens in the following steps:
In your front-controller (index.php, usually) you would find this line:
(new Dotenv())->bootEnv(dirname(__DIR__).'/.env');
And on DotEnv::bootEnv() you'll find this:
$debug = $_SERVER[$k] ?? !\in_array($_SERVER[$this->envKey], $this->prodEnvs, true);
This will compare your APP_ENV with an array of "environments" that DotEnv, considers "production-like". By default, this array includes only prod.
You could modify the instance of the DotEnv by calling setProdEnvs():
(new Dotenv())
->setProdEnvs(['prod', 'staging'])
->bootEnv(dirname(__DIR__).'/.env');
... but generally simply disabling debug mode on your .env file would be enough.

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 4.2 Environments

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

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