Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions .ai/wayfinder/core.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
@php
/** @var \Laravel\Boost\Install\GuidelineAssist $assist */
@endphp
## Laravel Wayfinder

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.

### Development Guidelines
- Always use `search-docs` to check wayfinder correct usage before implementing any features.
- Always Prefer named imports for tree-shaking (e.g., `import { show } from '@/actions/...'`)
- Avoid default controller imports (prevents tree-shaking)
- Run `wayfinder:generate` after route changes if Vite plugin isn't installed

### Feature Overview
- Form Support: Use `.form()` with `--with-form` flag for HTML form attributes — `<form {...store.form()}>` → `action="/posts" method="post"`
- HTTP Methods: Call `.get()`, `.post()`, `.patch()`, `.put()`, `.delete()` for specific methods — `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 `@/routes/` for non-controller routes. For example, `import { show } from '@/routes/post'; show(1)` for route name `post.show`
- Parameter Binding: Detects route keys (e.g., `{post:slug}`) and accepts matching object properties — `show("my-post")` or `show({ slug: "my-post" })`
- Query Merging: Use `mergeQuery` to merge with `window.location.search`, set values to `null` to remove — `show(1, { mergeQuery: { page: 2, sort: null } })`
- Query Parameters: Pass `{ query: {...} }` in options to append params — `show(1, { query: { page: 1 } })` → `"/posts/1?page=1"`
- Route Objects: Functions return `{ url, method }` shaped objects — `show(1)` → `{ url: "/posts/1", method: "get" }`
- URL Extraction: Use `.url()` to get URL string — `show.url(1)` → `"/posts/1"`

### Example Usage
@verbatim
<code-snippet name="Wayfinder Basic Usage" lang="typescript">
// Import controller methods (tree-shakable)
import { show, store, update } from '@/actions/App/Http/Controllers/PostController'

// 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>
@endverbatim

@if($assist->roster->uses(\Laravel\Roster\Enums\Packages::INERTIA_LARAVEL) || $assist->roster->uses(\Laravel\Roster\Enums\Packages::INERTIA_REACT) || $assist->roster->uses(\Laravel\Roster\Enums\Packages::INERTIA_VUE) || $assist->roster->uses(\Laravel\Roster\Enums\Packages::INERTIA_SVELTE))
### Wayfinder + Inertia
@if($assist->inertia()->hasFormComponent())
If your application uses the `<Form>` component from Inertia, you can use Wayfinder to generate form action and method automatically.
@if($assist->roster->uses(\Laravel\Roster\Enums\Packages::INERTIA_REACT))
@boostsnippet("Wayfinder Form Component (React)", "typescript")
<Form {...store.form()}><input name="title" /></Form>
@endboostsnippet
@endif
@if($assist->roster->uses(\Laravel\Roster\Enums\Packages::INERTIA_VUE))
@boostsnippet("Wayfinder Form Component (Vue)", "vue")
<Form v-bind="store.form()"><input name="title" /></Form>
@endboostsnippet
@endif
@if($assist->roster->uses(\Laravel\Roster\Enums\Packages::INERTIA_SVELTE))
@boostsnippet("Wayfinder Form Component (Svelte)", "svelte")
<Form {...store.form()}><input name="title" /></Form>
@endboostsnippet
@endif
@else
If your application uses the `useForm` component from Inertia, you can directly submit to the wayfinder generated functions.

<code-snippet name="Wayfinder useForm Example" lang="typescript">
import { store } from "@/actions/App/Http/Controllers/ExampleController";

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

form.submit(store());
</code-snippet>
@endif
@endif
1 change: 1 addition & 0 deletions all.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public function packages(): \Laravel\Roster\PackageCollection
'folio' => \Laravel\Roster\Enums\Packages::FOLIO,
'pennant' => \Laravel\Roster\Enums\Packages::PENNANT,
'tailwindcss' => \Laravel\Roster\Enums\Packages::TAILWINDCSS,
'wayfinder' => \Laravel\Roster\Enums\Packages::WAYFINDER,
];

if (isset($enumMapping[$packageName])) {
Expand Down
160 changes: 158 additions & 2 deletions tests/Feature/Install/GuidelineComposerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
$this->herd = Mockery::mock(Herd::class);
$this->herd->shouldReceive('isInstalled')->andReturn(false)->byDefault();

// Bind the mock to the service container so it's used everywhere
$this->app->instance(Roster::class, $this->roster);

$this->composer = new GuidelineComposer($this->roster, $this->herd);
Expand All @@ -37,7 +36,6 @@
]);

$this->roster->shouldReceive('packages')->andReturn($packages);
// Mock all Inertia package version checks
$this->roster->shouldReceive('usesVersion')
->with(Packages::INERTIA_LARAVEL, '2.1.0', '>=')
->andReturn($shouldIncludeForm);
Expand Down Expand Up @@ -426,3 +424,161 @@
->toContain('Run `npm install` to install dependencies')
->toContain('Package manager: npm');
});

test('includes wayfinder guidelines with inertia integration when both packages are present', function (): void {
$packages = new PackageCollection([
new Package(Packages::LARAVEL, 'laravel/framework', '11.0.0'),
new Package(Packages::WAYFINDER, 'laravel/wayfinder', '1.0.0'),
new Package(Packages::INERTIA_REACT, 'inertiajs/inertia-react', '2.1.2'),
new Package(Packages::INERTIA_LARAVEL, 'inertiajs/inertia-laravel', '2.1.2'),
]);

$this->roster->shouldReceive('packages')->andReturn($packages);

$this->roster->shouldReceive('uses')->with(Packages::INERTIA_LARAVEL)->andReturn(true);
$this->roster->shouldReceive('uses')->with(Packages::INERTIA_REACT)->andReturn(true);
$this->roster->shouldReceive('uses')->with(Packages::INERTIA_VUE)->andReturn(false);
$this->roster->shouldReceive('uses')->with(Packages::INERTIA_SVELTE)->andReturn(false);

$this->roster->shouldReceive('usesVersion')
->with(Packages::INERTIA_LARAVEL, Mockery::any(), '>=')
->andReturn(true);
$this->roster->shouldReceive('usesVersion')
->with(Packages::INERTIA_REACT, Mockery::any(), '>=')
->andReturn(true);
$this->roster->shouldReceive('usesVersion')
->with(Packages::INERTIA_VUE, Mockery::any(), '>=')
->andReturn(false);
$this->roster->shouldReceive('usesVersion')
->with(Packages::INERTIA_SVELTE, Mockery::any(), '>=')
->andReturn(false);

$guidelines = $this->composer->compose();

expect($guidelines)
->toContain('=== wayfinder/core rules ===')
->toContain('Wayfinder + Inertia')
->toContain('Wayfinder Form Component (React)')
->toContain('<Form {...store.form()}>')
->toContain('## Laravel Wayfinder')
->not->toContain('Wayfinder Form Component (Vue)')
->not->toContain('Wayfinder Form Component (Svelte)')
->not->toContain('<Form v-bind="store.form()">');
});

test('includes wayfinder guidelines with inertia vue integration', function (): void {
$packages = new PackageCollection([
new Package(Packages::LARAVEL, 'laravel/framework', '11.0.0'),
new Package(Packages::WAYFINDER, 'laravel/wayfinder', '1.0.0'),
new Package(Packages::INERTIA_VUE, 'inertiajs/inertia-vue', '2.1.2'),
new Package(Packages::INERTIA_LARAVEL, 'inertiajs/inertia-laravel', '2.1.2'),
]);

$this->roster->shouldReceive('packages')->andReturn($packages);

$this->roster->shouldReceive('uses')->with(Packages::INERTIA_LARAVEL)->andReturn(true);
$this->roster->shouldReceive('uses')->with(Packages::INERTIA_REACT)->andReturn(false);
$this->roster->shouldReceive('uses')->with(Packages::INERTIA_VUE)->andReturn(true);
$this->roster->shouldReceive('uses')->with(Packages::INERTIA_SVELTE)->andReturn(false);

$this->roster->shouldReceive('usesVersion')
->with(Packages::INERTIA_LARAVEL, Mockery::any(), '>=')
->andReturn(true);
$this->roster->shouldReceive('usesVersion')
->with(Packages::INERTIA_REACT, Mockery::any(), '>=')
->andReturn(false);
$this->roster->shouldReceive('usesVersion')
->with(Packages::INERTIA_VUE, Mockery::any(), '>=')
->andReturn(true);
$this->roster->shouldReceive('usesVersion')
->with(Packages::INERTIA_SVELTE, Mockery::any(), '>=')
->andReturn(false);

$guidelines = $this->composer->compose();

expect($guidelines)
->toContain('=== wayfinder/core rules ===')
->toContain('Wayfinder + Inertia')
->toContain('Wayfinder Form Component (Vue)')
->toContain('<Form v-bind="store.form()">')
->toContain('## Laravel Wayfinder')
->not->toContain('Wayfinder Form Component (React)')
->not->toContain('Wayfinder Form Component (Svelte)');
});

test('includes wayfinder guidelines with inertia svelte integration', function (): void {
$packages = new PackageCollection([
new Package(Packages::LARAVEL, 'laravel/framework', '11.0.0'),
new Package(Packages::WAYFINDER, 'laravel/wayfinder', '1.0.0'),
new Package(Packages::INERTIA_SVELTE, 'inertiajs/inertia-svelte', '2.1.2'),
new Package(Packages::INERTIA_LARAVEL, 'inertiajs/inertia-laravel', '2.1.2'),
]);

$this->roster->shouldReceive('packages')->andReturn($packages);

$this->roster->shouldReceive('uses')->with(Packages::INERTIA_LARAVEL)->andReturn(true);
$this->roster->shouldReceive('uses')->with(Packages::INERTIA_REACT)->andReturn(false);
$this->roster->shouldReceive('uses')->with(Packages::INERTIA_VUE)->andReturn(false);
$this->roster->shouldReceive('uses')->with(Packages::INERTIA_SVELTE)->andReturn(true);

$this->roster->shouldReceive('usesVersion')
->with(Packages::INERTIA_LARAVEL, Mockery::any(), '>=')
->andReturn(true);
$this->roster->shouldReceive('usesVersion')
->with(Packages::INERTIA_REACT, Mockery::any(), '>=')
->andReturn(false);
$this->roster->shouldReceive('usesVersion')
->with(Packages::INERTIA_VUE, Mockery::any(), '>=')
->andReturn(false);
$this->roster->shouldReceive('usesVersion')
->with(Packages::INERTIA_SVELTE, Mockery::any(), '>=')
->andReturn(true);

$guidelines = $this->composer->compose();

expect($guidelines)
->toContain('=== wayfinder/core rules ===')
->toContain('Wayfinder + Inertia')
->toContain('Wayfinder Form Component (Svelte)')
->toContain('<Form {...store.form()}>')
->toContain('## Laravel Wayfinder')
->not->toContain('Wayfinder Form Component (React)')
->not->toContain('Wayfinder Form Component (Vue)')
->not->toContain('<Form v-bind="store.form()">');
});

test('includes wayfinder guidelines without inertia integration when inertia is not present', function (): void {
$packages = new PackageCollection([
new Package(Packages::LARAVEL, 'laravel/framework', '11.0.0'),
new Package(Packages::WAYFINDER, 'laravel/wayfinder', '1.0.0'),
]);

$this->roster->shouldReceive('packages')->andReturn($packages);

$this->roster->shouldReceive('uses')->with(Packages::INERTIA_LARAVEL)->andReturn(false);
$this->roster->shouldReceive('uses')->with(Packages::INERTIA_REACT)->andReturn(false);
$this->roster->shouldReceive('uses')->with(Packages::INERTIA_VUE)->andReturn(false);
$this->roster->shouldReceive('uses')->with(Packages::INERTIA_SVELTE)->andReturn(false);

$this->roster->shouldReceive('usesVersion')
->with(Packages::INERTIA_LARAVEL, Mockery::any(), '>=')
->andReturn(false);
$this->roster->shouldReceive('usesVersion')
->with(Packages::INERTIA_REACT, Mockery::any(), '>=')
->andReturn(false);
$this->roster->shouldReceive('usesVersion')
->with(Packages::INERTIA_VUE, Mockery::any(), '>=')
->andReturn(false);
$this->roster->shouldReceive('usesVersion')
->with(Packages::INERTIA_SVELTE, Mockery::any(), '>=')
->andReturn(false);

$guidelines = $this->composer->compose();

expect($guidelines)
->toContain('=== wayfinder/core rules ===')
->toContain('## Laravel Wayfinder')
->toContain('import { show } from \'@/actions/')
->not->toContain('Wayfinder + Inertia')
->not->toContain('Wayfinder Form Component');
});