>_ DevTrendsen

Language

Home

Languages

Sections

Frontend Backend Mobile DevOps AI / ML GameDev Security
PHP

Laravel Wayfinder: When Backend and Frontend Speak the Same Language

1,752 stars

Laravel Wayfinder Logo

Know this situation? You're happily writing frontend in TypeScript, but somewhere in the depths of your Laravel backend, a route URL or set of parameters changes. And then, after hours of debugging, you realize your frontend is sending requests to nowhere or with incorrect data. It hurts, it's frustrating, it's annoying. What if your TypeScript frontend always knew which routes and methods were available on the backend, complete with full type safety?

This is exactly the problem Laravel Wayfinder solves – a fresh but already very promising project from the Laravel team. It's not just another utility, but a real bridge that connects your Laravel backend and TypeScript frontend, making them a single, harmoniously working whole. Forget about manual synchronization, hardcoded URLs, and headaches from changed parameters. Wayfinder does this for you, automatically.

What is Laravel Wayfinder and Why Do You Need It?

Essentially, Laravel Wayfinder is a tool that takes your Laravel controllers and routes, analyzes them, and based on this information generates fully typed TypeScript functions. These functions can be imported and used in your client code just like any other TypeScript functions. This means you can call backend endpoints directly from the frontend without worrying about the correctness of the URL, HTTP method, or set of parameters.

Who will benefit from this? Any developer building modern single-page applications (SPAs) or hybrid projects where Laravel serves as the API and the frontend is written in React, Vue, Angular, or Svelte using TypeScript. If you value type safety, development speed, and want to minimize errors related to frontend-backend interaction, Wayfinder is your choice.

Key Features: How Wayfinder Changes the Game

Let's break down what makes Wayfinder such a convenient and powerful tool.

1. Automatic Generation of Type-Safe Functions

This is Wayfinder's heart. Instead of manually writing interfaces or types for each endpoint, Wayfinder does it for you. It analyzes your controllers and routes, then creates corresponding TypeScript files. For example, if you have a controller PostController with a method show(Post $post), Wayfinder will generate a function that expects a post ID and returns an object with URL and method.

import { show } from "@/actions/App/Http/Controllers/PostController";

show(1); // { url: "/posts/1", method: "get" }

Or, if you only need the URL:

import { show } from "@/actions/App/Http/Controllers/PostController";

show.url(1); // "/posts/1"

2. Flexible Route Parameter Handling

Wayfinder can handle various forms of parameter passing, making it very convenient. You can pass them as separate arguments, objects, or even nested objects if the route uses model binding by key (for example, {post:slug}).

import { show, update } from "@/actions/App/Http/Controllers/PostController";

// Одиночный параметр
show(1);
show({ id: 1 });

// Несколько параметров
update([1, 2]);
update({ post: 1, author: 2 });
update({ post: { id: 1 }, author: { id: 2 } });

// Если роут /posts/{post:slug}
show("my-new-post");
show({ slug: "my-new-post" });

This allows you to write cleaner and more intuitive frontend code without thinking about how Laravel expects parameters on the backend.

3. Support for Named Routes and Invokable Controllers

Wayfinder isn't limited to controller methods only. It can also generate functions for named routes, which is very convenient if you actively use the route() helper in Laravel.

import { show } from "@/routes/post";

// Если именованный роут `post.show`
show(1); // { url: "/posts/1", method: "get" }

And if you have invokable controllers (those using the __invoke method), Wayfinder supports them too:

import StorePostController from "@/actions/App/Http/Controllers/StorePostController";

StorePostController();

4. Simplified HTML Forms

Even if you're using traditional HTML forms, Wayfinder can significantly simplify their creation. By enabling the --with-form option during generation, you'll get a special variant of .form() that automatically generates action and method attributes for your <form> tag.

import { store, update } from "@/actions/App/Http/Controllers/PostController";

const Page = () => (
    <form {...store.form()}>
        {/* <form action="/posts" method="post"> */}
        {/* ... */}
    </form>
);

const Page = () => (
    <form {...update.form(1)}>
        {/* <form action="/posts/1?_method=PATCH" method="post"> */}
        {/* ... */}
    </form>
);

This eliminates the need for manually forming URLs and hidden fields for HTTP methods other than GET and POST.

5. Query Parameter Management

Need to add query parameters to a URL? Wayfinder provides a convenient options object with a query or mergeQuery field.

import { show } from "@/actions/App/Http/Controllers/PostController";

const options = {
    query: {
        page: 1,
        sort_by: "name",
    },
};

show(1, options); // { url: "/posts/1?page=1&sort_by=name", method: "get" }

And mergeQuery allows you to merge parameters with those already in the URL, which is very convenient for pagination or filtering.

6. Seamless Integration with Inertia.js

For those working with Inertia.js, Wayfinder is a real find. The results of Wayfinder functions can be passed directly to useForm().submit() or to href of the Link component.

import { useForm } from "@inertiajs/react";
import { store } from "@/actions/App/Http/Controllers/PostController";

const form = useForm({
    name: "My Big Post",
});

form.submit(store()); // Отправит POST запрос на `/posts`
import { Link } from "@inertiajs/react";
import { show } from "@/actions/App/Http/Controllers/PostController";

const Nav = () => <Link href={show(1)}>Показать первый пост</Link>;

This maximally simplifies backend interaction in Inertia applications, making the code cleaner and more reliable.

How It Works Under the Hood

Laravel Wayfinder consists of two main parts:

  1. Composer package for Laravel: It contains the logic for analyzing your routes and controllers, as well as the php artisan wayfinder:generate command that performs the main work of generating TypeScript files. These files are saved by default to resources/js/wayfinder, resources/js/actions, and resources/js/routes.
  2. Vite plugin @laravel/vite-plugin-wayfinder: This plugin ensures your routes are automatically regenerated during Vite build and whenever files change during dev server operation. This is critically important for keeping types up to date.

The generated files can be safely added to .gitignore, as they are fully recreated on each build or when the generation command is run.

Practical Application: Fewer Errors, More Speed

In my practice, I often encounter that frontend-backend integration is a source of many errors. Incorrectly written URL, forgotten parameter, changed route name that wasn't synchronized on the frontend... Wayfinder solves these problems at the root. It brings confidence and predictability to the development process:

  • Reduced error count: Thanks to type safety, most endpoint problems will be caught at the TypeScript compilation stage, not at runtime.
  • Faster development: You no longer need to constantly switch between backend and frontend to check route names or parameters. Everything is available right in your IDE with autocompletion.
  • Improved code maintainability: Code becomes more readable and understandable, as you can see what data is expected for each API call.
  • Easier onboarding: New team members will find it much easier to understand how to interact with the API, thanks to clearly defined types.

Conclusion: Is It Worth Trying?

Absolutely! Laravel Wayfinder is a breath of fresh air for everyone building applications with Laravel and TypeScript. It significantly simplifies and improves the interaction process between the two parts of your application. Although the project is in Beta stage and the API may change (as the developers themselves warn), its potential is obvious. Given that this is an official package from the Laravel team, we can expect active development and stability in the future.

Who is it especially suited for?

  • Developers using Laravel and TypeScript (obviously).
  • Those working with Vite for frontend builds.
  • Projects actively using Inertia.js, where Wayfinder truly shines.
  • Teams striving for higher code quality and fewer bugs.

I strongly recommend trying Laravel Wayfinder in your next or current project. It can become that very tool that makes your development more enjoyable, efficient, and, most importantly, less prone to errors. Let your backend and frontend finally speak the same, universally understood language!

Related projects