Ad

Laravel Environments In Route

- 1 answer

I want to have different domains for my application based on the environment;

Like so; www.prod.co.uk and www.dev.co.uk Now in my route.php I have defined subdomains like so;

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

In the laravel documentation you can access the env based on what your have in your .env can I do something like below in my 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]
Ad

Answer

A nicer (IMHO) way to do this would be to define your custom domains in .env files – this way it's clear that domain names are environment-specific and there won't be a need for any 'ifs':

.env:

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

Then add to config/app.php:

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

routes.php would become simpler and nicer to read:

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

PS. Imagine if you get more environment-specific URLs in the future – your routes.php will get bloated and it will (it already does, actually) contain environment-specific data which is not nice!

Ad
source: stackoverflow.com
Ad