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

Ziggy generates URL with a double leading slash #751

Closed
tmannherz opened this issue May 6, 2024 · 6 comments · Fixed by #754
Closed

Ziggy generates URL with a double leading slash #751

tmannherz opened this issue May 6, 2024 · 6 comments · Fixed by #754
Assignees
Labels

Comments

@tmannherz
Copy link

Ziggy version

v2.1.0

Laravel version

v10.48.4

Description

We have a route whose sole parameter comes from the DB:

Route::get('{catalog_uri}', fn() => view('pdp'))
    ->where('catalog_uri', '^/p/.+$')
    ->name('catalog.pdp');

It's admittedly non-standard as you would usually move /p/ as part of the route, but this prefix is part of the data provided by the catalog.

In Laravel, creating a URL to this route works as you would expect:

route('catalog.pdp', ['catalog_uri' => '/p/test']); // http://example.com/p/test

Yet if generated by Ziggy, a URL with a double-slash is generated:

route('catalog.pdp', { catalog_uri: '/p/test' }); // http://example.com//p/test

Ziggy call and context

We use Ziggy in Vue, so its route method is called like:

<a
    :href="
        route('catalog.pdp', {
            catalog_uri: listing.catalog_uri,
        })
    "
    class="btn-black-small px-6 py-2 text-sm"
    >Link to catalog</a
>

Ziggy configuration

{
    "url": "https:\/\/example.com",
    "port": null,
    "defaults": {},
    "routes": {
        "catalog.pdp": {
            "uri": "{catalog_uri}",
            "methods": ["GET", "HEAD"],
            "wheres": {
                "catalog_uri": "^\/p\/.+$"
            },
            "domain": "example.com",
            "parameters": ["catalog_uri"]
        }
    }
}

Route definition

Route::get('{catalog_uri}', fn() => view('pdp'))
    ->where('catalog_uri', '^/p/.+$')
    ->name('catalog.pdp');
@bakerkretzmar bakerkretzmar self-assigned this May 6, 2024
@bakerkretzmar
Copy link
Collaborator

What's your actual route path? Is it at the root of your app's domain, so the entire URI is literally {catalog_uri}?

I'm adding tests for this to Ziggy and testing it in a real app too and I actually can't reproduce your exact examples. Laravel and Ziggy are behaving the same way for me, but they both behave differently if the route parameter is right at the root of the domain:

Route::get('{catalog_uri}', fn() => '')->where('catalog_uri', '^/p/.+$')->name('catalog1');

Route::get('catalog/{catalog_uri}', fn() => '')->where('catalog_uri', '^/p/.+$')->name('catalog2');

Both Laravel and Ziggy produce the same output for these:

route('catalog1', '/p/test'): http://example.com/p/test

route('catalog2', '/p/test'): http://example.com/catalog//p/test

@tmannherz
Copy link
Author

@bakerkretzmar The catalog routes are bound to a different domain, which is why these routes existing at all, so we can generate them correctly with the alternate domain.

Route::domain('catalog.example.com')->group(function () {
    Route::get('{catalog_uri}', fn() => view('pdp'))
        ->where('catalog_uri', '^/p/.+$')
        ->name('pdp');
});

Does that answer your question?

@bakerkretzmar
Copy link
Collaborator

Yeah it does, thanks. I can't reproduce this at all then. When I add a route with the exact configuration you shared to Ziggy's tests or to one of my own apps, Ziggy's route('catalog.pdp', { catalog_uri: '/p/test' }) returns http://example.com/p/test as expected. Tried on Laravel 10 and 11. Can you reproduce it in another app or a fresh install?

@tmannherz
Copy link
Author

@bakerkretzmar apologies...in my attempt to simply this for the ticket I actually removed the component that causes the issue. You're right that the scenario above works as expected.
I'm able to reproduce it with this slightly different route setup:

// catalog.php
Route::domain('{storefront}.example.com')->name('test.')->group(function () {
    Route::get('{catalog_uri}', fn() => view('pdp'))
        ->where('catalog_uri', '^/p/.+$')
        ->name('pdp');
});

Calling it like this in Vue:

<pre>
test pdp: {{
    route('catalog.test.pdp', {
        storefront: 'me',
        catalog_uri: '/p/test',
    })
}}
</pre>

Yields test pdp: https://me.example.com//p/test

For context, we run a multi-tenant site with multiple storefronts, each with its own subdomain.

@bakerkretzmar
Copy link
Collaborator

Thanks, I can reproduce that. Will investigate!

@bakerkretzmar
Copy link
Collaborator

I'll tag another release for this shortly. Thanks for your help @tmannherz!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants