PHP code example of larasofthu / localized-routes-plus
1. Go to this page and download the library: Download larasofthu/localized-routes-plus library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
larasofthu / localized-routes-plus example snippets
use Illuminate\Support\Facades\Route;
// Creates routes for all configured locales
Route::get('about', function () {
return view('about');
})->name('about')->localized();
// Results in:
// /about (default locale)
// /hu/about (Hungarian)
// /de/about (German)
Route::resource('posts', PostController::class)
->names('posts')
->localized();
// Creates localized versions of all resource routes:
// en.posts.index, en.posts.create, en.posts.store, etc.
// hu.posts.index, hu.posts.create, hu.posts.store, etc.
Route::get('products', [ProductController::class, 'index'])
->name('products.index')
->localized();
// With dash separator:
// GET /en-us/products -> en-us.products.index
// GET /en-ca/products -> en-ca.products.index
// Only create routes for English and Hungarian
Route::get('admin', [AdminController::class, 'index'])
->name('admin.index')
->localized(['en', 'hu']);
// Single locale
Route::get('terms', function () {
return view('terms');
})->name('terms')->localized('en');
// Create routes for all locales except German
Route::get('news', [NewsController::class, 'index'])
->name('news.index')
->localizedExcept('de');
// Multiple locales
Route::get('blog', [BlogController::class, 'index'])
->name('blog.index')
->localizedExcept(['de', 'fr']);
Route::get('posts/{post}', [PostController::class, 'show'])
->name('posts.show')
->localized();
// Works with model binding:
// /posts/my-post-slug -> en.posts.show
// /hu/posts/my-post-slug -> hu.posts.show
// Automatically applied to localized routes
SetLocaleFromRoute::class // Sets App::setLocale()
SetCountryFromRoute::class // Sets App::setCountry() (when countries enabled)
// In your views or controllers
$currentRoute = request()->route();
// Get route for different locale
$germanRoute = $currentRoute->locale('de');
$germanUrl = $germanRoute->getUrl();
// With countries
$usRoute = $currentRoute->locale('en', 'us');
$canadaRoute = $currentRoute->locale('en', 'ca');
// Standard Laravel route() helper works
$url = route('products.index'); // Current locale
$hungarianUrl = route('hu.products.index'); // Specific locale
// With countries
$usUrl = route('en-us.products.index');
$canadaUrl = route('en-ca.products.index');
// Using helper methods
$route = request()->route();
$germanUrl = $route->getUrl('de');
$usUrl = $route->getUrl('en', 'us'); // With country
$route = request()->route();
// Get locale and country
$locale = $route->getLocale(); // 'en'
$country = $route->getCountry(); // 'us' (if countries enabled)
// Get route name without locale prefix
$safeName = $route->getSafeName(); // 'products.index'
// Check if route matches a name
if ($route->is('products.index')) {
// Current route is products.index (any locale)
}
// Make sure all locales have corresponding domains when using subdomains
'domains' => [
'en' => 'example.com',
'hu' => 'example.hu', // Don't forget this!
],