I built a website with several languages ββand in order to display the correct language, I am doing something like this:
routes.php:
Route::group(['middleware' => 'web', 'prefix' => '{locale}'], function () { Route::auth(); Route::get('home', ' HomeController@index '); etc... });
My controllers:
<?php namespace App\Http\Controllers; use App\Http\Controllers\Controller; use App\Http\Requests; use Illuminate\Http\Request; class HomeController extends Controller { public function index($locale) { app()->setLocale($locale); return view('home'); } }
As you can see, I get a local variable from my prefix, and I install the application locally inside each function.
This works fine, but I wonder if there is a better way to do this? I feel this is a little redundant.
I thought to configure the local application directly in the route group. Something like that:
Route::group(['middleware' => 'web', 'prefix' => '{locale}'], function ($locale) { app()->setLocale($locale); Route::auth(); Route::get('home', ' HomeController@index '); ... });
But this obviously does not work. Has anyone already dealt with such things?
source share