I have installed a fresh copy of Laravel.
I need it so i can set the environment to the URL.
So for instance in L4 i used this within the start.php
file
$env = $app->detectEnvironment(function()
{
return isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '';
});
This then loaded the correct environment for the project, so if we had a local domain dev.laravel.com
it would load in the environment variables from dev.laravel.com
I cannot do this within Laravel 5.
Any guesses how I can apply this?
You can for example do it for use different database, then you have some different variables on .env like:
DB_HOST=localhost
DB_DATABASE=database1
DB_USERNAME=foo
DB_PASSWORD=foo
DB_HOST_2=localhost
DB_DATABASE_2=database2
DB_USERNAME_2=foo
DB_PASSWORD_2=foo
and in config/dababase.php:
'connections' => [
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
'mysql2' => [
'driver' => 'mysql',
'host' => env('DB_HOST_2', 'localhost'),
'database' => env('DB_DATABASE_2', 'forge'),
'username' => env('DB_USERNAME_2', 'forge'),
'password' => env('DB_PASSWORD_2', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
and finally use:
Config::set('database.default', "mysql") or Config::set('database.default', "mysql2")
depending you need.
For example I use to change database connection after login user doing it at controller abstract class changing the value depending session var, that is changed after login depending type of user:
abstract class Controller extends BaseController {
use DispatchesCommands, ValidatesRequests;
public function __construct()
{
Config::set('database.default', Session::get('myapp.database','mysql'));
}
}
After login I put to change this value:
Session::set('myapp.database', 'mysql2') or Session::set('myapp.database', 'mysql')
and in the next call to controller the database connections is changed.
You need to put a .env
file in each of your environments - and put the specific config for those environments into the .env
file
So one one environment you might put
APP_ENV=local
APP_DEBUG=true
DB_PASSWORD=secret
and in another .env
on a different environment you might put
APP_ENV=staging
APP_DEBUG=false
DB_PASSWORD=other