wayfinder
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseWayfinder
Wayfinder
Instructions
使用说明
Wayfinder generates TypeScript functions and types for Laravel controllers and routes which you can import into your client side code. It provides type safety and automatic synchronization between backend routes and frontend code.
Wayfinder可为Laravel控制器和路由生成TypeScript函数与类型,你可以将其导入到客户端代码中。它提供类型安全性,实现后端路由与前端代码的自动同步。
Development Guidelines
开发指南
- Always Prefer named imports for tree-shaking (e.g., )
import { show } from '@/actions/...' - Avoid default controller imports (prevents tree-shaking)
- Run after route changes if there are any errors
php artisan wayfinder:generate
- 优先使用命名导入以支持tree-shaking(例如:)
import { show } from '@/actions/...' - 避免默认控制器导入(会阻碍tree-shaking)
- 若路由变更后出现错误,请运行
php artisan wayfinder:generate
Feature Overview
功能概述
- Form Support: Use with
.form()flag for HTML form attributes —--with-form→<form {...store.form()}>action="/posts" method="post" - HTTP Methods: Call ,
.get(),.post(),.patch(),.put()for specific methods —.delete()→show.head(1){ url: "/posts/1", method: "head" } - Invokable Controllers: Import and invoke directly as functions. For example,
import StorePost from '@/actions/.../StorePostController'; StorePost() - Named Routes: Import from for non-controller routes. For example,
@/routes/for route nameimport { show } from '@/routes/post'; show(1)post.show - Parameter Binding: Detects route keys (e.g., ) and accepts matching object properties —
{post:slug}orshow("my-post")show({ slug: "my-post" }) - Query Merging: Use to merge with
mergeQuery, set values towindow.location.searchto remove —nullshow(1, { mergeQuery: { page: 2, sort: null } }) - Query Parameters: Pass in options to append params —
{ query: {...} }→show(1, { query: { page: 1 } })"/posts/1?page=1" - Route Objects: Functions return shaped objects —
{ url, method }→show(1){ url: "/posts/1", method: "get" } - URL Extraction: Use to get URL string —
.url()→show.url(1)"/posts/1"
- 表单支持:结合与
.form()标志生成HTML表单属性——--with-form会转换为<form {...store.form()}>action="/posts" method="post" - HTTP方法:调用、
.get()、.post()、.patch()、.put()来指定请求方法——.delete()会返回show.head(1){ url: "/posts/1", method: "head" } - 可调用控制器:直接导入并作为函数调用。例如:
import StorePost from '@/actions/.../StorePostController'; StorePost() - 命名路由:从导入非控制器路由。例如,对于路由名称
@/routes/,使用post.showimport { show } from '@/routes/post'; show(1) - 参数绑定:自动识别路由参数(如),接受匹配的对象属性——
{post:slug}或show("my-post")show({ slug: "my-post" }) - 查询参数合并:使用与
mergeQuery合并参数,设置值为window.location.search可移除参数——nullshow(1, { mergeQuery: { page: 2, sort: null } }) - 查询参数传递:在选项中传入来追加参数——
{ query: {...} }会生成show(1, { query: { page: 1 } })"/posts/1?page=1" - 路由对象:函数返回格式的对象——
{ url, method }返回show(1){ url: "/posts/1", method: "get" } - URL提取:使用获取URL字符串——
.url()返回show.url(1)"/posts/1"
Examples
示例
<code-snippet name="Wayfinder Basic Usage" lang="typescript">
// Import controller methods (tree-shakable)
import { show, store, update } from '@/actions/App/Http/Controllers/PostController'
</code-snippet>
// Get route object with URL and method...
show(1) // { url: "/posts/1", method: "get" }
// Get just the URL...
show.url(1) // "/posts/1"
// Use specific HTTP methods...
show.get(1) // { url: "/posts/1", method: "get" }
show.head(1) // { url: "/posts/1", method: "head" }
// Import named routes...
import { show as postShow } from '@/routes/post' // For route name 'post.show'
postShow(1) // { url: "/posts/1", method: "get" }<code-snippet name="Wayfinder Basic Usage" lang="typescript">
// Import controller methods (tree-shakable)
import { show, store, update } from '@/actions/App/Http/Controllers/PostController'
</code-snippet>
// Get route object with URL and method...
show(1) // { url: "/posts/1", method: "get" }
// Get just the URL...
show.url(1) // "/posts/1"
// Use specific HTTP methods...
show.get(1) // { url: "/posts/1", method: "get" }
show.head(1) // { url: "/posts/1", method: "head" }
// Import named routes...
import { show as postShow } from '@/routes/post' // For route name 'post.show'
postShow(1) // { url: "/posts/1", method: "get" }Wayfinder + Inertia
Wayfinder + Inertia
If your application uses the component from Inertia, you can use Wayfinder to generate form action and method automatically.
<code-snippet name="Wayfinder Form Component (React)" lang="typescript">
<Form {...store.form()}><input name="title" /></Form>
</code-snippet><Form>如果你的应用使用Inertia的组件,可通过Wayfinder自动生成表单的action与method属性。
<code-snippet name="Wayfinder Form Component (React)" lang="typescript">
<Form {...store.form()}><input name="title" /></Form>
</code-snippet><Form>