1) создать middleware lang
php artisan make:middleware LangMiddleware
2) подключить LangMiddleware в файле Kernel.php и определить короткое имя.
protected $routeMiddleware = [ 'auth' => 'App\Http\Middleware\Authenticate', 'lang' => 'App\Http\Middleware\LangMiddleware', 'guest' => 'App\Http\Middleware\RedirectIfAuthenticated', ];
3) Находим метод handle LangMiddleware public function handle($request, Closure $next){ return $next($request) }
4) Логика обработки запроса
public function handle($request, Closure $next){ if(isset($_GET['lang'])){ $lang = $_GET['lang']; }else{ if(isset($_COOKIE['lang'])){ $lang = $_COOKIE['lang']; }else{ $lang = 'ru'; } } App::setLocale($lang); setcookie('lang', $lang, time()+3600, '/'); $request->merge(compact('lang')); return $next($request) }
5) web.php
Route::group(['middleware' => ['lang']], function(){ Route::get('/', ....) })
6) resources/lang/en
Validation.php
Menu.php
return [ 'about' => 'About', 'news' => 'News', ]/ru
return [ 'about' => 'О компании', 'news' => 'Новости', ]
7) blade {{__('menu.about')}}
use Illuminate\Http\Request; use App; public function getIndex(Request $request){ // $request->lang; // App::getLocale(); $obj = Maintext::where('url', 'index') ->where('lang', $request->lang) ->first(); $obj->name; }