Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trim slashes between origin and optional first param #637

Merged
merged 3 commits into from
May 5, 2023
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
18 changes: 14 additions & 4 deletions src/js/Route.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,23 @@ export default class Route {
* @return {String} Route template.
*/
get template() {
return `${this.origin}/${this.definition.uri}`.replace(/\/+$/, '');
}

/**
* Get a template of the origin for this route.
*
* @example
* https://{team}.ziggy.dev/
*
* @return {String} Route origin template.
*/
get origin() {
// If we're building just a path there's no origin, otherwise: if this route has a
// domain configured we construct the origin with that, if not we use the app URL
const origin = !this.config.absolute ? '' : this.definition.domain
return !this.config.absolute ? '' : this.definition.domain
? `${this.config.url.match(/^\w+:\/\//)[0]}${this.definition.domain}${this.config.port ? `:${this.config.port}` : ''}`
: this.config.url;

return `${origin}/${this.definition.uri}`.replace(/\/+$/, '');
}

/**
Expand Down Expand Up @@ -101,6 +111,6 @@ export default class Route {
}

return encodeURIComponent(params[segment] ?? '');
}).replace(/\/+$/, '');
}).replace(`${this.origin}//`, `${this.origin}/`).replace(/\/+$/, '');
}
}
15 changes: 15 additions & 0 deletions tests/Unit/ZiggyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -612,4 +612,19 @@ public function can_cache_compiled_route_list_internally_on_repeated_instantiati

$this->assertSame($routes, (new Ziggy)->toArray()['routes']);
}

/** @test */
public function optional_params_inside_path()
{
app('router')->get('{country?}/test/{language?}/products/{id}', $this->noop())->name('products.show');
app('router')->getRoutes()->refreshNameLookups();

$this->assertSame('http://ziggy.dev/ca/test/fr/products/1', route('products.show', ['country' => 'ca', 'language' => 'fr', 'id' => 1]));
// Optional param in the middle of a path
$this->assertSame('http://ziggy.dev/ca/test//products/1', route('products.show', ['country' => 'ca', 'id' => 1]));
// Optional param at the beginning of a path
$this->assertSame('http://ziggy.dev/test/fr/products/1', route('products.show', ['language' => 'fr', 'id' => 1]));
// Both
$this->assertSame('http://ziggy.dev/test//products/1', route('products.show', ['id' => 1]));
}
}
9 changes: 9 additions & 0 deletions tests/js/route.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,15 @@ describe('route()', () => {
same(route('pages.optional', null), 'https://ziggy.dev/optionalpage');
});

test('missing optional parameter in first path segment', () => {
same(route('products.show', { country: 'ca', language: 'fr', id: 1 }), 'https://ziggy.dev/ca/fr/products/1');
// These URLs aren't valid but this matches the behavior of Laravel's PHP `route()` helper
same(route('products.show', { country: 'ca', id: 1 }), 'https://ziggy.dev/ca//products/1');
same(route('products.show', { id: 1 }), 'https://ziggy.dev//products/1');
// First param is handled correctly
same(route('products.show', { language: 'fr', id: 1 }), 'https://ziggy.dev/fr/products/1');
});

test('can error if a route name doesn’t exist', () => {
throws(() => route('unknown-route'), /Ziggy error: route 'unknown-route' is not in the route list\./);
});
Expand Down