kingardox
kingardox

Reputation: 127

Laravel 5.1 group routes for specific domains and all subdomains

So I have a project with multiple domains pointing to it. Now I want to group routes by domains and subdomains.

For example I want to have separate routes for example.com and every subdomain of example.com

I do this right now like this:

$RoutesOnlyForExamp = function() {
  Route::get('/', ['as' => 'home', 'uses' => 'TestController@home']);
};

Route::group(['domain' => 'sub1.example.com'], $RoutesOnlyForExamp);
Route::group(['domain' => 'sub2.example.com'], $RoutesOnlyForExamp);
Route::group(['domain' => 'example.com'], $RoutesOnlyForExamp);

The home() method would give me this:

return view('home')

Now I have a weired problem. I have some links in my view which all lead into a

return view('RANDOM_VIEW');

like the home() method.

The links in the view are generated like this:

<a href="{{route('ROUTENAME')}}">LINKNAME</a>

Now let's say I access the site via sub1.example.com. Now some of my links look right, like this:

<a href="sub1.example.com/slug">Random Link 1</a>

But some look like this, which is wrong because I am on sub1.example.com and not example.com:

<a href="example.com/slug">Random Link 1</a>

And I don't see any pattern when and why it takes the wrong domain. My goal is that if I access the site via sub1.example.com all links should take this and not just example.com.

I hope the problem is clear. Otherwise I can try it with more detail.

I appreciate any help and thank you in advance!

Upvotes: 0

Views: 1075

Answers (1)

thefallen
thefallen

Reputation: 9749

You can accept the subdomain name like

Route::group(['domain' => '{subdomain}.example.com'], $RoutesOnlyForExamp);

and have it available as a variable and route parameter.

When you want to generate a link to a subdomain route you can do route('ROUTENAME', [$subdomain]).

Your main website routes are going to work as expected if declared like this:

Route::group(['domain' => 'example.com'], $RoutesOnlyForExamp);

Upvotes: 2

Related Questions