I have solved my issue as follow:
1 Step. Created a middleware. php artisan make:middleware SetLocale
2 Step. Updated the middleware file. File: app/Http/Middleware/SetLocale.php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
class SetLocale
{
private $locales = ['ar', 'en'];
// ...
public function handle($request, Closure $next, $locale)
{
if (array_search($locale, $this->locales) === false) {
return redirect('/');
}
App::setLocale($locale);
return $next($request);
}
}
3 Step. Appended my new middleware to app/Http/Kernel.php
$routeMiddleware
array.
namespace App\Http;
use Illuminate\Foundation\Http\Kernel as HttpKernel;
class Kernel extends HttpKernel
{
// ...
protected $routeMiddleware = [
// ...
'locale' => \App\Http\Middleware\SetLocale::class,
];
}
4 Step. I have used my the middleware in my routes/web.php
file.
Route::group(['prefix' => 'ar', 'namespace' => 'Arabic', 'middleware' => 'locale:ar'], function() {
Route::get('/', 'PashtoHomeController@index')->name('arHome');
// ...
});
Route::group(['prefix' => 'en', 'namespace' => 'English', 'middleware' => 'locale:en'], function() {
Route::get('/', 'PashtoHomeController@index')->name('enHome');
// ...
});
Route::get('/', function() {
return redirect()->route('arHome');
});
$next
hold? – Huffy