PHP code example of skyraptor / laravel-localized-routes

1. Go to this page and download the library: Download skyraptor/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/ */

    

skyraptor / laravel-localized-routes example snippets


'supported-locales' => ['en', 'nl', 'fr'],

'supported-locales' => [
  'en' => 'example.com',
  'nl' => 'nl.example.com',
  'fr' => 'fr.example.com',
],

'omit_url_prefix_for_locale' => null

protected $middlewarePriority = [
    \Illuminate\Session\Middleware\StartSession::class, // <= after this
    //...
    \CodeZero\LocalizedRoutes\Middleware\SetLocale::class,
    //...
    \Illuminate\Routing\Middleware\SubstituteBindings::class, // <= before this
];

'use_locale_middleware' => true

protected $middlewareGroups = [
    'web' => [
        \Illuminate\Session\Middleware\StartSession::class, // <= after this
        //...
        \CodeZero\LocalizedRoutes\Middleware\SetLocale::class,
        //...
        \Illuminate\Routing\Middleware\SubstituteBindings::class, // <= before this
    ],
];

Route::localized(function () {

    Route::get('about', AboutController::class.'@index')
        ->name('about')
        ->middleware(\CodeZero\LocalizedRoutes\Middleware\SetLocale::class);

    Route::group([
        'as' => 'admin.',
        'middleware' => [\CodeZero\LocalizedRoutes\Middleware\SetLocale::class],
    ], function () {

        Route::get('admin/reports', ReportsController::class.'@index')
            ->name('reports.index');

    });

});

'use_localizer' => true

Route::localized(function () {

    Route::get('about', AboutController::class.'@index')
        ->name('about');

}, [
    'supported-locales' => ['en', 'nl', 'fr'],
    'omit_url_prefix_for_locale' => null,
    'use_locale_middleware' => false,
]);

// Not localized
Route::get('home', HomeController::class.'@index')
    ->name('home');

// Localized
Route::localized(function () {

    Route::get('about', AboutController::class.'@index')
        ->name('about');

    Route::name('admin.')->group(function () {
        Route::get('admin/reports', ReportsController::class.'@index')
            ->name('reports.index');
    });

});

Route::fallback(\CodeZero\LocalizedRoutes\Controller\FallbackController::class)
    ->middleware(\CodeZero\LocalizedRoutes\Middleware\SetLocale::class);

$url = route(app()->getLocale().'.admin.reports.index');

$url = route('admin.reports.index'); // current locale
$url = route('admin.reports.index', [], true, 'nl'); // dutch URL

route($name, $parameters = [], $absolute = true, $locale = null)

app()->setLocale('en');
app()->getLocale(); // 'en'

$url = route('home'); // /home (normal routes have priority)
$url = route('about'); // /en/about (current locale)

// Get specific locales...
// This is most useful if you want to generate a URL to switch language.
$url = route('about', [], true, 'en'); // /en/about
$url = route('about', [], true, 'nl'); // /nl/about

// You could also do this, but it kinda defeats the purpose...
$url = route('en.about'); // /en/about
$url = route('en.about', [], true, 'nl'); // /nl/about

return redirect()->route('home'); // non-localized route, redirects to /home
return redirect()->route('about'); // localized route, redirects to /en/about (current locale)

return redirect(route('about', [], true, 'nl')); // localized route, redirects to /nl/about

$current = \Route::localizedUrl(); // /en/posts/en-slug
$en = \Route::localizedUrl('en'); // /en/posts/en-slug
$nl = \Route::localizedUrl('nl'); // /nl/posts/nl-slug

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 :)
        ];
    }
}

$current = \Route::localizedUrl(); // /en/posts/en-slug
$en = \Route::localizedUrl('en'); // /en/posts/en-slug
$nl = \Route::localizedUrl('nl'); // /nl/posts/nl-slug

$nl = \Route::localizedUrl('nl'); // Wrong: /nl/posts/en-slug
$nl = \Route::localizedUrl('nl', [$post->getSlug('nl')]); // Right: /nl/posts/nl-slug

$signedUrl = URL::signedRoute('reset.password', ['user' => $id], now()->addMinutes(30));

$signedUrl = URL::signedRoute($name, $parameters, $expiration, true, 'nl');

// lang/nl/routes.php
return [
    'glass' => 'glas',
    'products' => 'producten',
    'materials' => 'materiaal',
    'materials/glass' => 'materiaal/glazen'
];

Route::localized(function () {

    Route::get(Lang::uri('products/glass'), ProductsController::class.'@index')
        ->name('products.glass');

    Route::get(Lang::uri('materials/glass'), MaterialsController::class.'@index')
        ->name('materials.glass');

});

Route::localized(function () {

    Route::get(Lang::uri('products/glass', null, 'shop'), ProductsController::class.'@index')
        ->name('products.glass');

});

class Post extends \Illuminate\Database\Eloquent\Model
{
    public function getRouteKey()
    {
        $slugs = [
            'en' => 'en-slug',
            'nl' => 'nl-slug',
        ];

        return $slugs[app()->getLocale()];
    }
}

Route::localized(function () {

    Route::get('posts/{post}', PostsController::class.'@show')
        ->name('posts.show');

});

app()->setLocale('en');
app()->getLocale(); // 'en'

$post = new Post;

$url = route('posts.show', [$post]); // /en/posts/en-slug
$url = route('posts.show', [$post], true, 'nl'); // /nl/posts/nl-slug

public function resolveRouteBinding($value)
{
    // Perform the logic to find the given slug in the database...
    return $this->where($this->getRouteKeyName().'->'.app()->getLocale(), $value)->firstOrFail();
}

// Use the post slug as the route parameter instead of the default ID
Route::get('posts/{post:slug}', ...);

public function resolveRouteBinding($value, $field = null)
{
    // Perform the logic to find the given slug in the database...
    return $this->where($field ?? $this->getRouteKeyName().'->'.app()->getLocale(), $value)->firstOrFail();
}
bash
php artisan vendor:publish --provider="CodeZero\LocalizedRoutes\LocalizedRoutesServiceProvider" --tag="config"

resources
 └── lang
      ├── en
      │    └── routes.php
      └── nl
           └── routes.php
bash
php artisan route:cache