ziggy-js 1.7.2 → 1.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +202 -236
- package/dist/index.es.js +1 -1
- package/dist/index.js +1 -1
- package/dist/index.m.js +1 -1
- package/dist/react.es.js +1 -1
- package/dist/react.js +1 -1
- package/dist/react.m.js +1 -1
- package/dist/vue.es.js +1 -1
- package/dist/vue.js +1 -1
- package/dist/vue.m.js +1 -1
- package/package.json +21 -14
- package/src/js/index.d.ts +172 -0
- package/src/js/Route.js +0 -123
- package/src/js/Router.js +0 -275
- package/src/js/index.js +0 -7
- package/src/js/react.js +0 -9
- package/src/js/vue.js +0 -17
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-

|
|
2
2
|
|
|
3
3
|
# Ziggy – Use your Laravel routes in JavaScript
|
|
4
4
|
|
|
@@ -8,142 +8,133 @@
|
|
|
8
8
|
[](https://npmjs.com/package/ziggy-js)
|
|
9
9
|
[](https://npmjs.com/package/ziggy-js)
|
|
10
10
|
|
|
11
|
-
Ziggy provides a JavaScript `route()`
|
|
11
|
+
Ziggy provides a JavaScript `route()` function that works like Laravel's, making it a breeze to use your named Laravel routes in JavaScript.
|
|
12
12
|
|
|
13
13
|
Ziggy supports all versions of Laravel from `5.4` onward, and all modern browsers.
|
|
14
14
|
|
|
15
15
|
- [**Installation**](#installation)
|
|
16
16
|
- [**Usage**](#usage)
|
|
17
|
-
- [
|
|
18
|
-
- [
|
|
17
|
+
- [`route()` function](#route-function)
|
|
18
|
+
- [`Router` class](#router-class)
|
|
19
19
|
- [Route-model binding](#route-model-binding)
|
|
20
|
-
- [TypeScript
|
|
21
|
-
- [**
|
|
22
|
-
- [
|
|
20
|
+
- [TypeScript](#typescript)
|
|
21
|
+
- [**JavaScript frameworks**](#javascript-frameworks)
|
|
22
|
+
- [Generating and importing Ziggy's configuration](#generating-and-importing-ziggys-configuration)
|
|
23
|
+
- [Importing the `route()` function](#importing-the-route-function)
|
|
23
24
|
- [Vue](#vue)
|
|
24
25
|
- [React](#react)
|
|
25
26
|
- [SPAs or separate repos](#spas-or-separate-repos)
|
|
26
27
|
- [**Filtering Routes**](#filtering-routes)
|
|
27
|
-
- [
|
|
28
|
-
- [Filtering
|
|
28
|
+
- [Including/excluding routes](#includingexcluding-routes)
|
|
29
|
+
- [Filtering with groups](#filtering-with-groups)
|
|
29
30
|
- [**Other**](#other)
|
|
30
31
|
- [**Contributing**](#contributing)
|
|
31
32
|
|
|
32
33
|
## Installation
|
|
33
34
|
|
|
34
|
-
Install Ziggy
|
|
35
|
+
Install Ziggy in your Laravel app with Composer:
|
|
35
36
|
|
|
36
|
-
|
|
37
|
+
```bash
|
|
38
|
+
composer require tightenco/ziggy
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Add the `@routes` Blade directive to your main layout (_before_ your application's JavaScript), and the `route()` helper function will be available globally!
|
|
37
42
|
|
|
38
43
|
> By default, the output of the `@routes` Blade directive includes a list of all your application's routes and their parameters. This route list is included in the HTML of the page and can be viewed by end users. To configure which routes are included in this list, or to show and hide different routes on different pages, see [Filtering Routes](#filtering-routes).
|
|
39
44
|
|
|
40
45
|
## Usage
|
|
41
46
|
|
|
42
|
-
|
|
47
|
+
### `route()` function
|
|
43
48
|
|
|
44
|
-
Ziggy's `route()`
|
|
49
|
+
Ziggy's `route()` function works like [Laravel's `route()` helper](https://laravel.com/docs/10.x/helpers#method-route)—you can pass it the name of a route, and the parameters you want to pass to the route, and it will generate a URL.
|
|
45
50
|
|
|
46
|
-
|
|
51
|
+
#### Basic usage
|
|
47
52
|
|
|
48
53
|
```php
|
|
49
|
-
// routes/web.php
|
|
50
|
-
|
|
51
54
|
Route::get('posts', fn (Request $request) => /* ... */)->name('posts.index');
|
|
52
55
|
```
|
|
53
56
|
|
|
54
57
|
```js
|
|
55
|
-
// app.js
|
|
56
|
-
|
|
57
58
|
route('posts.index'); // 'https://ziggy.test/posts'
|
|
58
59
|
```
|
|
59
60
|
|
|
60
|
-
|
|
61
|
+
#### Parameters
|
|
61
62
|
|
|
62
63
|
```php
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
Route::get('posts/{post}', fn (Request $request, Post $post) => /* ... */)->name('posts.show');
|
|
64
|
+
Route::get('posts/{post}', fn (Post $post) => /* ... */)->name('posts.show');
|
|
66
65
|
```
|
|
67
66
|
|
|
68
67
|
```js
|
|
69
|
-
// app.js
|
|
70
|
-
|
|
71
68
|
route('posts.show', 1); // 'https://ziggy.test/posts/1'
|
|
72
69
|
route('posts.show', [1]); // 'https://ziggy.test/posts/1'
|
|
73
70
|
route('posts.show', { post: 1 }); // 'https://ziggy.test/posts/1'
|
|
74
71
|
```
|
|
75
72
|
|
|
76
|
-
|
|
73
|
+
#### Multiple parameters
|
|
77
74
|
|
|
78
75
|
```php
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
Route::get('events/{event}/venues/{venue}', fn (Request $request, Event $event, Venue $venue) => /* ... */)->name('events.venues.show');
|
|
76
|
+
Route::get('venues/{venue}/events/{event}', fn (Venue $venue, Event $event) => /* ... */)
|
|
77
|
+
->name('venues.events.show');
|
|
82
78
|
```
|
|
83
79
|
|
|
84
80
|
```js
|
|
85
|
-
//
|
|
86
|
-
|
|
87
|
-
route('events.venues.show', [1, 2]); // 'https://ziggy.test/events/1/venues/2'
|
|
88
|
-
route('events.venues.show', { event: 1, venue: 2 }); // 'https://ziggy.test/events/1/venues/2'
|
|
81
|
+
route('venues.events.show', [1, 2]); // 'https://ziggy.test/venues/1/events/2'
|
|
82
|
+
route('venues.events.show', { venue: 1, event: 2 }); // 'https://ziggy.test/venues/1/events/2'
|
|
89
83
|
```
|
|
90
84
|
|
|
91
|
-
|
|
85
|
+
#### Query parameters
|
|
92
86
|
|
|
93
|
-
|
|
94
|
-
// routes/web.php
|
|
87
|
+
Ziggy adds arguments that don't match any named route parameters as query parameters:
|
|
95
88
|
|
|
96
|
-
|
|
89
|
+
```php
|
|
90
|
+
Route::get('venues/{venue}/events/{event}', fn (Venue $venue, Event $event) => /* ... */)
|
|
91
|
+
->name('venues.events.show');
|
|
97
92
|
```
|
|
98
93
|
|
|
99
94
|
```js
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
event: 1,
|
|
104
|
-
venue: 2,
|
|
95
|
+
route('venues.events.show', {
|
|
96
|
+
venue: 1,
|
|
97
|
+
event: 2,
|
|
105
98
|
page: 5,
|
|
106
99
|
count: 10,
|
|
107
100
|
});
|
|
108
|
-
// 'https://ziggy.test/
|
|
101
|
+
// 'https://ziggy.test/venues/1/events/2?page=5&count=10'
|
|
109
102
|
```
|
|
110
103
|
|
|
111
|
-
If you
|
|
104
|
+
If you need to pass a query parameter with the same name as a route parameter, nest it under the special `_query` key:
|
|
112
105
|
|
|
113
106
|
```js
|
|
114
|
-
route('events.
|
|
115
|
-
|
|
116
|
-
|
|
107
|
+
route('venues.events.show', {
|
|
108
|
+
venue: 1,
|
|
109
|
+
event: 2,
|
|
117
110
|
_query: {
|
|
118
111
|
event: 3,
|
|
119
112
|
page: 5,
|
|
120
113
|
},
|
|
121
114
|
});
|
|
122
|
-
// 'https://ziggy.test/
|
|
115
|
+
// 'https://ziggy.test/venues/1/events/2?event=3&page=5'
|
|
123
116
|
```
|
|
124
117
|
|
|
125
|
-
Like Laravel
|
|
118
|
+
Like Laravel, Ziggy automatically encodes boolean query parameters as integers in the query string:
|
|
126
119
|
|
|
127
120
|
```js
|
|
128
|
-
route('events.
|
|
129
|
-
|
|
130
|
-
|
|
121
|
+
route('venues.events.show', {
|
|
122
|
+
venue: 1,
|
|
123
|
+
event: 2,
|
|
131
124
|
_query: {
|
|
132
125
|
draft: false,
|
|
133
126
|
overdue: true,
|
|
134
127
|
},
|
|
135
128
|
});
|
|
136
|
-
// 'https://ziggy.test/
|
|
129
|
+
// 'https://ziggy.test/venues/1/events/2?draft=0&overdue=1'
|
|
137
130
|
```
|
|
138
131
|
|
|
139
|
-
|
|
132
|
+
#### Default parameter values
|
|
140
133
|
|
|
141
|
-
|
|
134
|
+
Ziggy supports default route parameter values ([Laravel docs](https://laravel.com/docs/urls#default-values)).
|
|
142
135
|
|
|
143
136
|
```php
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
Route::get('{locale}/posts/{post}', fn (Request $request, Post $post) => /* ... */)->name('posts.show');
|
|
137
|
+
Route::get('{locale}/posts/{post}', fn (Post $post) => /* ... */)->name('posts.show');
|
|
147
138
|
```
|
|
148
139
|
|
|
149
140
|
```php
|
|
@@ -153,12 +144,12 @@ URL::defaults(['locale' => $request->user()->locale ?? 'de']);
|
|
|
153
144
|
```
|
|
154
145
|
|
|
155
146
|
```js
|
|
156
|
-
// app.js
|
|
157
|
-
|
|
158
147
|
route('posts.show', 1); // 'https://ziggy.test/de/posts/1'
|
|
159
148
|
```
|
|
160
149
|
|
|
161
|
-
|
|
150
|
+
#### Examples
|
|
151
|
+
|
|
152
|
+
HTTP request with `axios`:
|
|
162
153
|
|
|
163
154
|
```js
|
|
164
155
|
const post = { id: 1, title: 'Ziggy Stardust' };
|
|
@@ -166,14 +157,14 @@ const post = { id: 1, title: 'Ziggy Stardust' };
|
|
|
166
157
|
return axios.get(route('posts.show', post)).then((response) => response.data);
|
|
167
158
|
```
|
|
168
159
|
|
|
169
|
-
|
|
160
|
+
### `Router` class
|
|
170
161
|
|
|
171
|
-
Calling Ziggy's `route()`
|
|
162
|
+
Calling Ziggy's `route()` function with no arguments will return an instance of its JavaScript `Router` class, which has some other useful properties and methods.
|
|
172
163
|
|
|
173
|
-
|
|
164
|
+
#### Check the current route: `route().current()`
|
|
174
165
|
|
|
175
166
|
```js
|
|
176
|
-
//
|
|
167
|
+
// Laravel route called 'events.index' with URI '/events'
|
|
177
168
|
// Current window URL is https://ziggy.test/events
|
|
178
169
|
|
|
179
170
|
route().current(); // 'events.index'
|
|
@@ -182,40 +173,41 @@ route().current('events.*'); // true
|
|
|
182
173
|
route().current('events.show'); // false
|
|
183
174
|
```
|
|
184
175
|
|
|
185
|
-
|
|
176
|
+
`route().current()` optionally accepts parameters as its second argument, and will check that their values also match in the current URL:
|
|
186
177
|
|
|
187
178
|
```js
|
|
188
|
-
//
|
|
189
|
-
// Current window URL is https://myapp.com/
|
|
179
|
+
// Laravel route called 'venues.events.show' with URI '/venues/{venue}/events/{event}'
|
|
180
|
+
// Current window URL is https://myapp.com/venues/1/events/2?hosts=all
|
|
190
181
|
|
|
191
|
-
route().current('events.
|
|
192
|
-
route().current('events.
|
|
193
|
-
route().current('events.
|
|
182
|
+
route().current('venues.events.show', { venue: 1 }); // true
|
|
183
|
+
route().current('venues.events.show', { venue: 1, event: 2 }); // true
|
|
184
|
+
route().current('venues.events.show', { hosts: 'all' }); // true
|
|
185
|
+
route().current('venues.events.show', { venue: 6 }); // false
|
|
194
186
|
```
|
|
195
187
|
|
|
196
|
-
|
|
188
|
+
#### Check if a route exists: `route().has()`
|
|
197
189
|
|
|
198
190
|
```js
|
|
199
|
-
//
|
|
191
|
+
// Laravel app has only one named route, 'home'
|
|
200
192
|
|
|
201
193
|
route().has('home'); // true
|
|
202
194
|
route().has('orders'); // false
|
|
203
195
|
```
|
|
204
196
|
|
|
205
|
-
|
|
197
|
+
#### Retrieve the current route params: `route().params`
|
|
206
198
|
|
|
207
199
|
```js
|
|
208
|
-
//
|
|
209
|
-
// Current window URL is https://myapp.com/
|
|
200
|
+
// Laravel route called 'venues.events.show' with URI '/venues/{venue}/events/{event}'
|
|
201
|
+
// Current window URL is https://myapp.com/venues/1/events/2?hosts=all
|
|
210
202
|
|
|
211
|
-
route().params; // {
|
|
203
|
+
route().params; // { venue: '1', event: '2', hosts: 'all' }
|
|
212
204
|
```
|
|
213
205
|
|
|
214
206
|
> Note: parameter values retrieved with `route().params` will always be returned as strings.
|
|
215
207
|
|
|
216
|
-
|
|
208
|
+
### Route-model binding
|
|
217
209
|
|
|
218
|
-
Ziggy supports Laravel [route-model binding](https://laravel.com/docs/routing#route-model-binding), and can even recognize custom route key names. If you pass `route()` a JavaScript object as
|
|
210
|
+
Ziggy supports Laravel's [route-model binding](https://laravel.com/docs/routing#route-model-binding), and can even recognize custom route key names. If you pass `route()` a JavaScript object as a route parameter, Ziggy will use the registered route-model binding keys for that route to find the correct parameter value inside the object. If no route-model binding keys are explicitly registered for a parameter, Ziggy will use the object's `id` key.
|
|
219
211
|
|
|
220
212
|
```php
|
|
221
213
|
// app/Models/Post.php
|
|
@@ -230,230 +222,231 @@ class Post extends Model
|
|
|
230
222
|
```
|
|
231
223
|
|
|
232
224
|
```php
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
{
|
|
237
|
-
public function show(Request $request, Post $post)
|
|
238
|
-
{
|
|
239
|
-
return view('posts.show', ['post' => $post]);
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
```
|
|
243
|
-
|
|
244
|
-
```php
|
|
245
|
-
// routes/web.php
|
|
246
|
-
|
|
247
|
-
Route::get('blog/{post}', [PostController::class, 'show'])->name('posts.show');
|
|
225
|
+
Route::get('blog/{post}', function (Post $post) {
|
|
226
|
+
return view('posts.show', ['post' => $post]);
|
|
227
|
+
})->name('posts.show');
|
|
248
228
|
```
|
|
249
229
|
|
|
250
230
|
```js
|
|
251
|
-
// app.js
|
|
252
|
-
|
|
253
231
|
const post = {
|
|
232
|
+
id: 3,
|
|
254
233
|
title: 'Introducing Ziggy v1',
|
|
255
234
|
slug: 'introducing-ziggy-v1',
|
|
256
235
|
date: '2020-10-23T20:59:24.359278Z',
|
|
257
236
|
};
|
|
258
237
|
|
|
259
|
-
// Ziggy knows that this route uses the 'slug' route-model binding key
|
|
238
|
+
// Ziggy knows that this route uses the 'slug' route-model binding key:
|
|
260
239
|
|
|
261
240
|
route('posts.show', post); // 'https://ziggy.test/blog/introducing-ziggy-v1'
|
|
262
241
|
```
|
|
263
242
|
|
|
264
|
-
Ziggy also supports [custom keys](https://laravel.com/docs/routing#customizing-the-key) for scoped bindings in
|
|
243
|
+
Ziggy also supports [custom keys](https://laravel.com/docs/routing#customizing-the-key) for scoped bindings declared directly in a route definition:
|
|
265
244
|
|
|
266
245
|
```php
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
Route::get('authors/{author}/photos/{photo:uuid}', fn (Request $request, Author $author, Photo $photo) => /* ... */)->name('authors.photos.show');
|
|
246
|
+
Route::get('authors/{author}/photos/{photo:uuid}', fn (Author $author, Photo $photo) => /* ... */)
|
|
247
|
+
->name('authors.photos.show');
|
|
270
248
|
```
|
|
271
249
|
|
|
272
250
|
```js
|
|
273
|
-
// app.js
|
|
274
|
-
|
|
275
251
|
const photo = {
|
|
276
252
|
uuid: '714b19e8-ac5e-4dab-99ba-34dc6fdd24a5',
|
|
277
253
|
filename: 'sunset.jpg',
|
|
278
254
|
}
|
|
279
255
|
|
|
280
|
-
route('authors.photos.show', [{ id: 1, name: '
|
|
256
|
+
route('authors.photos.show', [{ id: 1, name: 'Ansel' }, photo]);
|
|
281
257
|
// 'https://ziggy.test/authors/1/photos/714b19e8-ac5e-4dab-99ba-34dc6fdd24a5'
|
|
282
258
|
```
|
|
283
259
|
|
|
284
|
-
|
|
260
|
+
### TypeScript
|
|
285
261
|
|
|
286
|
-
|
|
262
|
+
Ziggy includes TypeScript type definitions, and an Artisan command that can generate additional type definitions to enable route name and parameter autocompletion.
|
|
287
263
|
|
|
288
|
-
|
|
264
|
+
To generate route types, run the `ziggy:generate` command with the `--types` or `--types-only` option:
|
|
289
265
|
|
|
290
|
-
|
|
266
|
+
```bash
|
|
267
|
+
php artisan ziggy:generate --types
|
|
268
|
+
```
|
|
291
269
|
|
|
292
|
-
|
|
270
|
+
To make your IDE aware that Ziggy's `route()` helper is available globally, and to type it correctly, add a declaration like this in a `.d.ts` file somewhere in your project:
|
|
293
271
|
|
|
294
|
-
|
|
272
|
+
```ts
|
|
273
|
+
import routeFn from 'ziggy-js';
|
|
295
274
|
|
|
296
|
-
|
|
297
|
-
|
|
275
|
+
declare global {
|
|
276
|
+
var route: typeof routeFn;
|
|
277
|
+
}
|
|
278
|
+
```
|
|
298
279
|
|
|
299
|
-
|
|
300
|
-
routes: {"home":{"uri":"\/","methods":["GET","HEAD"],"domain":null},"login":{"uri":"login","methods":["GET","HEAD"],"domain":null}},
|
|
301
|
-
url: 'http://ziggy.test',
|
|
302
|
-
port: false
|
|
303
|
-
};
|
|
280
|
+
If you don't have Ziggy's NPM package installed, add the following to your `jsconfig.json` or `tsconfig.json` to load Ziggy's types from your vendor directory:
|
|
304
281
|
|
|
305
|
-
|
|
282
|
+
```json
|
|
283
|
+
{
|
|
284
|
+
"compilerOptions": {
|
|
285
|
+
"paths": {
|
|
286
|
+
"ziggy-js": ["./vendor/tightenco/ziggy"]
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
}
|
|
306
290
|
```
|
|
307
291
|
|
|
308
|
-
|
|
292
|
+
## JavaScript frameworks
|
|
293
|
+
|
|
294
|
+
> [!NOTE]
|
|
295
|
+
> Many applications don't need the additional setup described here—the `@routes` Blade directive makes Ziggy's `route()` function and config available globally, including within bundled JavaScript files.
|
|
296
|
+
|
|
297
|
+
If you are not using the `@routes` Blade directive, you can import Ziggy's `route()` function and configuration directly into JavaScript/TypeScript files.
|
|
298
|
+
|
|
299
|
+
### Generating and importing Ziggy's configuration
|
|
300
|
+
|
|
301
|
+
Ziggy provides an Artisan command to output its config and routes to a file:
|
|
302
|
+
|
|
303
|
+
```bash
|
|
304
|
+
php artisan ziggy:generate
|
|
305
|
+
```
|
|
306
|
+
|
|
307
|
+
This command places your configuration in `resources/js/ziggy.js` by default, but you can customize this path by passing an argument to the Artisan command or setting the `ziggy.output.path` config value.
|
|
308
|
+
|
|
309
|
+
The file `ziggy:generate` creates looks something like this:
|
|
309
310
|
|
|
310
311
|
```js
|
|
311
|
-
//
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
312
|
+
// resources/js/ziggy.js
|
|
313
|
+
|
|
314
|
+
const Ziggy = {
|
|
315
|
+
url: 'https://ziggy.test',
|
|
316
|
+
port: null,
|
|
317
|
+
routes: {
|
|
318
|
+
home: {
|
|
319
|
+
uri: '/',
|
|
320
|
+
methods: [ 'GET', 'HEAD'],
|
|
321
|
+
domain: null,
|
|
322
|
+
},
|
|
323
|
+
login: {
|
|
324
|
+
uri: 'login',
|
|
325
|
+
methods: ['GET', 'HEAD'],
|
|
326
|
+
domain: null,
|
|
317
327
|
},
|
|
318
328
|
},
|
|
319
|
-
}
|
|
329
|
+
};
|
|
330
|
+
|
|
331
|
+
export { Ziggy };
|
|
320
332
|
```
|
|
321
333
|
|
|
334
|
+
### Importing the `route()` function
|
|
335
|
+
|
|
336
|
+
You can import Ziggy like any other JavaScript library. Without the `@routes` Blade directive Ziggy's config is not available globally, so it must be passed to the `route()` function manually:
|
|
337
|
+
|
|
322
338
|
```js
|
|
323
|
-
|
|
339
|
+
import route from '../../vendor/tightenco/ziggy/dist';
|
|
340
|
+
import { Ziggy } from './ziggy.js';
|
|
324
341
|
|
|
325
|
-
|
|
326
|
-
|
|
342
|
+
route('home', undefined, undefined, Ziggy);
|
|
343
|
+
```
|
|
327
344
|
|
|
328
|
-
|
|
329
|
-
ziggy: path.resolve('vendor/tightenco/ziggy/dist'),
|
|
330
|
-
// 'vendor/tightenco/ziggy/dist/vue' if using the Vue plugin
|
|
331
|
-
});
|
|
345
|
+
To simplify importing the `route()` function, you can create an alias to the vendor path:
|
|
332
346
|
|
|
333
|
-
|
|
334
|
-
|
|
347
|
+
```js
|
|
348
|
+
// vite.config.js
|
|
335
349
|
|
|
336
|
-
|
|
350
|
+
export default defineConfig({
|
|
337
351
|
resolve: {
|
|
338
352
|
alias: {
|
|
339
|
-
ziggy:
|
|
353
|
+
'ziggy-js': 'vendor/tightenco/ziggy/dist',
|
|
354
|
+
// 'vendor/tightenco/ziggy/dist/vue.es' if using the Vue plugin
|
|
340
355
|
},
|
|
341
356
|
},
|
|
342
357
|
});
|
|
343
358
|
```
|
|
344
359
|
|
|
345
|
-
|
|
360
|
+
Now your imports can be shortened to:
|
|
346
361
|
|
|
347
362
|
```js
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
import route from 'ziggy';
|
|
351
|
-
import { Ziggy } from './ziggy';
|
|
352
|
-
|
|
353
|
-
// ...
|
|
354
|
-
|
|
355
|
-
route('home', undefined, undefined, Ziggy);
|
|
363
|
+
import route from 'ziggy-js';
|
|
356
364
|
```
|
|
357
365
|
|
|
358
|
-
|
|
366
|
+
### Vue
|
|
359
367
|
|
|
360
368
|
Ziggy includes a Vue plugin to make it easy to use the `route()` helper throughout your Vue app:
|
|
361
369
|
|
|
362
370
|
```js
|
|
363
371
|
import { createApp } from 'vue';
|
|
364
|
-
import { ZiggyVue } from 'ziggy';
|
|
365
|
-
import
|
|
366
|
-
import App from './App';
|
|
372
|
+
import { ZiggyVue } from 'ziggy-js';
|
|
373
|
+
import App from './App.vue';
|
|
367
374
|
|
|
368
|
-
createApp(App).use(ZiggyVue
|
|
375
|
+
createApp(App).use(ZiggyVue);
|
|
376
|
+
```
|
|
369
377
|
|
|
370
|
-
|
|
371
|
-
import Vue from 'vue'
|
|
372
|
-
import { ZiggyVue } from 'ziggy';
|
|
373
|
-
import { Ziggy } from './ziggy';
|
|
378
|
+
Now you can use the `route()` function anywhere in your Vue components and templates:
|
|
374
379
|
|
|
375
|
-
|
|
380
|
+
```vue
|
|
381
|
+
<a class="nav-link" :href="route('home')">Home</a>
|
|
376
382
|
```
|
|
377
383
|
|
|
378
|
-
If you
|
|
379
|
-
|
|
380
|
-
> Note: If you use the `@routes` Blade directive in your views, Ziggy's configuration will already be available globally, so you don't need to import the `Ziggy` config object and pass it into `use()`.
|
|
384
|
+
If you are not using the `@routes` Blade directive, import Ziggy's configuration too and pass it to `.use()`:
|
|
381
385
|
|
|
382
|
-
|
|
386
|
+
```js
|
|
387
|
+
import { createApp } from 'vue';
|
|
388
|
+
import { ZiggyVue } from 'ziggy-js';
|
|
389
|
+
import { Ziggy } from './ziggy.js';
|
|
390
|
+
import App from './App.vue';
|
|
383
391
|
|
|
384
|
-
|
|
385
|
-
<a class="nav-link" :href="route('home')">Home</a>
|
|
392
|
+
createApp(App).use(ZiggyVue, Ziggy);
|
|
386
393
|
```
|
|
387
394
|
|
|
388
|
-
|
|
395
|
+
If you use the Vue plugin with the `ziggy-js` import alias shown above, make sure to update the alias to `vendor/tightenco/ziggy/dist/vue.es`.
|
|
396
|
+
|
|
397
|
+
### React
|
|
389
398
|
|
|
390
399
|
Ziggy includes a `useRoute()` hook to make it easy to use the `route()` helper in your React app:
|
|
391
400
|
|
|
392
401
|
```jsx
|
|
393
|
-
// PostsLink.js
|
|
394
402
|
import React from 'react';
|
|
395
403
|
import { useRoute } from 'ziggy-js';
|
|
396
|
-
import { Ziggy } from './ziggy';
|
|
397
404
|
|
|
398
405
|
export default function PostsLink() {
|
|
399
|
-
const route = useRoute(
|
|
406
|
+
const route = useRoute();
|
|
400
407
|
|
|
401
408
|
return <a href={route('posts.index')}>Posts</a>;
|
|
402
409
|
}
|
|
403
410
|
```
|
|
404
411
|
|
|
405
|
-
If you
|
|
406
|
-
|
|
407
|
-
```js
|
|
408
|
-
// app.js
|
|
409
|
-
import { Ziggy } from './ziggy';
|
|
410
|
-
globalThis.Ziggy = Ziggy;
|
|
411
|
-
```
|
|
412
|
+
If you are not using the `@routes` Blade directive, import Ziggy's configuration too and pass it to `useRoute()`:
|
|
412
413
|
|
|
413
414
|
```jsx
|
|
414
|
-
// PostsLink.js
|
|
415
415
|
import React from 'react';
|
|
416
416
|
import { useRoute } from 'ziggy-js';
|
|
417
|
+
import { Ziggy } from './ziggy.js';
|
|
417
418
|
|
|
418
419
|
export default function PostsLink() {
|
|
419
|
-
const route = useRoute();
|
|
420
|
+
const route = useRoute(Ziggy);
|
|
420
421
|
|
|
421
422
|
return <a href={route('posts.index')}>Posts</a>;
|
|
422
423
|
}
|
|
423
424
|
```
|
|
424
425
|
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
#### SPAs or separate repos
|
|
428
|
-
|
|
429
|
-
Ziggy's `route()` helper function is also available as an NPM package, for use in JavaScript projects managed separately from their Laravel backend (i.e. without Composer or a `vendor` directory). You can install the NPM package with `npm install ziggy-js`.
|
|
430
|
-
|
|
431
|
-
To make your routes available on the frontend for this function to use, you can either run `php artisan ziggy:generate` and add the generated routes file to your project, or you can return Ziggy's config as JSON from an endpoint in your Laravel API (see [Retrieving Ziggy's routes and config from an API endpoint](#retrieving-ziggys-routes-and-config-from-an-api-endpoint) below for an example of how to set this up).
|
|
432
|
-
|
|
433
|
-
Then, import and use Ziggy as above:
|
|
426
|
+
You can also make the `Ziggy` config object available globally, so you can call `useRoute()` without passing Ziggy's configuration to it every time:
|
|
434
427
|
|
|
435
428
|
```js
|
|
436
429
|
// app.js
|
|
430
|
+
import { Ziggy } from './ziggy.js';
|
|
431
|
+
globalThis.Ziggy = Ziggy;
|
|
432
|
+
```
|
|
437
433
|
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
import { Ziggy } from './ziggy';
|
|
441
|
-
// or...
|
|
442
|
-
const response = await fetch('/api/ziggy');
|
|
443
|
-
const Ziggy = await response.json();
|
|
434
|
+
### SPAs or separate repos
|
|
444
435
|
|
|
445
|
-
|
|
436
|
+
Ziggy's `route()` function is available as an NPM package, for use in JavaScript projects managed separately from their Laravel backend (i.e. without Composer or a `vendor` directory). You can install the NPM package with `npm install ziggy-js`.
|
|
446
437
|
|
|
447
|
-
|
|
448
|
-
```
|
|
438
|
+
To make your routes available on the frontend for this function to use, you can either run `php artisan ziggy:generate` and add the generated config file to your frontend project, or you can return Ziggy's config as JSON from an endpoint in your Laravel API (see [Retrieving Ziggy's config from an API endpoint](#retrieving-ziggys-routes-from-an-api-endpoint) below for an example of how to set this up).
|
|
449
439
|
|
|
450
440
|
## Filtering Routes
|
|
451
441
|
|
|
452
|
-
Ziggy supports filtering the routes it
|
|
442
|
+
Ziggy supports filtering the list of routes it outputs, which is useful if you have certain routes that you don't want to be included and visible in your HTML source.
|
|
443
|
+
|
|
444
|
+
> [!IMPORTANT]
|
|
445
|
+
> Hiding routes from Ziggy's output is not a replacement for thorough authentication and authorization. Routes that should not be accessibly publicly should be protected by authentication whether they're filtered out of Ziggy's output or not.
|
|
453
446
|
|
|
454
|
-
|
|
447
|
+
### Including/excluding routes
|
|
455
448
|
|
|
456
|
-
To set up
|
|
449
|
+
To set up route filtering, create a config file in your Laravel app at `config/ziggy.php` and add **either** an `only` or `except` key containing an array of route name patterns.
|
|
457
450
|
|
|
458
451
|
> Note: You have to choose one or the other. Setting both `only` and `except` will disable filtering altogether and return all named routes.
|
|
459
452
|
|
|
@@ -465,7 +458,7 @@ return [
|
|
|
465
458
|
];
|
|
466
459
|
```
|
|
467
460
|
|
|
468
|
-
You can
|
|
461
|
+
You can use asterisks as wildcards in route filters. In the example below, `admin.*` will exclude routes named `admin.login`, `admin.register`, etc.:
|
|
469
462
|
|
|
470
463
|
```php
|
|
471
464
|
// config/ziggy.php
|
|
@@ -475,7 +468,7 @@ return [
|
|
|
475
468
|
];
|
|
476
469
|
```
|
|
477
470
|
|
|
478
|
-
|
|
471
|
+
### Filtering with groups
|
|
479
472
|
|
|
480
473
|
You can also define groups of routes that you want make available in different places in your app, using a `groups` key in your config file:
|
|
481
474
|
|
|
@@ -510,39 +503,27 @@ To expose multiple groups you can pass an array of group names:
|
|
|
510
503
|
|
|
511
504
|
## Other
|
|
512
505
|
|
|
513
|
-
|
|
506
|
+
### TLS/SSL termination and trusted proxies
|
|
514
507
|
|
|
515
508
|
<!-- Or: What to do if your app is served over `https` but Ziggy's `route()` helper generates `http` URLs -->
|
|
516
509
|
|
|
517
|
-
If your application is using [TLS/SSL termination](https://en.wikipedia.org/wiki/TLS_termination_proxy) or is behind a load balancer or proxy, or if it's hosted on a service that is, Ziggy may generate URLs with a scheme of `http` instead of `https`, even if your app URL uses `https`. To
|
|
510
|
+
If your application is using [TLS/SSL termination](https://en.wikipedia.org/wiki/TLS_termination_proxy) or is behind a load balancer or proxy, or if it's hosted on a service that is, Ziggy may generate URLs with a scheme of `http` instead of `https`, even if your app URL uses `https`. To fix this, set up your Laravel app's trusted proxies according to the documentation on [Configuring Trusted Proxies](https://laravel.com/docs/requests#configuring-trusted-proxies).
|
|
518
511
|
|
|
519
|
-
|
|
512
|
+
### Using `@routes` with a Content Security Policy
|
|
520
513
|
|
|
521
|
-
A [Content Security Policy](https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP) (CSP) may block inline scripts, including those output by Ziggy's `@routes` Blade directive. If you have a CSP and are using a nonce to flag safe inline scripts, you can pass the nonce
|
|
514
|
+
A [Content Security Policy](https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP) (CSP) may block inline scripts, including those output by Ziggy's `@routes` Blade directive. If you have a CSP and are using a nonce to flag safe inline scripts, you can pass the nonce to the `@routes` directive and it will be added to Ziggy's script tag:
|
|
522
515
|
|
|
523
516
|
```php
|
|
524
|
-
// PHP ^8.0
|
|
525
517
|
@routes(nonce: 'your-nonce-here')
|
|
526
|
-
|
|
527
|
-
// PHP <=7.4
|
|
528
|
-
@routes(null, 'your-nonce-here')
|
|
529
518
|
```
|
|
530
519
|
|
|
531
|
-
|
|
520
|
+
### Disabling the `route()` helper
|
|
532
521
|
|
|
533
|
-
If you only want to use the `@routes` directive to make
|
|
522
|
+
If you only want to use the `@routes` directive to make Ziggy's configuration available in JavaScript, but don't need the `route()` helper function, set the `ziggy.skip-route-function` config to `true`.
|
|
534
523
|
|
|
535
|
-
|
|
536
|
-
// config/ziggy.php
|
|
524
|
+
### Retrieving Ziggy's config from an API endpoint
|
|
537
525
|
|
|
538
|
-
|
|
539
|
-
'skip-route-function' => true,
|
|
540
|
-
];
|
|
541
|
-
```
|
|
542
|
-
|
|
543
|
-
#### Retrieving Ziggy's routes and config from an API endpoint
|
|
544
|
-
|
|
545
|
-
Ziggy can easily return its config object as JSON from an endpoint in your Laravel app. For example, you could set up an `/api/ziggy` route that looks something like this:
|
|
526
|
+
If you need to retrieve Ziggy's config from your Laravel backend over the network, you can create a route that looks something like this:
|
|
546
527
|
|
|
547
528
|
```php
|
|
548
529
|
// routes/api.php
|
|
@@ -552,27 +533,12 @@ use Tightenco\Ziggy\Ziggy;
|
|
|
552
533
|
Route::get('api/ziggy', fn () => response()->json(new Ziggy));
|
|
553
534
|
```
|
|
554
535
|
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
```js
|
|
558
|
-
// app.js
|
|
559
|
-
|
|
560
|
-
import route from 'ziggy-js';
|
|
561
|
-
|
|
562
|
-
const response = await fetch('/api/ziggy');
|
|
563
|
-
const Ziggy = await response.toJson();
|
|
564
|
-
|
|
565
|
-
// ...
|
|
566
|
-
|
|
567
|
-
route('home', undefined, undefined, Ziggy);
|
|
568
|
-
```
|
|
569
|
-
|
|
570
|
-
#### Re-generating the routes file when your app routes change
|
|
536
|
+
### Re-generating the routes file when your app routes change
|
|
571
537
|
|
|
572
|
-
If you
|
|
538
|
+
If you are generating your Ziggy config as a file by running `php artisan ziggy:generate`, you may want to re-run that command when your app's route files change. The example below is a Laravel Mix plugin, but similar functionality could be achieved without Mix. Huge thanks to [Nuno Rodrigues](https://github.com/nacr) for [the idea and a sample implementation](https://github.com/tighten/ziggy/issues/321#issuecomment-689150082). See [#655 for a Vite example](https://github.com/tighten/ziggy/pull/655/files#diff-4aeb78f813e14842fcf95bdace9ced23b8a6eed60b23c165eaa52e8db2f97b61).
|
|
573
539
|
|
|
574
540
|
<details>
|
|
575
|
-
<summary>
|
|
541
|
+
<summary>Laravel Mix plugin example</summary>
|
|
576
542
|
<p></p>
|
|
577
543
|
|
|
578
544
|
```js
|
|
@@ -632,4 +598,4 @@ Please review our [security policy](../../security/policy) on how to report secu
|
|
|
632
598
|
|
|
633
599
|
## License
|
|
634
600
|
|
|
635
|
-
Ziggy is open
|
|
601
|
+
Ziggy is open-source software released under the MIT license. See [LICENSE](LICENSE) for more information.
|