1

I have been testing sub-domain routing functionality in Laravel 5 and have had success with the following code as described in the docs. When the user visits {username}.mysite.com, the user profile view is shown as expected.

Route::group(['domain' => '{username}.{tld}'], function () {
    Route::get('user/{id}', function ($username, $id) {
        //
    });
});

But, I was expecting a bit of different behavior from what I am experiencing. When the user visits the site through the sub-domain, all of the links in my views now retain the sub-domain within them. All of the other links like {username}.mysite.com/home and {username}.mysite.com/login etc... are fully functional, but I don't understand why Laravel is populating all of my links with the sub-domain and/or how I can get rid of this and only retain the sub-domain for just the single route. I want all of the other links in my views to be like mysite.com/home and mysite.com/login. I was hoping to just use {username}.mysite.com as a quick access point for site visitors, not to retain it in all views.

What can I do to change this behavior?

user3691644
  • 477
  • 3
  • 7
  • 18

3 Answers3

1

Move routes you don’t want prefixing with the subdomain outside of the route group:

// These routes won’t have the subdomain
$router->controllers([
    'auth' => 'Auth\AuthController',
    'password' => 'Auth\PasswordController',
]);

// These routes WILL have the subdomain
$router->group(['domain' => '{username}.{tld}'], function ($router) {
    $router->get('/', 'UserDashboard@index');

    $router->controller('account', 'AccountController');

    $router->resource('users', 'UserController');
});
Martin Bean
  • 38,379
  • 25
  • 128
  • 201
  • Is this code to be placed in the `map` function of `RouteServiceProvider.php`? – user3691644 Jun 24 '15 at 16:57
  • No, your routes file. You can use either the `Route` façade or the `$router` variable. As you can see I prefer the latter, but they both yield the same results. – Martin Bean Jun 24 '15 at 17:38
  • Is it possible to do the reverse? That is, define the subset of controllers which will use the prefix? Note, I'm not using a a RESTful convention. `$router->group(['domain' => '{username}.{tld}'], function ($router) { $router->controllers(['/' => 'SubdomainController']); });` – user3691644 Jun 24 '15 at 17:59
  • I’m not sure I understand from your example code, that wouldn’t work as you can’t map a controller to the root (`/`). You can define resource controllers inside a group as you would outside, though. – Martin Bean Jun 24 '15 at 18:08
  • Ok, disregard that previous comment. Adding controllers to the `$router->controllers` array still leaves all of my links in my views prefixed with the subdomain. Maybe I need to take a further step back. I really only want one sub-domain link to be accessible `{username}.mysite.com`. The rest of the links should be just to `mysite.com`. – user3691644 Jun 24 '15 at 19:06
  • How are you adding links in your views? – Martin Bean Jun 24 '15 at 19:47
  • Like this: `{!! HTML::link('contact', 'Contact Us') !!}` – user3691644 Jun 24 '15 at 19:58
0

You forgot to redirect user... So:

First, as Martin Bean suggested, exclude undesired controllers from sub-domained group.

Second, after user's successful login - redirect him to address without subdomain. You can do that by overriding auth middleware with yours implementation (which must implement TerminableMiddleware interface).

I. e.:

  1. User was on page https://auth.example.com and logined.
  2. Your's override of auth middleware checks for succesful login.
  3. ... and redirects user to https://example.com/home or whatever...

That should be enough.

ankhzet
  • 2,517
  • 1
  • 24
  • 31
0

I found the problem where all of my links/routes were being prefixed with the subdomain, even when they were outside of the route group. The issue is with the Illuminate HTML link builder. It renders relative links rather than full absolute links.

So instead of using: {!! HTML::link('contact', 'Contact Us') !!}

I should have used: {!! HTML::linkRoute('contact_route_name', 'Contact Us') !!}

The linkRoute() function takes into consideration the route group and applies the subdomain as required.

user3691644
  • 477
  • 3
  • 7
  • 18