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
05. Route model binding to resolve the user

Transcript

00:00
Clearly the way that we're doing this at the moment is not going to work very well because we're going to have to pick the user out of the database every single time we register a
00:08
route here. Now let's go ahead and just get rid of what we've done here and the first thing I'm going to do is create out a controller in here called home controller which represents a controller under the subdomain category of controllers. This isn't part of our actual app, it's part of the user within our app. So let's comment this out, go ahead and create out a new controller for this.
00:31
So let's make a controller. I'm going to put this in a folder called subdomain just so I know which ones are part of the subdomain section of our app to separate them out. So let's pull this back in and pull the namespace in for this and we can head over to the home controller now and just create this out. So we'll probably get a request in here if we need it and now we want to figure out how
00:56
to conveniently grab the user within here. So this should work if we just come back over and give that a refresh. Any domain that we access now is going to hit that controller. Now what we could do is we could use root model binding within the root domain definition. What that will do is it will grab a user by their id and then we can just have them injected within this controller. So let's go
01:20
ahead and just die dump on that user and see what we get when we pass a id of a user in. So if we head over to the database this is id 1. So really what we could do now is say 1.laravel user subdomains.test and sure enough we get my user. If we had another user with an id of 2 that's going to work in exactly the same way. Now that's not what we want. We want to pluck them out by their
01:45
subdomain. So with root model binding what we can actually do is choose the column that we want to identify this model by. So if I just use colon and then subdomain what this is now going to allow me to do is pass in the subdomain of the user and then that's going to be looked up by their subdomain. We have root model binding now that's automatically looked that user up for us and it's available in
02:10
our controller method here. So we can just start to output stuff about this user and this user only. So now all we need to do is start to define out any other routes we want about that particular user and that's just going to work and in every controller we define here we can just do exactly what we've done here and define the user in here that gets plucked automatically from the database.
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!