chinleung / laravel-multilingual-routes

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

Error when Route not defined for this locale #63

Open domihagen opened 2 years ago

domihagen commented 2 years ago

Describe the bug

When defining an route that is only available in view locales it throws an Error when calling localized_route()

To Reproduce

Steps to reproduce the behavior:

  1. Define a Route Route::multilingual('test/', [TestController::class, 'show'])->name('myTest')->only(['en', 'nl']);
  2. Place localized_route('myTest') in a Blade-File
  3. Call the Page with some other locale e.g. fr
  4. See error Route [fr.myTest] not defined.

Expected behavior

No Error is thrown

Additional context

As an quick fix i copied the helper function into my own helper file and added following condition:

return Route::has("$locale.$name") ? route("$locale.$name", $parameters, $absolute) : '';

chinleung commented 2 years ago

Hey @domihagen,

Thanks for the bug report. I'm still thinking about the behaviour we'd like to have since it'd be the same case if you use the route helper on a non registered route.

For instance:

Route::get('test', TestController::class);

And then you would still get an error using Laravel's helper route('test'); since we forgot to name the route with ->name.

If we silence the error in this case, the error might go unnoticed, so I'm not sure that's what I want to do in this case. 🤔

Do you have a real life example of why your route does not exist in a given locale?

domihagen commented 2 years ago

Hey @chinleung,

It is clear that you get an error if you call a route that is not defined. But your package offers the possibility to have routes only in certain locales.

Maybe it makes sense to be able to pass a fallback, like in the current_route() helper?

We have those locales: EN (default), NL, FR, IT, ES Published locales: EN & NL

Real life example:

We have some routes (routes are defined for all locales) through which a slug is passed to the controller. The corresponding entry is output on the basis of the slug. If no entry exists in this language, a 404 is returned.

Now we have a static route (as in the example above) that contains an SPA. And as long as we do not have all locales live, this route should not be able to be called in the locales that are not publishes yet.

We also generate the "hreflang" meta tag, so Google knows in which locale this page is available. Thats why we added the ->only(['en', 'nl']) to the static route, to prevent generating this tag and Google from indexing this Page.

I hope I have explained it clearly. If not, let me know.

chinleung commented 2 years ago

Hey,

I hope I have explained it clearly. If not, let me know.

Yeah it makes sens to me. 😄

What do you think about having a config to have the helper do one of the following actions if the route doesn't exist:

  1. Throw an exception
  2. Returns null
  3. Returns the homepage of the locale

Or maybe adding a fallback to the helper like you've suggested.

domihagen commented 2 years ago

This sounds good to me 👍