如何在Laravel中从URL获取语言环境?

时间:2018-08-07 14:00:26

标签: laravel

我想在API中应用语言环境,但是它仅返回ar语言。我想从网址[ar或en]中获取语言环境,例如:localhost/mertaah/api/en/settings必须采用en语言环境,但始终需要ar

<?php

namespace App\Http\Middleware;

use Closure;

class ApplyAPILocale
{
    /**
     * Handle an incoming request.
     *
     * @param \Illuminate\Http\Request $request
     * @param \Closure                 $next
     *
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        app()->setLocale($request->header('locale') ?? 'ar');

        return $next($request);
    }
}

1 个答案:

答案 0 :(得分:-1)

这是可以帮助我解决问题的解决方案 https://laracasts.com/discuss/channels/laravel/pass-locale-to-url