Search code examples
phprouteslaravel-5middleware

Laravel 5.2: Filter route group based on the domain name


I have an application, which uses multiple domains: 1. the main domain 2. domains for different sections of the site (each user can have a domain for his own page, saved in the user_profile).

I need a route group, which I want to be available only for the main domain, and an other group which will be available for the other domains. The first group will not be available for the other domains and the second group will not be available for the main domain. How can I implement this?

A middleware is the solution? How can I access the current domain in a middleware?

Thanks for the help!


Solution

  • This will return the current host, which can be used in your middleware:

    $request->server->get('HTTP_HOST')

    You could actually just use the a route group like (in your routes.php):

    Route::group(['domain' => '{sitedomain}'], function()
    {
        // routes here
    
    })->where('sitedomain', '.+\.your_domain_regex\.com$');