laravel-localization icon indicating copy to clipboard operation
laravel-localization copied to clipboard

Handle field name in route parameter

Open blackjak231 opened this issue 1 year ago • 2 comments

Right now, the current localization system does not handle using field names in route parameters.

Example

Route definitions

french: /ville/{city:slug} --> default language english: /city/{city:slug}

Using LaravelLocalization::getLocalizedURL() to get the english url, it will return /en/ville/{city:slug}.

After checking, it seems that when getting the attributes it ignores :slug and uses the key city and then tries to find {city} in the translated url to return it.

I wish to not use the route binding on my model as the id and other attributes are used in other places.

Thanks in advance !

blackjak231 avatar Jul 22 '22 16:07 blackjak231

Same issue

kamranata avatar Jul 30 '22 20:07 kamranata

Is this fixed?

I switched my routes from /page/{model} to /page/{model:slug} and /some-other-page/{model:id} because in some routes I need to fetch same model via slug and in some via ID and package stopped to translate my routes.

Before routes switch I used this fix (which works for my specific use case where all routes that require slug starts with pages.), this isn't long-term solution and it would be good to fix this small bug:

public function getRouteKeyName()
{
    return request()->route() && str_starts_with(request()->route()->getName(), 'pages.') ? 'slug' : 'id';
}

kskrlinnorth2 avatar Sep 07 '22 15:09 kskrlinnorth2