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
locale
required or omit it completely and use something else, like a Session variable or similar. – Tim Lewis Commented Mar 31 at 14:34/{lang}
in the URL; big, small, local and international本文标签: Optional locale in routes messes up controller arguments (Laravel 11)Stack Overflow