title: Laravel authenticated dynamic subdomain routing
body: <p>I'm trying to get authenticated subdomain routing working for some specific variable subdomains:</p>
  
  <pre><code>app.example.com
  staging.app.example.com
  testing.app.example.com
  </code></pre>
  
  <p>These should be guarded by the auth middleware. They all essentially reference <code>app.example.com</code> but for different environments.</p>
  
  <p>Everything that hits these domains should go to the guest routes:</p>
  
  <pre><code>example.com
  staging.example.com
  testing.example.com
  </code></pre>
  
  <p>This is what I've tried so far...</p>
  
  <p>Created this middleware to prevent the subdomain parameter from messing up other routes and to allow successful authentication to redirect to <code>app.example.com</code>:</p>
  
  <pre><code>class Subdomain
  {
      /**
       * Handle an incoming request.
       *
       * @param  \Illuminate\Http\Request $request
       * @param  \Closure $next
       * @return mixed
       */
      public function handle($request, Closure $next)
      {
          $route = $request-&gt;route();
          $subdomain = $route-&gt;parameter('subdomain');
  
          if (!empty($subdomain) &amp;&amp; preg_match('/^(staging|testing)\.(app.\)?/', $subdomain, $m)) {
              \Session::put('subdomain', $m[1]);
          }
  
          $route-&gt;forgetParameter('subdomain');
  
          return $next($request);
      }
  }
  </code></pre>
  
  <p>Added this to Kernel.php:</p>
  
  <pre><code>protected $routeMiddleware = [
      'subdomain' =&gt; \App\Http\Middleware\Subdomain::class,
  ];
  </code></pre>
  
  <p>Contents of routes.php:</p>
  
  <pre><code>Route::group(['domain' =&gt; '{subdomain?}example.com', 'middleware' =&gt; 'subdomain'], function () {
      // Backend routes
      Route::group(['middleware' =&gt; 'auth'], function () {
          Route::get('/', ['as' =&gt; 'dashboard', 'uses' =&gt; 'Controller@dashboard']);
  
          // ...various other backend routes...
      });
  
      // Frontend routes
      Route::auth();
      Route::get('/', function () {
          return view('frontend');
      });
  });
  </code></pre>
  
  <p>When I access any route, I can trace that nothing hits the <code>subdomain</code> middleware...it just routes to the 404 page.</p>
  
  <p>How would I make this work in Laravel 5.2?</p>