9

In a laravel 5 application, I'm trying to make a route for subdomain without knowing the domain.

Route::group(array('domain' => 'subdomain.domain.tld'), function() {
    Route::get('/', 'testController@getTest2');
});
Route::get('/', 'testController@getTest1');

This kind of routing works, and I get getTest2() called for subdomain and getTest1() for calling without subdomain.

Now, I'd like this to work with wildcard domain, but without sending parameters to controller, so the application in dev enviroinment can be on any domain. (I also considered using .env for storing domain, but that seems too much hassle for just routing)

I've tried

array('domain' => 'subdomain.{domain}.{tld}')

Which requires parameters on controller methods. and I've tried

array('domain' => 'subdomain.{domain?}.{tld?}')

Which doesn't require parameters, but sends them anyway, so my actual route parameters get shifted.

I've also seen http://laravel-tricks.com/tricks/dynamic-subdomain-routing, but I don't like the idea of handling my domains in filters.

Is there any other way to have a wildcard domain that will be ignored once route group is handled?

YomY
  • 603
  • 6
  • 16
  • i'm pretty interested in this too. i my laravel backend delivers an api and i configured it so that the api is available under api.domain and some other things like oauth related things under different subdomains. as i am developing locally with homestead and the server obviously will be on another domain i hope there is a way of not hardcoding the domain in my application – Moritz Ewert Jul 16 '15 at 11:06
  • Until I find a better solution, I've added manual domain checking directly in routes file, by using something similar to what user mazon answered here: http://stackoverflow.com/a/12372310/2099306 – YomY Jul 16 '15 at 13:02

3 Answers3

6

The best way i've found to do this is to create a middleware that removes specific parameters from your route. For instance, in your case:

class Subdomain {
  public function handle($request, Closure $next) 
  {
    $route = $request->route();
    $domain = $route->parameter('domain');
    $tld = $route->parameter('tld');

    //do something with your params

    $route->forgetParameter('domain');
    $route->forgetParameter('tld');
    return $next($request);
  }
}

//inside your Kernel.php, make sure to register the middleware
protected $routeMiddleware = [
  'subdomain' => \App\Http\Middleware\Subdomain::class,
];

Route::group(['middleware' => 'subdomain', 'domain' => 'subdomain.{domain}.{tld}'], function () {
  Route::get('/', function () {
    dd(func_get_args()); //no args, because it was removed in the middleware
  });
});
jpcamara
  • 672
  • 6
  • 18
  • 1
    Interesting approach. I like it, and might use it to manipulate parameters in a route. However it does not solve the problem. This way, the route function for "/" would still be the same for all subdomains. The idea was to point to different functions based on which subdomain was called. x.sub.tld > function1, y.sub.tld > function2. Thank You for a new idea though. – YomY Feb 09 '16 at 06:45
1

Just so you know you can do

'domain' => '{subdomain}.{domain}.{tld}' 

and that will route a.domain.com and b.domain.com to all he same routes / same controllers and you can do your logic inside of that controller.

I would use a middleware like suggjested above to remove the domain and tld from the request or else every modeling is going to have to look like.

public function myMethod($subdomain, $domain, $tld, $id)

assuming your route needs an $id or any other route parameter.

One other thing you might be interested in is Explicit Form Model Binding.

hcker2000
  • 603
  • 1
  • 6
  • 30
-1

check this it might help answer your question :

http://laravel.com/docs/5.1/routing#parameters-regular-expression-constraints

http://laravel.com/docs/5.1/routing#route-group-sub-domain-routing

sabtikw
  • 67
  • 4
  • I've already read the documentation, The regex in parameters will affect what counts as a parameter, but will not change the way controllers get them. – YomY Jul 13 '15 at 08:50