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

Redirect Issue with Octane #102

Closed UtkuDalmaz closed 1 year ago

UtkuDalmaz commented 1 year ago

I use Laravel Octane

When I put

Route::fallback(\CodeZero\LocalizedRoutes\Controller\FallbackController::class) ->middleware(\CodeZero\LocalizedRoutes\Middleware\SetLocale::class);

to web.php

I get Invalid route action: [App\Http\Controllers\CodeZero\LocalizedRoutes\Controllers\FallbackController].

Whe I put

Route::fallback([\CodeZero\LocalizedRoutes\Controller\FallbackController::class, '__invoke']) ->middleware(\CodeZero\LocalizedRoutes\Middleware\SetLocale::class);

to web.php

I get Target class [CodeZero\LocalizedRoutes\Controller\FallbackController] does not exist.

Any idea?

ivanvermeyen commented 1 year ago

Hello,

I don't know why the invoke method isn't used automatically, I've seen this come up before.

Your second example with the array brackets is missing an S in Controllers in the namespace. Will it work like that?

EDIT:

In your first example, it seems that a default namespace is applied.

Can't remember if you can override this just by importing the controller at the top:

use \CodeZero\LocalizedRoutes\Controllers\FallbackController;

Route::fallback(FallbackController::class)
->middleware(\CodeZero\LocalizedRoutes\Middleware\SetLocale::class);
UtkuDalmaz commented 1 year ago

Adding the missing S fixed the issue. Thanks

By the way, I have a question.

How can I configure it to flow like this A user enters the website, e.g., example.com If the user's browser's language is set to English, he is redirected to /en. If French, he is redirected to /fr

ivanvermeyen commented 1 year ago

The middleware will look for a locale in the browser on a first visit (unless there is a valid locale in the URL). You already added the FallbackController, so you only need to set the redirect_to_localized_urls config option to true to auto redirect to the found locale.

More info: https://github.com/codezero-be/laravel-localized-routes#detectors https://github.com/codezero-be/laravel-localized-routes#-automatically-redirect-to-localized-urls

If you need more control, you can just use app()->getLocale() in a route action and redirect manually based on that, or basically do anything you need with it..