12

I’ve just started learning Laravel 5 and trying to create multilanguage web site and want to use different domains for the language so en.example.app points to English version, es.example.app to Spanish and so on. I use route groups and below is my code.

Route::group(['domain' => '{domain}.example.app'], function() {
    Route::get('/', function () {
        return view('index');
    });
    Route::get('test', function(){
        return view('index');
    });
});

It works fine for all domains except example.app. Unfortunately optional parameters {domain?} doesn’t work for subdomains, and I don’t want to duplicate routes like this.

Route::get('/', function () {
    return view('index');
});
Route::get('test', function(){
    return view('index');
});

Route::group(['domain' => '{domain}.example.app'], function() {
    Route::get('/', function () {
        return view('index');
    });
    Route::get('test', function(){
        return view('index');
    });
});

Could somebody please advise how to avoid this duplication?

Teymur
  • 121
  • 1
  • 5

5 Answers5

5

Thats becuase the {domain}.example.app requires a . before example.app.

You can remove the . and add contraint for domain parameter for it to have atmost 1 .

So the code will look like

Route::group(['domain' => '{domain}example.app'], function($group) {
    Route::get('/', function ($domain) {
        //code
    }) ;
    // more routes

    foreach($group->getRoutes() as $route){
        $route->where('domain', '[a-z]+\.{0,1}');
    }

});

P.S. : I don't know whether my regex is correct or not.

Kapil Garg
  • 462
  • 1
  • 5
  • 17
4

You could create a file named app-routes.php which contains all your routes and then in your actual routes.php file

Route::group(['domain' => '{domain}.example.app'], function() {
    include('app-routes.php');
}); 

Route::group(['domain' => 'example.app'], function() {
    include('app-routes.php');
}); 
J. Davis
  • 49
  • 2
  • Great 1, but have you tried it? How to set the domain to some constant? – Sougata Bose Jul 19 '16 at 11:02
  • I tried this out but i wasnt able to link properly from example.app to subdomain.example.app... Any thoughts on this? @j-davis. I tried this {{URL::route('home', array('subdomain' => 'johndoe'))}} – AceKYD Aug 09 '16 at 22:09
3

A MiddleWare helped me.

Route::group(array('middleware' => 'resolve_domain'), function () {
    Route::get('/', 'WhitePapersController@getHomepage');
});

And in MiddleWare -

public function handle($request, Closure $next)
{
    $params = explode('.', $request->getHost());
    $sub_domains = config('admin_configs.editions'); // Predefined sub-domain
    $edition = false;
    if(!empty($params[0]) && in_array($params[0], $sub_domains, true))  {
        $edition = $params[0];
    }
    define('DOMAIN_EDITION', $edition); // Set constant to be used.

    return $next($request);
}
Sougata Bose
  • 31,517
  • 8
  • 49
  • 87
2

Your options are either the route duplication or a server level redirect for HTTP requests without a subdomain.

The simple option is to just forward example.app to www.example.app.

Qevo
  • 2,343
  • 1
  • 11
  • 17
1
Route::group(['domain' => '{domain}.example.app'], function() {

}); 

Route::group(['domain' => 'example.app'], function() {

}); 

this pattern is good but if you want to use different language add localization file

tapos ghosh
  • 2,114
  • 23
  • 37