laravel / blog-contest-may-mayhem

240 stars 16 forks source link

How to build Multi-Domain Laravel Application (with demo) #37

Open maxkostinevich opened 6 years ago

maxkostinevich commented 6 years ago

https://maxkostinevich.com/blog/multi-domain-laravel

monaam commented 6 years ago

If I may suggest something, use the route defaults function to force the subdomain in a middleware

maxkostinevich commented 6 years ago

@monaam Can you provide an example?

monaam commented 6 years ago

For example, in my multi tenant apps I often use this code

app('url')->defaults(['host'  => $request->route('host')]);
$request->route()->forgetParameter('host');

And then whenever I use the route() helper the host get inserted by default without the need of another helper function (for the sake of simplicity)

monaam commented 6 years ago

The second line actually removes the $host parameter from controllers, so I dont have to pass it through all controller methods for example

public function saveCategory(Request $request, Category $category){ ... }

Instead of

public function saveCategory(Request $request, Host $host, Category $category){ ... }

But to make it work you need a singleton instance of your host model binded to the container, and you load it when initializing the tenant in your middleware