PHP code example of mvccore / ext-router-localization

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

    

mvccore / ext-router-localization example snippets


$app = \MvcCore\Application::GetInstance();
$app->SetRouterClass('\MvcCore\Ext\Routers\Localization');
...
// to get router instance for next configuration:
/** @var \MvcCore\Ext\Routers\Localization $router */
$router = \MvcCore\Router::GetInstance();

$router->SetDefaultLocalization('en-US');

$router->SetAllowedLocalizations(/*'en-US', */'en-DE');

$router->SetRoutes([

    // If you want to add automatically localized route very easily, 
    // you can use only definition like this to define router key with 
    // `Namespace\Controller:Action` and `pattern` as '/something' 
    'Admin\Index:Index'    => '/admin',
    
    // Localized route with automatically completed `match` 
    // and `reverse` records from `pattern` record:
    'Front\Products:List'   => [
        'pattern'          => [
            'en'           => "/products-list[/<page>]",
            'de'           => "/produkte-liste[/<page>]",
        ],
        'defaults'         => ['page' => 1],
        'constraints'      => ['page' => '\d+'],
    ],
    
    // Localized route with explicitly defined `match` and `reverse` 
    // records with also localized `defaults` values:
    'Front\Products:Detail' => [
        'match'            => [
            'en'           => '#^/product/(?<id>\d+)(/(?<color>[a-z]+))?/?#',
            'de'           => '#^/produkt/(?<id>\d+)(/(?<color>[a-z]+))?/?#'
        ],
        'reverse'          => [
            'en'           => '/product/<id>[/<color>]',
            'de'           => '/produkt/<id>[/<color>]'
        ],
        'defaults'         => [
            'en'           => ['color' => 'red'],
            'de'           => ['color' => 'rot'],
        ]
    ],
    
    // Automatically localized route, `pattern` record and later 
    // `match` and `reverse` records are defined for all localizations
    // with the same values `/<path>`, `constraints` are never localized:
    'Front\Index:Index'    => [
        'pattern'          => '/<path>',
        // constraints are never localized:
        'constraints'      => ['path' => '[-a-zA-Z0-9_/]+']
    ],
    
]);

$router->SetAllowNonLocalizedRoutes(FALSE);

$router->SetDetectLocalizationOnlyByLang(FALSE);

$router->->SetLocalizationEquivalents([
    // Browsers preferring UK, USA or Canada are considered as `en-US` locale to send
    'en-US'    => ['en-GB', 'en-CA', 'en-AU'],
    // Browsers preferring Slovak are considered as `cs-CZ` locale to send
    'cs-CZ'    => ['sk-SK'],    // Czech and Slovak
]);
...
$router->AddLocalizationEquivalents(/*... same param syntax as method above*/);

$router->SetRouteRecordsByLanguageAndLocale(TRUE);

$router->SetRedirectFirstRequestToDefault(TRUE);

// somewhere in Bootstrap.php:
$router
    ->SetDefaultLocalization('en-US')
    ->SetAllowedLocalizations('de-DE')
    ->SetRouteRecordsByLanguageAndLocale(FALSE)
    ->AddRoutes([
        'Front\Product:Detail' => [
            'pattern'              => [
                'en'               '/product/<id>',
                'de'               '/produkt/<id>',
            ],
            'constraints'          => [
                'id'               => '\d+',
            ]
        ]
    ]);
...
// somewhere in template or in controller (if router has matched localization `de-DE`):
$this->Url('Front\Product:Detail', [
    'id' => 50
]);
// will return: `/de-DE/produkt/50`

// somewhere in Bootstrap.php:
$router->AddRoutes([
    'admin' => [
        'pattern'              => '/admin/<controller>/<action>[/<id>]',
        'constraints'          => [
            'controller'       => '-a-z0-9',
            'action'           => '-a-z0-9',
            'id'               => '\d+',
        ]
    ]
]);
...
// somewhere in template or in controller (if router has matched any localization):
$this->Url('admin', [
    'controller' => 'products', 
    'action'     => 'update', 
    'id'         => 50
]);
// will return: `/admin/products/update/50`

$router->SetLocalization('de', 'DE');
...
// anywhere you have $router instance:
$router->Url('Front\Product:Detail', ['id' => 50]);    // `/de-DE/produkt/50`
...
// or somewhere in template or in controller:
$this->Url('Front\Product:Detail', ['id' => 50]);    // `/de-DE/produkt/50`

$router->SetSessionExpirationSeconds(
    \MvcCore\Session::EXPIRATION_SECONDS_DAY
);

$router->SetStricModeBySession(TRUE);

$router->SetRouteGetRequestsOnly(FALSE);