In this episode, we're diving into how to set up subdomain routing in a Laravel application. We start by modifying our web.php
routes file to define a domain route that accepts a subdomain—basically, letting us do things like alex.laravel-user-subdomains.test
. By setting up a route group, we can define routes that are specific to each user's subdomain.
We then play around with extracting the subdomain value from the route and checking it with a quick dd
(die and dump), just to confirm it's working. Instead of hardcoding our domain right in the route definition, we improve things by grabbing the domain from our app config via the .env
file. This makes the setup work seamlessly across different environments, whether it's .test
locally or .com
in production.
To tie it all together, we show how you can query your users table to find the user based on the subdomain passed in, and mention the rough edges: you'd have to repeat this user-lookup code for each route, which isn't ideal. So, as a tease for the next episode, we point towards route model binding, which will let us pull in the user automatically and clean up our controllers and routes a lot.