PHP code example of codezero / laravel-localized-routes
1. Go to this page and download the library: Download codezero/laravel-localized-routes 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/ */
codezero / laravel-localized-routes example snippets
// Example: use the post slug as the route parameter
Route::get('posts/{post:slug}', [PostsController::class, 'index']);
// PostsController.php
public function index(Post $post)
{
return $post;
}
public function resolveRouteBinding($value, $field = null)
{
// Default field to query if no parameter field is specified
$field = $field ?: $this->getRouteKeyName();
// If the parameter field is 'slug',
// lets query a JSON field with translations
if ($field === 'slug') {
$field .= '->' . App::getLocale();
}
// Perform the query to find the parameter value in the database
return $this->where($field, $value)->firstOrFail();
}
// Translate every slug individually
// Translates to: 'hallo/wereld'
Lang::uri('hello/world');
// Keep original slug when missing translation
// Translates to: 'hallo/big/wereld'
Lang::uri('hello/big/world');
// Translate slugs, but not parameter placeholders
// Translates to: 'hallo/{world}'
Lang::uri('hello/{world}');
// Translate full URIs if an exact translation exists
// Translates to: 'something/very/different'
Lang::uri('override/hello/world');
// Translate full URIs if an exact translation exists (with placeholder)
// Translates to: 'uri/with/{parameter}'
Lang::uri('hello/world/{parameter}');
// When the fallback locale is set to 'en'
// and the supported locales are 'en' and 'nl'
$url = route('about', [], true, 'nl'); // this will load 'nl.about'
$url = route('about', [], true, 'wk'); // this will load 'en.about'
use CodeZero\LocalizedRoutes\ProvidesRouteParameters;
use Illuminate\Database\Eloquent\Model;
class Post extends Model implements ProvidesRouteParameters
{
public function getRouteParameters($locale = null)
{
return [
$this->id,
$this->getSlug($locale) // Add this method yourself of course :)
];
}
}
// Check if a named route exists in the active locale:
$exists = Route::hasLocalized('about');
// Check if a named route exists in a specific locale:
$exists = Route::hasLocalized('about', 'nl');
// Check if the current route is localized:
$isLocalized = Route::isLocalized();
// Check if the current route is localized and has a specific name:
$isLocalized = Route::isLocalized('about');
// Check if the current route has a specific locale and has a specific name:
$isLocalized = Route::isLocalized('about', 'nl');
// Check if the current route is localized and its name matches a pattern:
$isLocalized = Route::isLocalized(['admin.*', 'dashboard.*']);
// Check if the current route has one of the specified locales and has a specific name:
$isLocalized = Route::isLocalized('about', ['en', 'nl']);
// Check if the current route is a fallback route:
$isFallback = Route::isFallback();