gnabin01's avatar

Managing Subdomain

I am working on a ecommerce site from laravel.I want subdomain according to user's.How can it be done ??Any configuration to be made?? Thanks in advance !!!

0 likes
8 replies
d3xt3r's avatar

You can place your routes inside a group with dynamic domain like

Route::group(['domain' => '{whatever}.domain.com'] , function() {
    // define your routes here
});

However, first parameter passed to your controller methods will always be the value of whatever.

1 like
bobbybouwmann's avatar

You can however remove the variable with a middleware, but that also means that you don't have that variable anymore for redirecting to the correct subdomain

public function handle($request, Closure $next)
{
    $route->forgetParameter('identifier');

    return $next($request);
}
pmall's avatar

I don't remember the syntax exactly but parameters can be forgot.

d3xt3r's avatar

@bobbybouwmann Yes, you can forget the parameters, i do the same for versioning my api. However, It makes things only complicated. As you have already mentioned route() won't work correctly, you will require a sort of manager to remember the parameter and be the proxy for all those functions which are dependent on the parameter in question.

So, for most , the simplest approach would be to declare it as the first parameter of the controller methods.

bobbybouwmann's avatar

I wrote a simple helper function called subRoute which grabs the subdomain from the session. I set the session in a middleware where I remove the variable from the parameters in the request (see code above).

This however makes things also complicated for redirecting and for example the Form helper. I need to create helper functions for all of these functions.

d3xt3r's avatar

That's what i meant by sort of manager and all those functions which are dependent on the parameter in question

Please or to participate in this conversation.