blog-contest-may-mayhem icon indicating copy to clipboard operation
blog-contest-may-mayhem copied to clipboard

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

Open maxkostinevich opened this issue 6 years ago • 4 comments

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

maxkostinevich avatar May 25 '18 12:05 maxkostinevich

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

monaam avatar May 25 '18 17:05 monaam

@monaam Can you provide an example?

maxkostinevich avatar May 27 '18 09:05 maxkostinevich

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 avatar May 27 '18 13:05 monaam

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

monaam avatar May 27 '18 13:05 monaam