codezero-be / laravel-localized-routes

⭐️ A convenient way to set up and use localized routes in a Laravel app.
MIT License
491 stars 45 forks source link

auto redirect #85

Closed cod3rshotout closed 1 year ago

cod3rshotout commented 1 year ago

Hi, this is just a question not an issue .. I'm not sure how to do this. Essentially I have this old url: https://example.com/hello-world, that now is in 404, is there a way that if the locale prefix is not specified then the url will go directly on https://example.com/en/hello-world?

Let me know, thanks in advance

ivanvermeyen commented 1 year ago

Hello,

To redirect any non-localized URL to its localized version, you can set the config option redirect_to_localized_urls to true, and register the following fallback route with the FallbackController at the end of your routes/web.php file.

Route::fallback(\CodeZero\LocalizedRoutes\Controller\FallbackController::class);

This route also allows you to localize your 404 page with a custom 404 view that you define in the config file.

cod3rshotout commented 1 year ago
FallbackController

Hi, thanks for the quick reply. I already did this, but for some reason the route isn't redirected

ivanvermeyen commented 1 year ago

Does the fallback route have the middleware applied? If not, it will use the app locale to generate a localized URL. And is the hello-world route registered with the locale slug?

Maybe it's a combination of settings... Can you compare with these tests?

https://github.com/codezero-be/laravel-localized-routes/blob/2.x/tests/Unit/RedirectToLocalizedTest.php

cod3rshotout commented 1 year ago

Does the fallback route have the middleware applied? If not, it will use the app locale to generate a localized URL. And is the hello-world route registered with the locale slug?

Maybe it's a combination of settings... Can you compare with these tests?

https://github.com/codezero-be/laravel-localized-routes/blob/2.x/tests/Unit/RedirectToLocalizedTest.php

Yes, the middleware was applied. Actuallly the SetLocale middleware is correctly invoked but if I print the $locale variable from handle method of the middleware I get null. Also for some reason no method of FallbackController are invoked.

ivanvermeyen commented 1 year ago

Did you fix it?