admin管理员组

文章数量:1355612

I have a project in laravel 11 and there are some issues with my config.

The site should support optional locale param to differentiate between default and custom language:

DE: 
EN: 

This is the code defined in routes:

Route::get('/auto/{id}/{slug}', [ItemController::class, 'show'])->name('items.show.default');

Route::group([
  'prefix' => '{locale}', 
  'middleware' => ['setLocale'],
  'where' => ['locale' => 'de|en',
  'as' => 'localized.'
], function () {
  Route::get('/auto/{id}/{slug}', [ItemController::class, 'show'])->name('items.show.default');
});

SetLocale middleware just sets the App and Session locale.

The controller looks like this:

public function show(Request $request, $id, $slug)
{
    dd('$id: '.$id);
    $item = Item::where('id', $id)->firstOrFail();
}

The issue is that $id in the controller method wrongly shows the value.

       $id: 1
    $id: en

Question is: Is there any laravel default function or recommended way to handle this challenge?

Thanks!

I have a project in laravel 11 and there are some issues with my config.

The site should support optional locale param to differentiate between default and custom language:

DE: https://example/auto/1/my-ferrari
EN: https://example/en/auto/1/my-ferrari

This is the code defined in routes:

Route::get('/auto/{id}/{slug}', [ItemController::class, 'show'])->name('items.show.default');

Route::group([
  'prefix' => '{locale}', 
  'middleware' => ['setLocale'],
  'where' => ['locale' => 'de|en',
  'as' => 'localized.'
], function () {
  Route::get('/auto/{id}/{slug}', [ItemController::class, 'show'])->name('items.show.default');
});

SetLocale middleware just sets the App and Session locale.

The controller looks like this:

public function show(Request $request, $id, $slug)
{
    dd('$id: '.$id);
    $item = Item::where('id', $id)->firstOrFail();
}

The issue is that $id in the controller method wrongly shows the value.

https://example/auto/1/my-ferrari       $id: 1
https://example/en/auto/1/my-ferrari    $id: en

Question is: Is there any laravel default function or recommended way to handle this challenge?

Thanks!

Share Improve this question asked Mar 31 at 10:11 BajloBajlo 1,4272 gold badges11 silver badges18 bronze badges 4
  • 1 That's how it's always worked; chained URL Params are not the right solution when one of them is optional, since (as you're seeing), omitting an optional one messes with the rest of them and causes them to be out of order. The solution is make locale required or omit it completely and use something else, like a Session variable or similar. – Tim Lewis Commented Mar 31 at 14:34
  • That is the solution for either an international (domain) or a small project. In my case, it is for a local and big brand where having example.de/de... is not an option. – Bajlo Commented Apr 1 at 19:47
  • Then omit it; detect the current language of the browser and store that, then allow the user to change their language manually. None of the localized websites I've worked on have used /{lang} in the URL; big, small, local and international

    本文标签: Optional locale in routes messes up controller arguments (Laravel 11)Stack Overflow