In modern multi-tenancy systems, it’s pretty popular to give a specific sub-domain for every user or company, like laraveldaily.slack.com. How to handle these subdomains in Laravel routes?
The code in routes/web.php is pretty straightforward:
Route::domain('{company_name}.workspace.com')->group(function () {
Route::get('users', 'UsersController@index');
});
So {company_name} could be any value (of course, you need to configure it in your domain DNS records, too), and then this will come as a parameter variable to the Controller, with the same name.
public function index($company_name)
{
$company = Company::findOrFail($company_name);
$users = User::where('company_id', $company->id)->get();
return view('users.index', compact('users'));
}
Wait, But.. How to Setup Local Environment?
With this routes file, we have a problem. Then we need to configure our local server to have specific workspace.com domain? Don’t worry, there’s a cure for that, we can make even that domain a variable:
Route::domain('{company_name}.' . env('SITE_URL', 'workspace.com'))->group( // ...
So now the whole string would consists of two variables – subdomain and main domain, both configurable.
For your local environment, you can set up something like workspace.test domain, and then add this into your .env file:
SITE_URL=workspace.test
If Laravel doesn’t find any value for SITE_URL, it will default to workspace.com.
That’s it, good luck with your multi-tenancy projects!
Ho, great article. Make me think about, how about having a complet domain for each of the tenant ? Something like mysite1.com for user1, mysite2.com for user2. Is this feasible ?
Not really, you can’t register new domains that flexibly, that’s not how domain registration works. It’s not about Laravel or code, it’s domain system.
good article (:
I have applied all of the items and changed apache like
ServerName website.com
ServerAlias *.website.com
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html/host/blog1019
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
and also my hosts file
0.0.0.0 website.com
0.0.0.0 *.website.com
.i work laravel5.7 ubuntu on localhost .. what is miss ??
Great article, save my day, Thanks,,
Hi, nice article, thanks, I was wondering how do you make this work with the subdomain dns and nginx in forge? thanks again
Hi man, nice and short article.
But I wonder, how some other online sites do that. For example Slack.com has this feature. As soon as we register it asks to set a workspace url so if I set “web-developer” as a workspace name, the Slack creates my url like “web-developer.slack.com”
I can use this url as soon as I verifies my email address. So I don’t think there is any manual process to add a sub-domain entry in DNS records.
I want to achieve like that in laravel, can you guide me please?
It’s not in Laravel, you need to configure your DNS, domains, subdomains and web-server for this. So search for documentation for your web-server.
This can work without configuring DNS and subdomains manually. I can’t remember the exact steps, but I know I had a tenancy project a couple of years back, coded in procedural php, where we used .htaccess to accomplish the subdomain routing. I would think the same thing would be possible with Laravel, but haven’t found a single article/tutorial on how to accomplish it.
You can set wildcard domain in your server.
*.your_domain.com A record -> your server ip,
Once you set that, then you will be able to use any subdomains immediately.
in my app I am registering user on app.client.net and then use willdcard subdomain {subdoman}.client.net both route groups are in route.php and working good on localhost but wildcard subdomain not working on live server. I deployed my project on app.client.net subdomain on server.
Hi, thanks for useful article.
I have a subdomain route in my project and I get the domain parameter in the controller then I return a view.
Route::domain(‘{website}.localhost’)->group(function () {
Route::get(‘/’, ‘HomeController@index’)->name(‘home’);
Route::resource(‘/courses’, ‘CourseController’);
…. other routes
}
In the view I use route() function for calling another page:
مشاهده دوره
But I have error which says :Missing required parameters for [Route: courses.show] [URI: courses/{course}].
I think I have to pass the subdomain parameter too. but I want laravel to do that automatically.
Is there any solution for this situation or I need pass subdomain parameter to each route?
You need to pass to each parameter, there’s no shorter way.
You can set middleware.
In middleware, you can remove domain route parameter.
public function handle($request, Closure $next)
{
// Set defaults for use with route() helpers.
\URL::defaults([
‘subdomain’ => $request->route()->parameter(‘subdomain’),
]);
// Remove these params so they aren’t passed to controllers.
$request->route()->forgetParameter(‘subdomain’);
return $next($request);
}
Hi everybody,
I have a member store subdomain system: username1.mydomain.com, username2.mydomain.com…
How when I point the domain name corresponding to the subdomain above. For example, abc.com will run the data of username1.mydomain.com. def.com will run the data of username2.mydomain.com
Thank you!