In this episode, we're diving into the basics of subdomain routing in Laravel.
We start by setting up a route group that applies only to a specific subdomain—in the example, we use alex.laravelfarmarketplace.test
. At first, everything looks the same as your main domain, but then we separate things out by creating a dedicated group of routes for the subdomain. This includes creating a new controller (named explicitly for clarity) and setting up a route that points to the subdomain's own homepage.
Next, we make the routing dynamic. Instead of hardcoding the subdomain, we utilize Laravel's route model binding. This allows us to use the subdomain as an identifier to load the correct user's data. When you visit alex.laravelfarmarketplace.test
, for example, the routes and views are now scoped to Alex's account. If another user exists (e.g., Mabel), you could just swap the subdomain to load her marketplace page.
We also go through creating a dedicated view for the subdomain homepage and display the user's name, making things feel personalized. There are little touches here, like discussing possessive names, which we'll make even better later on.
By the end of the episode, you see how subdomain routing lets users access their own marketplace sections, setting up the foundation for user-specific content under custom subdomains.