7. Làm đường dẫn Ứng dụng của đa ngôn ngữ (ok)

https://viblo.asia/p/tap-18-url-laravel-gDVK2pyjlLj

C:\xampp\htdocs\blog\routes\web.php

<?php

Route::get('/{locale}/post', function() {
    return 'My post';
})->name('post');
Route::get('/try', function() {
    return route('post');
})->middleware('locale');

C:\xampp\htdocs\blog\config\app.php

'locale' => 'en',

C:\xampp\htdocs\blog\app\Http\Kernel.php

'locale' => \App\Http\Middleware\SetDefaultLocaleForUrls::class,

C:\xampp\htdocs\blog\app\Http\Middleware\SetDefaultLocaleForUrls.php

<?php

namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\URL;
class SetDefaultLocaleForUrls {
  /**
   * Handle an incoming request.
   *
   * @param  \Illuminate\Http\Request  $request
   * @param  \Closure  $next
   * @return mixed
   */
  public function handle($request, Closure $next) {
    URL::defaults(['locale' => config('app.locale')]);
    return $next($request);
  }
}

Last updated