chinleung / laravel-multilingual-routes

A package to handle multilingual routes in your Laravel application.
https://github.com/chinleung/laravel-multilingual-routes-demo
MIT License
394 stars 26 forks source link

Suggestion to add fallback for current_route helper #36

Closed jeffreyvr closed 4 years ago

jeffreyvr commented 4 years ago

Thanks for the package, works well!

This is not so much an issue, but more a specific use case I ran into.

Only part of my app uses localized pages. In the footer of my site I have a language switcher dropdown. Within this dropdown I'm using the current_route helper.

Sometimes (it's rare, but it happens) a page is not localized and using the Route::multilingual() route registration.

When you then call current_route, this will result in a ViewException. I added a very simple helper to fallback on the current path of the request when that happens.

I'm not saying that this should be default behavior, since you might also think: why are you showing that dropdown in the first place. I still thought I'd share it.

/**
 * Try to get the current route.
 *
 * @param  string $locale The locale for the current route.
 * @return mixed
 */
function try_current_route($locale = null)
{
    try {
        return current_route($locale);
    } catch(\Exception $e) {
        return request()->path();
    }
}
chinleung commented 4 years ago

Thanks for the package, works well!

Thanks @jeffreyvr! 😄

I'm not saying that this should be default behaviour, since you might also think: why are you showing that dropdown in the first place.

Why is this the case though? What's the expected behaviour? 🧐

I could update the current_route helper to accept a second parameter, which will be the fallback route. So if the route does not exist in the locale or the current route doesn't have any route name, we could return the provided fallback. If no fallback has been provided, we could return the current route.

What do you think about that?

So for instance, you are on https://example.test/route:

// Returns the current route if the current route does not have a name or the route is not registered in the provided $locale
current_route($locale); // https://example.test/route

// Returns the fallback if the current route does not have a name or the route is not registered in the provided $locale
current_route($locale, route('fallback')); // https://example.test/fallback
jeffreyvr commented 4 years ago

Thank you for your response! I think that the second parameter for the callback is a nice additional feature.

To further illustrate my situation, I have this in my footer to switch locale (uses a JS-redirect which is not included in the snippet):

<select>
    <option value="{{ current_route('nl') }}">Nederlands</option>
    <option value="{{ current_route('en') }}">English</option>
</select>

Now once I visit a page for which the route does not exist in the locale, it will result in the exception being thrown. But with your suggested solution it would be prevented and fallback to the current route. So that would be nice :)

chinleung commented 4 years ago

@jeffreyvr The fallback option is now available as of v1.7.0 and v2.4.0. 🎉