This episode is for members only

Sign up to access "Build a File Marketplace with Laravel" right now.

Get started
Already a member? Sign in to continue
Playing
21. Subdomain routing basics

Episodes

0%
Your progress
  • Total: 3h 32m
  • Played: 0m
  • Remaining: 3h 32m
Join or sign in to track your progress

Transcript

00:00
We have a completely separate course on Laravel subdomain routing. If you want to dive a little bit deeper into this, what we're going to do now is set this up so we can access a specific set of routes for the subdomain that we're working with. So the first thing I'm going to do is open up alex.laravelfarmarketplace.test in my browser.
00:20
Now, at the moment, this doesn't do much because we're just within the homepage of this site. It's working in exactly the same way. I'm actually going to change this over to Alex as well. Just so we can keep some consistency.
00:32
So this at the moment is just showing us the homepage, which of course is not what we want. What we want to do is create a completely separate group of routes under the subdomain that we want. So we're going to go ahead and say route domain. And in here, what we can do is define out how we pluck out this particular domain.
00:52
Let's just say Alex for now. And then let's say dot and then the name of our app. So we know that this is laravelfarmarketplace.test. I'm going to go ahead and add that in there just so we can kind of test this out.
01:06
Now, for this route group, we want to give this a name. So we want this to be subdomain dot. That means that if we ever want to reference a subdomain within our route helper, for example, we're going to say subdomain.home. Now, subdomain.home will reference the user's marketplace home rather than our homepage.
01:28
If we did have a name for that, which we can actually add in there in case we need to reference it. So that's the difference between adding a name. This is just a prefix for all of these routes. Now for this, we want to go ahead and create our group and a closure inside of here.
01:43
And now any routes that we define inside of here are now going to be under this Alex.laravelfarmarketplace.test domain. Of course, we want to switch this out dynamically, and I'll show you how to do that in a minute. But let's just define the route out in here first of all. So what I'm going to do is create out a set of controllers that are completely separate from our main app controllers.
02:03
So I'm going to create these under a subdomain directory. So I'm going to create out a subdomain home controller. And of course, you could just call that home controller, but I'm going to be really explicit with the names here. OK, so we've now got a subdomain home controller.
02:20
Let's go ahead and create out a route here to grab the home page. So just slash for subdomain home controller. And let's call that home. So let's name that home.
02:33
So the name for this route is now subdomain.home. And that only exists under this domain just here. OK, so let's go over to here. And sure enough, this is already changed because we've got an invalid route action for this particular controller.
02:50
Our main app should still work. Well, it doesn't at the moment, but let's go ahead and just update this to have an invoke magic method and do nothing. And our main app still works. Our main app home page should still work.
03:04
But this is now completely empty. So now we've successfully set up a subdomain for Alex. Now, like I said before, that's not what we want, because we want this to be completely dynamic to the actual domain that we pass in, the subdomain that we pass in. So we need something here to work that out.
03:23
Now, what we're going to do is get rid of this because we can actually pass in the config app URL to this. So let's go ahead and just append onto this config app dot URL. So this value here, where is it going to come from? Well, we can actually use route model binding here, as in pulling the user by their ID, or in our case, it's going to be by their subdomain.
03:49
So just to give you an idea of how this is currently working, if we give this a refresh, sure enough, it doesn't work. We get the home page. But if I were to choose the ID of Alex, so let's just check that out in the database here, it's three. That will give me that user, or at least it should, because we're not putting a dot after this.
04:05
So let's go over and give that a refresh. And there we go. So now technically we're in the context of user three. Now, we don't want that.
04:12
We want the actual subdomain passed into here. So with route model binding, we can use a colon to tell us which column we want this to work for. So in our case, we want this to be Alex. So let's go ahead and switch this over to Alex.
04:25
And now we should be in the context of the user Alex. Now, how do we know this? Well, let's head over to our subdomain home controller and under the invoke magic method, what we can actually do is pull in the request if we need it. So it's going to work in exactly the same way.
04:40
But because we've used route model binding, we should have the user in here and we should be able to dump that out and see my user. So let's just die dump here on the user and let's head over and give that a refresh. And sure enough, we are now in the context of Alex. If we had another user with a different subdomain like Mabel, that would pull in their account.
05:02
OK, so we know this is working and we want to actually pull in Alex. Now we can start to render out views and show Alex's details. So for the home controller here, let's go ahead and return a view and let's put that in a subdomain directory and just call that home. OK, so let's go over to our view section and we're going to create out a subdomain folder and a home.blade.php file here.
05:29
OK, this is going to be very similar to what we've done before, like over on the home page. So let's just grab that and paste this in and let's output the user's name here. So user and name. And of course, we need to update our subdomain home controller to actually pass that user down.
05:45
So that's a user user. And what we should now see is my name within my own section of this app. Let's make this look a little bit nicer before we move on. So if we head over to the subdomain home view, we're going to go ahead and create out a header in here.
06:02
So let's do an H1 and we'll set the text in here to large. We're going to go ahead and grab in the user's name and we'll say marketplace. Now, we're going to update this later because in terms of this possessive here, it's not quite going to work if we have a name that ends in S. Doesn't really matter, but it would be a nice touch to modify this so it is outputting properly.
06:29
So we've got Alex Garrett Smith's marketplace in here, and that's pretty much it. To be honest, we've got pretty much everything we need in here. We can start to iterate over and show the user's products down here as well. And then we can redirect the user over to within this domain, the product that we want to view.
34 episodes3 hrs 32 mins

Overview

Build a marketplace where sellers can list and sell files, while we take a cut of each sale using Stripe Connect.

We'll cover onboarding users with Stripe Connect, creating products and uploading files, payments, and delivering purchased files to your customers.

Here's everything we'll cover:

  • The Stripe Connect onboarding flow
  • Effortlessly creating products (and uploading files) with Livewire forms
  • Subdomains for your user's marketplace
  • Stripe Checkout for a beautiful, secure payment flow
  • Securely delivering files with Signed URLs in Laravel
  • Showing sales stats on a dashboard
Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Comments

No comments, yet. Be the first to leave a comment.