2

I've to deploy several instances of a Laravel app to a unique server. Each instance requires a different database configuration. The default Laravel's environment configuration based on hostnames doesn't work for me because all the apps are going to be on the same server, so there's no way to tell which config fiel to use. Here is my bootstrap/start.php file:

$env = $app->detectEnvironment(array(

    'development' => array('Ariels-MacBook-Pro.local'),
    'server' => array('srv-hostname'),

));

It would be great that I can define the environment based upon a domain (because my apps area gonna be on diferent domains), so in this way I can define a different config for each domain (hosted on the same server)

Any ideas?

DavidT
  • 2,341
  • 22
  • 30
arielcr
  • 1,663
  • 2
  • 23
  • 34
  • Are you using Nginx? or Apache? – DavidT Sep 23 '14 at 21:16
  • Ok, Im not sure how you do this with Apache, but in Nginx you can set environment variables in your location blocks. Meaning a different variable for different sub-domains, domains or url patterns. Then get this variable in your `detectEnvironment` method. There must be a way to do the equivalent in Apache. Maybe someone else here will know. If not look that up maybe. – DavidT Sep 23 '14 at 21:24
  • [This](http://stackoverflow.com/questions/10902433/setting-environment-variables-for-accessing-in-php) may help you combined with [this](http://stackoverflow.com/questions/12886683/htaccess-in-multiple-environments) – DavidT Sep 23 '14 at 21:26
  • so this is the same app, or diffrent app's on the same server?? i would look at the app/config file and make different config builds for each instance type (this is assuming your build is hosted like /var/www/laravel/ (diffrent apps here)) – Zapps Ceo Sep 23 '14 at 22:19
  • It is the same app, but in different folders, for example /var/www/app1/ /var/www/app2/ /var/www/app3/ . How do I make different config builds ? – arielcr Sep 23 '14 at 22:23

2 Answers2

6

Laravel's detectEnvironment method has a nify feature where you can progamatically determine the current enviornment with a closure. For example, this would configure Laravel to always use the local enviornment.

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

The domain name should be somewhere in $_SERVER, so something like this untested pseudo-code should get you what you want.

$env = $app->detectEnvironment(function()
{
    switch($_SERVER['HTTP_HOST'])
    {
        case: 'example.com':
            return 'production';
        case: 'another.example.xom':
            return 'production';                    
        default:
            return 'local'; //default to local
    }
});
Nate-Wilkins
  • 5,364
  • 4
  • 46
  • 61
Alana Storm
  • 164,128
  • 91
  • 395
  • 599
  • 2
    Is it safe? http://laravel.com/docs/4.2/upgrade For security reasons, URL domains may no longer be used to detect your application environment. These values are easily spoofable and allow attackers to modify the environment for a request. You should convert your environment detection to use machine host names (hostname command on Mac, Linux, and Windows). – Dariux Dec 23 '14 at 13:38
  • I guess for safety you can check the host name first: `if(gethostname() == 'server-name')` and then you can check the url as Alan showed above. – HPage Aug 13 '15 at 20:20
0

My option, in file app/config/database.php add this line at the end

            'enviorement' => 'local',
            // 'enviorement' => 'development', // this is for dev
            // 'enviorement' => 'production', // this is for production

and then access from your controller with this

            $env = Config::get('database.enviorement');
            echo $env;

database.php file is different, you have for ur local another for developemet server and another for producction because there is the "database conection" so i used it to implicit write the enviorement.

Have fun.

Rolly
  • 3,205
  • 3
  • 26
  • 35