mardi 25 octobre 2016

Dynamic custom domain routing in Laravel 5.3

I develop a website generator. Right now, user can have their website at user.myapp.com and it works (with subdomain and model binding).

I now want to go one step further, and allow users to add their own custom domain names. So their website will be reachable at user-domain.com rather than (or as well as) user.myapp.com.

I found several tricks but I can't get them to work.

Here's a good example of the implemenatation I'm looking for : http://ift.tt/2ekgb8M

Actual route/web.php

Route::group(['domain' => '{subdomain}.'.env('APP_URL_SHORT')], function () {

    Route::bind('subdomain', function ($value) {
        return App\Site::where('sub_domain', $value)->first();
    });

    [...]
});

That way, all my routes have model binding with the App\Site.

Expected route/web.php

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

    Route::bind('domain', function ($value) {
        if (/* TEST if sudomain */)
            return App\Site::where('sub_domain', $value)->first();
        else // user domain
            return App\Site::where('self_domain', $value)->first();
    });

    [...]
});



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire