Closed jeffreyvr closed 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
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 :)
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 aViewException
. 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.