信息:我正在使用Larvel 6.1.0版,并且我希望我的网站支持不同的语言。因此,我创建了一个新的中间件,并希望给我的路由添加前缀,以便Laravel可以确定语言。也许有更好的方法,但是到目前为止,这是我所做的。
URL最终应该看起来像这样:
mywebsite.com/en/home,url / locale / home
用于设置语言环境的中间件
namespace App\Http\Middleware;
use Closure;
class SetLocale
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
app()->setLocale($request->segment(1));
return $next($request);
}
}
在kernel.php中注册了新的中间件setlocale
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
'setlocale' => \App\Http\Middleware\SetLocale::class,
];
我的web.php
Route::group(['middleware' => ['setlocale'],'prefix' => '{locale}', 'where' => ['locale' => '[a-zA-Z]{2}']], function() {
Route::get('/', 'NewController@frontpage');
Route::get('/home', 'NewController@frontpage')->name("home");
Auth::routes();
});
由于某种原因,我的路线组中的前缀部分根本无法使用。当我在浏览器中输入mywebsite.com/en/home时,我会得到
缺少[Route:login] [URI:{locale} / login]所需的参数
这很奇怪,因为我没有请求登录路由,但请求了本地路由,并且我通过了语言环境。 是否有人看到错误或有更好的主意在Laravel版本6中实现对多种语言的支持?
谢谢。
答案 0 :(得分:0)
您需要将Auth::routes()
带到路线外:
Route::group(['middleware' => ['setlocale'],'prefix' => '{locale}', 'where' => ['locale' => '[a-zA-Z]{2}']], function() {
Route::get('/', 'NewController@frontpage');
Route::get('/home', 'NewController@frontpage')->name("home");
});
Auth::routes();
此问题是由于{locale}
路由(以及所有其他身份验证路由)之前附加了/login
前缀引起的。