2014-01-25 113 views
1

如果我正在瀏覽特定路徑(domain.com/path1),我想擁有一個不同的環境,這在Laravel 4中是可行的嗎?如果是這樣,怎麼辦?我知道$app->detectEnvironment()方法,但我不知道如何使用它。如何根據路線設置環境?

回答

1

這可以用$app->detectEnvironment()方法(在/bootstrap/start.php),但不是在數組上發送,而是使用閉包。

$env = $app->detectEnvironment(function(){ 
    // get current http_host 
    $baseurl = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : null; 

    // our available environment 
    $envs = [ 
     'foo' => ['foo.com', 'bar.foo.com'], 
     'kex' => ['kex.foo.com'] 
    ]; 

    // default environment, you should not change this 
    $environment = 'production'; 

    // search trough each available environment to see if it matched our http_host 
    foreach($envs as $key => $env) { 
     foreach ($env as $url) { 
      if ($url == $baseurl) { 
       $environment = $key; 

       // match found, lets break our loop 
       break 2; 
      } 
     } 
    } 

    // we create segments of /our/path so we can check if it matches your condition 
    $segments = explode('/', isset($_SERVER['REQUEST_URI']) ? trim($_SERVER['REQUEST_URI']) : null); 

    // check if the first (second) segment matches our /path 
    if (isset($segments[1]) && $segments[1] == 'path') 
     return $environment . '-route'; // append -route to our environment and return it 

    return $environment; 
}); 

2到24行模仿Laravel的默認方法(使用數組)。下面的內容。