Skip to content
Open
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
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,29 @@ route().params; // { venue: '1', event: '2', hosts: 'all' }

> Note: parameter values retrieved with `route().params` will always be returned as strings.

#### Match url to get route name and params

```js
// Laravel route called 'post.update' with URI '/post/{post}'
// axios response.request.responseURL is https://myapp.com/post/1

route().match('/myapp.com/post/1');
/* result:
* {
* name: 'posts.update',
* params: {post: '1'},
* query: {},
* route: {
* uri: 'posts/{post}',
* methods: ['PUT'],
* bindings: {
* post: 'id',
* },
* },
* }
*/
```

### Route-model binding

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.
Expand Down
2 changes: 1 addition & 1 deletion dist/index.esm.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/route.umd.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/js/Route.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ export default class Route {
* @param {String} url - URL to check.
* @return {Object|false} - If this route matches, returns the matched parameters.
*/
matchesUrl(url) {
if (!this.definition.methods.includes('GET')) return false;
matchesUrl(url, method = 'GET') {
if (!this.definition.methods.includes(method.toUpperCase())) return false;

// Transform the route's template into a regex that will match a hydrated URL,
// by replacing its parameter segments with matchers for parameter values
Expand Down
12 changes: 6 additions & 6 deletions src/js/Router.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export default class Router extends String {
* @param {String} [url] - The URL to inspect, defaults to the current window URL.
* @return {{ name: string, params: Object, query: Object, route: Route }}
*/
_unresolve(url) {
match(url, method='GET') {
if (!url) {
url = this._currentUrl();
} else if (this._config.absolute && url.startsWith('/')) {
Expand All @@ -77,7 +77,7 @@ export default class Router extends String {
let matchedParams = {};
const [name, route] = Object.entries(this._config.routes).find(
([name, route]) =>
(matchedParams = new Route(name, route, this._config).matchesUrl(url)),
(matchedParams = new Route(name, route, this._config).matchesUrl(url, method)),
) || [undefined, undefined];

return { name, ...matchedParams, route };
Expand Down Expand Up @@ -112,7 +112,7 @@ export default class Router extends String {
* @return {(Boolean|String|undefined)}
*/
current(name, params) {
const { name: current, params: currentParams, query, route } = this._unresolve();
const { name: current, params: currentParams, query, route } = this.match();

// If a name wasn't passed, return the name of the current route
if (!name) return current;
Expand Down Expand Up @@ -191,17 +191,17 @@ export default class Router extends String {
* @return {Object}
*/
get params() {
const { params, query } = this._unresolve();
const { params, query } = this.match();

return { ...params, ...query };
}

get routeParams() {
return this._unresolve().params;
return this.match().params;
}

get queryParams() {
return this._unresolve().query;
return this.match().query;
}

/**
Expand Down
39 changes: 39 additions & 0 deletions tests/js/route.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1503,3 +1503,42 @@ describe('current()', () => {
global.window = oldWindow;
});
});

describe('match()', () => {
test('can get routes PUT requests', () => {
expect(route().match('ziggy.dev/posts/1', 'PUT')).toEqual({
name: 'posts.update',
params: {post: '1'},
query: {},
route: {
uri: 'posts/{post}',
methods: ['PUT'],
bindings: {
post: 'id',
},
},
});
});

test('can ignore routes that don’t allow PUT requests', () => {
expect(route().match('ziggy.dev/hosting-contacts', 'PUT')).toEqual({
name: undefined,
route: undefined
});
});

test('can get routes PUT requests by lower case', () => {
expect(route().match('ziggy.dev/posts/1', 'put')).toEqual({
name: 'posts.update',
params: {post: '1'},
query: {},
route: {
uri: 'posts/{post}',
methods: ['PUT'],
bindings: {
post: 'id',
},
},
});
});
});