路线中的Laravel环境

时间:2016-01-08 02:45:58

标签: php laravel laravel-5

我希望根据环境为我的应用程序提供不同的域名;

像这样; www.prod.co.ukwww.dev.co.uk 现在我的route.php已经定义了子域名,如此;

Route::group(['domain' => 'www.prod.co.uk']
Route::group(['domain' => 'blog.prod.co.uk']
Route::group(['domain' => 'careers.prod.co.uk']

laravel文档中,您可以根据env中的内容访问.env我可以在route.php中执行以下操作吗?

Use App;

if (App::environment('production')) {
    // The environment is local
    $domain = www.prod.co.uk;
    $subdomain1 = blog.prod.co.uk;
    $subdomain2 = careers.prod.co.uk;
}

if (App::environment('local', 'staging')) {
    // The environment is either local OR staging...
    $domain = www.dev.co.uk;
    $subdomain1 = blog.dev.co.uk;
    $subdomain2 = careers.dev.co.uk;
}


Route::group(['domain' => $domain]
Route::group(['domain' => $subdomain1]
Route::group(['domain' => $subdomain2]

1 个答案:

答案 0 :(得分:6)

更好的(IMHO)方法是在.env文件中定义您的自定义域 - 这样很明显,域名是特定于环境的,并且不需要任何'ifs':< / p>

.env:

URL=www.dev.co.uk 
SUBDOMAIN1=blog.dev.co.uk
SUBDOMAIN2=careers.dev.co.uk

然后添加到config / app.php:

'url' => env('URL'),
'subdomain1' => env('SUBDOMAIN1'),
'subdomain2' => env('SUBDOMAIN2'),

routes.php将变得更简单,更好阅读:

Route::group(['domain' => Config::get('app.url')] {}
Route::group(['domain' => Config::get('app.subdomain1')] {}
Route::group(['domain' => Config::get('app.subdomain2')] {}

PS。想象一下,如果你将来获得更多特定于环境的URL - 你的routes.php会变得臃肿,它会(它实际上已经包含)环境特定的数据,这是不好的!