Laravel 5, subdomain routing, with an additional parameter

I was just starting to learn Laravel 5 and was trying to create a multilingual website and want to use different domains for the language, so en.example.app points to the English version, es.example.app to Spanish, etc. 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 great for all domains except example.app. Unfortunately, the optional parameters {domain?} Do not work for subdomains, and I do not 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'); }); }); 

Can someone please advise how to avoid this duplication?

+8
php laravel laravel-5 laravel-routing
source share
6 answers

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); } 
+3
source share

To use the domain parameter in your route function, you need to pass it like this:

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

Described here in the docs - http://laravel.com/docs/5.1/routing#route-group-sub-domain-routing

+2
source share

That for {domain}.example.app is required . to example.app .

You can delete . and add a contraint for the domain parameter so that it has a maximum of 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', '[az]+\.{0,1}'); } }); 

PS: I don’t know if my regular expression is correct or not.

+2
source share

You can create a file called app-routes.php that 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'); }); 
+2
source share

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

A simple option is to simply forward example.app to www.example.app .

+1
source share
 Route::group(['domain' => '{domain}.example.app'], function() { }); Route::group(['domain' => 'example.app'], function() { }); 

this template is good, but if you want to use a different language, add a localization file

+1
source share

All Articles