Laravel locale route get



正常使用Route::get('{locale}/home', [MachineController::class, "home"]);

但是我想使用$locale变量来设置语言

Route::get('{locale}/home', function ($locale) {
App::setLocale(getLocale($locale));
[MachineController::class, "home"];
}); 

但是返回一个空页面,最有效的方法是什么?

你写的代码有问题。

MVC结构(Wikipedia)定义了你必须从MachineController类(Laravel Routing)的home方法中编写App::setLocale(getLocale($locale));

或者你可以这样定义你的路由:

Route::get('{locale}/home', function ($locale) {
App::setLocale(getLocale($locale));
(new MachineController())->home();
});

您的代码问题尝试这样做:

Route::get('{locale}/home', function ($locale) {
$current=getLocale($locale);
App::setLocale($current);
(new MachineController())->home();
});

最新更新