This episode is for members only

Sign up to access "Custom User Subdomains in Laravel" right now.

Get started
Already a member? Sign in to continue
Playing
04. Registering the subdomain route

Transcript

00:00
Let's explore subdomain routing in Laravel and see how this is going to help us achieve what we need. The first thing that we'll do is head over to our web routes and we're going to define out a new route. Now what this is going to be is a domain route which will allow us to take in something and then dot our domain. So in our case our domain is laravel user subdomains dot test.
00:23
So I'm going to go ahead and paste this in, change this around so we have the following and then from this what we can actually do is create a route group where we can define any routes out in here that we need. So for example let's say that we created out a get route in here to go through to slash and then we'll just leave it at that for now. Let's go ahead and define
00:44
out a closure in here. Now into this we're now going to get that account variable in here that we can do something with. Let's just die dump on that and see what we get. So now all we need to do is visit alex or any value dot and then the app url and we should see this dumped out. So let's go over and say alex dot and we'll go over to the home page and there we go. So pretty much anything
01:10
we now pass into here we can actually extract that value out and use it in some way. Now we've got a little bit of work to do here because we don't really want to hard code in this domain. That doesn't really make sense. What we can actually do is pull this from our app config. So we have a url in our app config which is defined by the app url over in emv. That should be based on what
01:35
environment you're working in. So obviously locally it will be a .test domain or something similar and then in production it will probably be something like .com. So really we want to pull from this value and replace that out in here. So we'll do that first just so we know that whatever environment we're in that is now going to work nicely. Let's go back over and give that a refresh
01:56
and sure enough we get exactly the same thing. So let's give this a different name. So I'm going to call this subdomain because that's what we're using within our app and let's call this subdomain as well. So really our goal is to define out any routes in here which belong to that user subdomain area and then resolve the user by their subdomain and then show information about them as we saw
02:19
from the introduction. Now the way that we've set things up just here means that we would have to every single time we wanted to do this perform a query like where subdomain and then actually pass a subdomain in. We could do that and grab the first user here and just die dump on that user and that would give my user. So let's just try that. We could even do first or fail which would
02:44
be a bit more convenient because then we would get a 404 if that didn't exist like this. But if the user did exist you can see that that has dumped out my user. Now this is a little bit annoying because if we were to define this out for lots of different routes, if we had more than one route in here, we would have to pluck the user out every single time either within a route here or within
03:05
a controller. So we're going to head up to the next episode where we can see how we can use route model binding to automatically pull in the user based on their subdomain. We'll create out a controller and then it will be really nice and clean and a lot more convenient to define out new routes in here as we need them.
9 episodes 31 mins

Overview

Allow users to choose their own subdomain, and give them their own area of your application where only their details are shown. You'll be able to register multiple routes for user areas.

Everything in this short course can be easily applied to your own applications where you need this functionality. We'll cover every step of the way, and some things to watch out for.

Finally, we'll learn how to deploy this and configure nginx and DNS records so you're ready to go.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!