laravel / ideas

Issues board used for Laravel internals discussions.
938 stars 28 forks source link

Using subdomains/domains with route() and bind a default parameter #1967

Open kasperhartwich opened 4 years ago

kasperhartwich commented 4 years ago

I been working on a couple of applications, where we want to use subdomains for each project or team.

I can specify this in my routes file:

Route::domain('{team}.example.com')->group(function () {

And i can then modelbind my Team eloquent model in my controller code.

The irritating thing is that i now have to provide the given team every time i use the route function in my blades and that gives me some ugly code as i need to pass the domain/team thru the controller to my blades.

route('profile', ['team' => $team])

It would be nice if I could somehow in my middleware og AppServiceProvider easily just bind that model for the given route if it is now present. Somehow make a default value for the route, if it has not been specified.

Any suggestions on how to solve this, without doing a lot if hacking thru a middleware? I think this is something that Laravel should support.

rs-sliske commented 4 years ago

you can do

\Illuminate\Support\Facades\URL::defaults(['team' => $team]);

kasperhartwich commented 4 years ago

@rs-sliske I did not know that, but can't quite get it to work. Where do i set that up? It just gives me missing parameters for my route()links when i add it to middleware og even in the RouteServiceProvider.

kasperhartwich commented 4 years ago

I found the problem. Apparently i can't just make a

Route::domain('{domain}')->group(function () {

It only work with

Route::domain('{subdomain}.example.com')->group(function () {

I don't quite get why..

rs-sliske commented 4 years ago

the project i was working on we had some custom middleware that handled checking which tenant the request was for so we added the URL::default bit to that

route domain requires the full url, in theory you could have completely separate domains pointing at a single laravel app rather than just using for subdomain multi tenant, but i will agree it would be nice to have an easier way of handling subdomains

it doesnt have to be hard coded, you can (and imo it would be better to) pull the rest of the domain from a config variable

julienbourdeau commented 4 years ago

@kasperhartwich By default, it only works with subdomains but you can make it work with full domain by providing a pattern. I think it's related to the dots in domains.

Route::domain('{domain}')->where('domain', '.*'); // Allow all characters
kasperhartwich commented 4 years ago

@julienbourdeau That works, thank you.

Though the argument should be an array:

Route::domain('{domain}')->where(['domain' => '.*']);
hubertnnn commented 4 years ago

I would suggest excluding the / character, so it would only match the domain and not any url parts. Otherwise if you have something like: www.example.com/foo/bar it could match route '/bar' setting $domain to www.example.com/foo.

julienbourdeau commented 4 years ago

As far as I remember, there is no risk because you're using the domain method, it's not matching against the entire URL.