Playing
01. Custom Laravel Jetstream Routes

Episodes

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

Transcript

00:00
In this snippet, I'm going to show you how to completely customise Laravel Jetstream routes. For example, their names, the controllers that they point to, any middleware
00:08
that you attach to them. And this method is really, really simple. But there's a small caveat that we need to make sure we deal with.
00:16
Now let's just pick a route that we want to change first of all. So for example, the profile page. You might want to change the name of the route. You might want to change any information about this,
00:25
the controller that it points to. It really doesn't matter what you want to change, the method of doing this is the same. Now if we head over to our routes file
00:33
over in Routes and Web, of course the only route really is the dashboard, which is what we see when we are authenticated. This is obviously where you just continue
00:41
to build the rest of your app out. But all of the other routes are actually hidden away within the composer files. So you can find this, whether you're using Inertia
00:49
or Livewire, by just looking for the Jetstream route and then the name of the scaffold that you're using. For example, Livewire in my case. So these are all the routes that Jetstream provides.
01:01
Most of the functionality within Jetstream is with Fortify. If you want to customize Fortify routes, we have another snippet on that as well, which pretty much uses the same method.
01:11
OK, so let's go ahead and change the name of user profile. We're not going to do this in this file, because this is tucked away within our composer files. We wouldn't want to modify anything about this at all.
01:22
So we're actually just going to take this and copy and paste it over to our routes. Now, that might seem a little bit weird. But let's go ahead and do it.
01:30
And then I'll show you how this all starts to pull together. So let's go over to our routes file. And let's create a Jetstream.php file out. I'm doing this separate, just so these
01:40
don't get bundled into our main web routes, just to keep things nice and tidy. So now what we can do with this is simply require this file in. Now, that will work, but it will give us duplicate routes.
01:51
So we'll fix that up in just a second. So let's do a require once on the current directory. And let's go into Jetstream.php and pull that in. And that's pretty much copying them routes over,
02:06
so we can start to modify them. So for example, user profile, you might just want that to be called profile. Let's go over and check this out.
02:14
So let's get rid of the user part of that. And sure enough, this still works. We can still do everything in here that we normally would, because of course, this still
02:22
points to this controller. Although at this point, you could start to modify that. Now, don't leave just yet, because what we now have is a duplicate route registered.
02:32
We can still access user profile. And of course, we can come over to the command line, list our routes. We'll basically just see a duplicate of the routes
02:40
that we've registered. So what we want to do is, over in the Jetstream service provider, disable the routes that Jetstream provides by default.
02:49
So if we just open up the Jetstream class here, under Jetstream source and Jetstream.php, we have a method just down here called ignore routes. That sets the registered routes property at the top of here
03:01
to false, which then allows us to ignore the routes that are registered by default and use the ones that we've copied over. So really easily, what we can now do in register
03:11
is just say Jetstream ignore, if I can type, routes. And that's it. Let's go over, hit refresh, and sure enough, this user profile is not found, because that's
03:22
the one that's been registered by Jetstream by default. But profile, which we have now copied over, works. So like I said at the start, you can use this to pretty much do anything now.
03:32
You can go ahead and change around any of the if statements in here. You can change around the controllers, the names of these, any middleware that's applied.
03:41
You can pretty much do anything you want now. And this is really useful, for example, if you want to change the terms of service controller or name over, or just have a completely custom controller
03:51
for what you're building within Jetstream. So a really simple solution to this, but again, really important that we ignore the routes that are registered by default.
1 episode 4 mins

Overview

Need to tinker with Laravel Jetstream routes? In this snippet, we’ll take a look at disabling the default routes and registering them in our project codebase instead.

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

Episode discussion

No comments, yet. Be the first!