Laravelvideo· Apr 9, 2025· 10:15
Everyone loves types, right? But what does type safety actually look like when it comes to a full stack Laravel application where you have a PHP backend within Laravel and then you have a JavaScript front end with something like inertia? Well, it looks a little bit different today than it might have in the past, especially with WFinder. So, what is Wfinder and what can it do for us, especially with inertia? Why don't we take a look? Put simply, Wayfinder
generates fully typed importable functions for your controllers and routes throughout your Laravel application. Now, I get it. Those are a whole bunch of big words, but what does it actually look like when you start using it? I'm starting off with a brand new Laravel application with the React Starter Kit, and then we can install Wfinder into it and see what it does. Just to note, Wfinder is currently in beta, so the API is subject to change prior to the v1.0 release. We can get started with composer require laravel
wayfinder. Now this application already has routes that I could generate and without doing anything else all I could do is say PHP artisan wavefinder generate. And you see here we have some generated actions and routes available for us now in our application. This is great if you want to generate type safe functions for your routes and your controllers whenever you want manually using that PHP artisan command. But there should be a way to do it easier,
right? a little bit more automatic. Yes, there is. So, you can only generate actions or routes using the skip actions/ routes respectively. But also, we can use a VE plugin to run this whenever we make changes to specific files. In fact, those instructions are right here for you in the Wavefinder repo. You can find that in the description. Let's go ahead and do this. I installed the V plugin run. And then in my V config, I just need to say I
want to run this run plugin when specific files change. Go ahead and yink this. Open this up in my code editor. vcon config and run. And this is imported by run from vit plugin run. So we should be good to go. But since I already generated the actions and the routes through the wavefinder generate php artisan command, we have these already available for us in our starter
kit application. Why don't I pull them up and see if we can swap some out from what inertia already gives us within this React starter kit. It is important to note that this is not inertia specific at all. In fact, if you're using even something like Axios or fetch, this works fantastic. Wayfinder just gives you a way to generate those type safe functions so that you can consume them however you want on your front end. But since we have an inertia starter kit, it's great to show how that
works specifically. The biggest difference is instead of using any of the available methods within the form object, get post, put, patch, destroy, you now can just call the form.submit method within your inertia form helper. And then you just pass the action. Inertia automatically completes the rest. So why don't we head to the register page? And we already have within this starter kit Ziggy, which is
awesome. It gives you named routes. So you don't have to pass in for like this href, you don't have to pass in slash login or remember what that route might have been. You can just use the name parameters that you have set in your web.php or wherever your routes are being stored. And this is great. And if you still want to use ziggate, you can. But Wayfinder allows you to have that type safety as well where I don't have
to remember if I called it login. I can actually just import the login route so that I know it's being passed down correctly. Here I could just say login and I have this at route/lo where I can just import that and we should be good to go. So now this already have an account login is being sent or being routed because of this href. Now I'm
just passing in the login route. What did we import? We imported login from at /outs/lo. And we can make this even visually more appealing or cleaner in our vit config. We could use aliases like it's shown here in the wayfinder GitHub repo. But for now, if I run composerdev, I go to that /lo route. I should be able to just automatically go
to that register page because we now have that uh type safe route being passed in. So on the login page, don't have an account, sign up and it just takes me right there. I didn't have to change anything else in my application. It just works. Now what does this look like for actions? Well, I have a very simple demo button on my dashboard. All I'm doing is just making a post request to a specific route. And this is how I
would do it in current inertia form. But how would we do this with named actions? In this case, a function that I can pass down because of what is generated with Wfinder. So, I'm using an invocable controller which can work with WFinder. I'm using a send demo action invocable controller. Now, in dashboard, I don't have this because there's no route associated with this action just yet.
Where would Inertia know where to post it to? If I was to search for uh the let's say send action, it's not there. And of course I have the composer rundev running. So wayfinder should generate any new files but I don't have that in here just yet. But if I go to our web routes folder and register this maybe a route uh post and we can say demo
request and I just want to return the send demo action class right there. Back in our dashboard, if I imported the send demo action controller, again, I didn't run PHP artisan wayfinder generate. It just does it for me. And now I have this function available. So instead of posting to this demo request, all I have to do is submit to the send demo action
function. And then we'll just bring this in into the structure from the use form. So we have submit from inertia. and I'm not using post anymore. So now what we're doing because the wayfinder gives you the ability to say, okay, I don't need to know if this is a post request or a put request or a patch request or destroy request. I just need to attach it to the submit action within the
inertia form helper. So I'm on the dashboard request server data. It's sleeping. This is why I need to double check the code because I do not want to sleep this long. Let's just go for two seconds and then we should be good to go. And back on the dashboard, request server data. 1 second, 2 seconds. And now we have our server response. This just shows in a
very minuscule way how you might use actions within your particular application. Well, how can we maybe update an existing inertia action to have this kind of type safety within Wfinder? Why don't we do the register controller? Let's make this create account button actually work with the new actions in register.tsx. You might think, okay, we
for our route down here, we inputed this route. So, I could just uh import that route right here, right? Well, that's not exactly it. We need to submit the form and then use the action from the actual controller that we are making this post request to. because if I was to use this route right here, that would be a a get route for the register page.
So instead, I'm going to bring in the submit method from the use form helper. Uh, and that means I'm going to say this is the submit form. And we'll go ahead and change that here. Submit form. Perfect. And instead of post, I want this to be submit. And then instead of this route register being passed into the submit method, I'm going to bring in
the controller action. And that's going to be the uh registered user controller. And I believe it's the store method there. Let's go ahead and check. Registered user controller. Looks like we have a create and that's just saying we're going to use the off/register page. But yes, the store method is the one we want. Well, of course, we could dstructure
that and we could go uh store from that register user controller method or we could just use the full register user controller name space and then in our submit say we'll use the registered user controller and then of course we have type safety. So we have either the create or store method. So, we can do
that and that would be a function. But since I already know what this page is doing, I'm just going to say that we're going to dstructure this and use the store method. So now our register.tsx page is storing based off of the registered user controller store method. Just to check Josh
demo. And there we go. So whether you like types or you don't really care too much, Wfinder does give you that little bit of extra safety cushion, maybe a little bit more peace of mind knowing that you can just find whatever method you know that you created in PHP and have it automatically transpiled into a JavaScript type for you to use, whether that's on your
JavaScript front end outside of Inertia or in Inertia to make it even easier. [Music]