M
M
mynameisflipe2017-06-01 13:41:19
Laravel
mynameisflipe, 2017-06-01 13:41:19

How to route multiple domains in one Laravel?

Laravel has routing, and it is possible to group routes depending on the domain.
There were no problems with grouping routes by subdomains, but it didn’t work out to do something like this.

Route::group(['domain' => '{domain}'], function () {});

(what is needed is dynamic routing with the transfer of the domain name to the controller)
Is this even possible?)
And if so, how? If anyone has done something like this please let me know.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alex Wells, 2017-06-01
@mynameisflipe

Maybe if you really need to. Not recommended, of course, but doable.
Here are the functions. Personally, I get the domain in the RouteServiceProvider, where it is scattered over the files as needed.
But you can implement some kind of middleware that will shove the domain somewhere (so as not to pull the function every time), or get the file in the same route Provider for the domain.

if (! function_exists('subdomain')) {
    /**
     * Returns subdomain.
     *
     * @return string
     */
    function subdomain()
    {
        $exploded = explode('.', Request::getHost());

        return count($exploded) == 3 ? $exploded[0] : null;
    }
}

if (! function_exists('domain')) {
    /**
     * Returns domain.
     *
     * @return string
     */
    function domain()
    {
        $exploded = explode('.', Request::getHost());

        return join('.', array_slice($exploded, -2, 2, true));
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question