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
06. Building pages for subdomain routes

Transcript

00:00
Building out pages within your subdomain routes is pretty much exactly the same as you would expect from any other route within your Laravel app
00:07
We're gonna go ahead and create a view out here And again, I'm gonna categorize these under a subdomain folder and we're just gonna call this the home page We're gonna go ahead and pass that user down So we can access the information about that user and let's build this out and create an example view
00:24
So under views, let's create that new subdomain category Let's create a user or home in this case dot blade dot PHP file And let's just keep this really simple and use the guest layout that comes with breeze So in here, we're just going to create our paragraph
00:39
let's say welcome to the subdomain for and then we can just access that user and Grab their name so it works in exactly the same way. There we go Welcome to the subdomain for Alex Garrett Smith, of course Any other one is not going to work at this point, but that's fine when we create new users and they add subdomains
00:59
They'll have their own page. Let's create out another page here Maybe an about page so we can see stuff about that user. So for this we're gonna go ahead and create a new route So it's just as easy as creating a new route within here we're gonna maybe call this about controller and let's go ahead and create that controller out and
01:18
Of course we're gonna do that within that subdomain Directory just to keep things nice and tidy and organized and we can now pull that in So we can just follow the same pattern here under subdomain. Let's create about dot blade dot PHP let's copy what we did over here and
01:37
let's just Put something in here. So I'm Alex now, obviously this content would come from the users table You might have a bio column where they can write something about themselves I'm just gonna hard code it in here for now just to keep things really simple
01:52
Okay, so on here we want to link through to that page. So if we just head over to the roots quickly just to fix this up and pull in the about controller properly and Yeah, of course, we didn't actually create a
02:10
Invoke or any other method for this. So let's return an actual view here. So that's now going to be sub Domain and about and once again, we could pass the user down to here To grab that information. We know that that comes from root model binding So let's bring our request in and the user in as well. Okay, so that should now work and
02:31
About would then show the information about that user from the model So from the home we want to link through to that so a good strategy here would be to name these properly So typically what we can do when we define domains is actually give these a name prefix So what I would do is name these all under subdomains
02:54
so you sort of know where to find them then what we can do with these is give these individual names like home and About Then the name for this becomes subdomain
03:08
About or home in this case and this one becomes subdomain about so now we know that we can link through to these properly Let's give it a go and see what happens. So here. I'm just gonna add a anchor on the end of here Just keep this really rough and just say about and let's go ahead and hook this up to the root called subdomain
03:32
About and let's just see what happens with this. So we've got subdomain dot about not found. That's absolutely fine we just need to go over and add a dot on the end of here and What we've got here is a missing required parameter So because with this it requires a user to be passed in
03:50
Every time we go ahead and use this what we're actually going to need to do is pass the user Into here so that can be constructed as part of the root with root model binding and you can see now That that works when I click on this you can see that at the bottom of my screen on the left We've got Alex dot Laravel user subdomains dot test forward slash about so it will hook it up to the correct
04:12
Subdomain and then obviously the root that you have given so that's pretty much how you would go about constructing pages Of course, there are lots of different variations here depending on what you're building But just always remember that whenever you are within these subdomain sections, you're in the context of a user You always want to make sure it goes through to the right place
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!