Laravel Subdomains - How To Create and Manage Subdomains in Your Apps
Laravel Subdomains - How To Create and Manage Subdomains in Your Apps
But the more functionality the app has, the clumsier your route
paths will get.
What if there was a way to separate all these parts into smaller
components with better and cleaner routes? Something that users
could easily access and use independently, under the same
Forumwebsite?
Donate
What is a Subdomain?
So let's say you have a website called mysite.com. You have a blog
section, a store section, and a general website section for about and
contact pages. The website could have subdomains like
blog.mysite.com and store.mysite.com, where the main website
would use the main domain.
Why Should You Use Forum Donate
Subdomains?
Support our charity and our mission. Donate to freeCodeCamp.org.
Subdomains are pretty useful, and here are some of their main
advantages:
Let's see how all this works by building an actual project and testing
it out.
./vendor/bin/sail up -d
Route::get('/', function () {
return 'First sub domain';
})->domain('blog.' . env('APP_URL'));
But more often than not, you'll have more than one path in an
application, like a domain and subdomains. So, it's a good idea to use
a route group to cover all the routes in the same domain or
subdomain.
Route::domain('blog.' . env('APP_URL'))->group(function () {
Route::get('posts', function () {
return 'Second subdomain landing page';
});
Route::get('post/{id}', function ($id) {
Forum Donate
return 'Post ' . $id . ' in second subdomain';
Support our charity and our mission. Donate to freeCodeCamp.org.
});
});
Now, all the routes for the domain can be handled in one place.
Route::domain('{username}.' . env('APP_URL'))->group(function ()
Route::get('post/{id}', function ($username, $id) {
return 'User ' . $username . ' is trying to read post '
});
});
$this->routes(function () {
Route::prefix('api')
->middleware('api')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
Route::middleware('web')
->namespace($this->namespace)
->group(base_path('routes/web.php'));
});
}
Route::domain('blog.' . env('APP_URL'))
->middleware('web')
->namespace($this->namespace)
->group(base_path('routes/blog.php'));
This is telling Laravel that whenever someone hits theForum Donate
blog.domain.com endpoint, look for the route in the blog.php (that we
Support our charity and our mission. Donate to freeCodeCamp.org.
are yet to create).
<?php
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
return 'Route using separate file';
});
At this point, you're done with all the code! All that's left is some
server configuration.
Server Configuration
If you're using a service such as Laravel Valet, it is way easier to
setup.
Valet setup
And if you're not using Laravel Valet, you can add this to your
/etc/hosts/ file:
Forum Donate
/etc/hosts configuration
Summary
Now you know how to set up and manage subdomains in your
Laravel apps. You can find all the code for this article here.
If you read this far, thank the author to show them you care.
Say Thanks
Our mission: to help people learn to code for free. We accomplish this by creating thousands of
videos, articles, and interactive coding lessons - all freely available to the public.
Donations to freeCodeCamp go toward our education initiatives, and help pay for servers,
services, and staff.
Trending Guides
Mobile App
Our Charity
About Alumni Network Open Source Shop Support Sponsors Academic Honesty
Code of Conduct Privacy Policy Terms of Service Copyright
Forum Policy Donate