Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when Route not defined for this locale #63

Open
domihagen opened this issue Dec 14, 2021 · 4 comments
Open

Error when Route not defined for this locale #63

domihagen opened this issue Dec 14, 2021 · 4 comments
Assignees
Labels
bug Something isn't working enhancement New feature or request

Comments

@domihagen
Copy link

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) : '';

@domihagen domihagen added the bug Something isn't working label Dec 14, 2021
@chinleung
Copy link
Owner

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
Copy link
Author

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
Copy link
Owner

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
Copy link
Author

This sounds good to me 👍

@chinleung chinleung self-assigned this Dec 16, 2021
@chinleung chinleung added the enhancement New feature or request label Dec 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants