Support getRouteKey method from Laravel
#818
Replies: 1 comment 1 reply
-
|
This would be really cool! I'm almost positive it's not achievable automatically but I'd love to be wrong 😅 We can't really "use" That being said, it should be pretty straightforward for you to implement in your own project since you know what that template is already. You could wrap Ziggy's route helper and inspect the route name or params to generate the correct key on those routes, or, since Ziggy doesn't really care how many params there are or how they're formatted, you may be able to get away with just overriding the route definition in Ziggy's config, something like this: @routes
<script>
Ziggy.config.routes['teams.show'] = {
uri: 'teams/{slug}--{uid}',
methods: ['GET', 'HEAD'],
parameters: ['slug', 'uid'],
bindings: {
slug: 'slug',
uid: 'uid',
};
</script> |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Description
In #315 you implemented the support for
getRouteKeyNamealready. I am build an app currently that uses "self healing" URLs. (Aaron Francis made a good video about this) These have to usegetRouteKeyinstead ofgetRouteKeyNameas they're a bit more complex.So what do I want to achieve? My Model has a
slugand auidthat should make up the URL.Example:
https://example.com/team/super-team--VCT8MTTo achieve this in Laravel the code usually would look like this:
That's enough to build the URL with the
routemethod from Laravel.To Resolve the method we have to do a bit more, but it's quite easy to understand.
The problem is, the URL Ziggy generates in this case is
https://example.com/team/VCT8MTand nothttps://example.com/team/super-team--VCT8MT, asgetRouteKeyis currently not evaluated.So instead of just
$uid = last(explode('--', $value));I have to write the following currently:This way the URLs still work, but I have to redirect every call as the URLs from Ziggy don't have the expected format.
Suggestion
I think Ziggy should be able to evaluate
getRouteKeythe same way it evaluatesgetRouteKeyName. Or let's say it in other words: The same way Laravel evaluates the route definition.Important to note is, that these two get used together in general. You still define the route key name but overwrite the actual path structure
Alternatives
I don't really see an alternative. As I described, the workaround is currently to generate the incomplete URLs and redirect to the expected one.
Beta Was this translation helpful? Give feedback.
All reactions