Playing
01. Custom Laravel Fortify 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 quickly show you how to customize any of Laravel Fortify's roots.
00:05
Now I'm using the Laravel Jetstream here, but even if you've implemented Laravel Fortify directly into your project, you'll have the same issue. The roots file that Fortify uses is tucked away
00:16
within our composer files, which of course means you can't really edit these, because then when you deploy this and you pull in your dependencies again,
00:24
all of your changes will be lost. That's pretty obvious, but let's now look at how we can start to modify these. So this is the roots file that we have tucked away.
00:34
I'm going to leave this open, because we're going to need to use that a little bit later. Before we start, though, it's important to know that if you just, for example, wanted
00:40
to add a prefix to your roots, in the Fortify config file you can use the prefix option. So if that's the only thing that you're looking to do, head over to the config and add the prefix in here
00:53
that you want to use. Let's just look at an example of this. So I'm going to say auth. And if we head over now and just give this page a refresh,
01:01
you'll notice just at the very bottom of my browser the logout root is now prefixed with auth, and that works as we would expect. So all of these are now prefixed with auth.
01:10
So if that's something you want to do, you don't need to follow the rest of the steps. OK, so with this roots file, the goal here is to copy this, basically copy the entire file over to our project.
01:22
Let's start by doing that. So I'm going to grab the entire content of this roots file, and we're going to come down to where our roots live under our roots file.
01:33
And I'm just going to create out a Fortify.php file and paste that in. Now, over in web, what we can do is require this file in. So let's go ahead and say require once,
01:44
go into the current directory, and we'll just put in Fortify.php. Now, that's not really going to do anything at the moment. If we just come up to our Fortify roots file
01:56
and we modify maybe, let's just choose something in here to modify a login page. Let's say that we want this to be sign in instead. So let's just modify a couple of these, sign in,
02:08
and that should be fine. Let's come over to slash sign in, and that does actually work. Now, the only issue is if we now head over to slash login,
02:19
that also works. What we've basically done, and that's pretty obvious, by copying the roots and registering them again, is we've got two versions of them root, which is not good.
02:30
So that's really the step to modify your roots within Fortify. Just copy the file over. However, we want to make sure we're not
02:39
double registering these. So let's head back over to sign in. We know that works. And let's go up to the provider that Fortify gives us,
02:49
Fortify Service Provider. Now, in here, this is doing a lot of things. We have looked at this in another course if you want to go ahead and check out Fortify.
02:57
But what we want to do in here is examine the Fortify file and check if we can actually disable the default routes that Fortify gives us. Let's go all the way down to the bottom here,
03:09
and you can see we've got a static method in here called ignore root, which sets register's roots to false at the top. So we've got a property up here called register's roots.
03:21
That's by default set to true. So the goal then is to just invoke this, and that will get rid of the previous root. So I'm going to go ahead and do this in register
03:31
and just say Fortify. And let's just check the name of that, ignore roots. Pop that in there, and we should be good. So let's just try this out.
03:40
Sign in still works because that's our root file. But now, if we head over to log in, sure enough, that doesn't work. So the first step was obviously just copying them files over
03:50
so we have our own roots, and we can change them around, whether it's the name, any middleware that's applied to them, the controller that these link up to, but then disabling the prior root
04:00
so we don't have two of these registered. So that's how easy it is. Ignore the base roots, requiring your own roots, which you've copied over from Fortify.

Episode summary

In this episode, we look at how you can customize the routes that Laravel Fortify provides out of the box. By default, all of Fortify's routes are hidden away inside your vendor folder (since they're distributed via Composer), which means you can't just go in and change them. If you try, your changes will get overridden next time you update your dependencies.

First, we discuss the simple case—if all you want is to add a prefix (like /auth/) to your Fortify routes, you can just update the prefix option in the Fortify config file. That covers the basics, but what if you want more control?

To get full control over the routes (changing paths like /login to /sign-in, or attaching your own middleware or controllers), you need to copy the actual Fortify routes file into your own routes folder (for example, as routes/Fortify.php). Then, require that file from your routes/web.php file. This lets you fully customize each route as you see fit.

However, simply copying them in means you'll have both the default Fortify routes and your new ones registered—so paths like /login AND your replacement (/sign-in) will both work. To avoid this, you need to disable Fortify's default route registration. You do this by calling Fortify::ignoreRoutes() in your service provider (usually in the register method).

After that, only your customized routes will be active, giving you complete control over your authentication URLs.

So, in summary: copy Fortify's route file into your app, require it, then disable the originals with ignoreRoutes(). That way, you can easily customize things like URL paths, middleware, and controllers that power your app's authentication!

Episode discussion

No comments, yet. Be the first!