yii2-translatable-url-rule A custom URL rule class for Yii 2 which allows for translated URL rules.

  1. Installation
  2. Usage
  3. Resources

A custom URL rule class for Yii 2 which allows for translated URL rules.

This extension allows you to write URL rules per language. For example, you can have signup for en, aanmelden for nl and registrieren for de. Because this extension uses normal Yii2 UrlRule objects to build language-specific URL rules, you still have all power that comes with normal Yii URL rules, including URL parameters and regular expressions.

The current language is determined by the Yii::$app->language parameter. The value of this parameter is used to create and to parse URLs.

This extension does not set the current language parameter. There are excellent plugins for that, like codemix/yii2-localeurls.


The preferred way to install this extension is through composer.

Either run

composer require geertw/yii2-translatable-url-rule

or add

"geertw/yii2-translatable-url-rule": "*"

to the require section of your composer.json file.


Update your URL rules to use this class and set the patterns per language.

$rules = [
        'class' => 'geertw\Yii2\TranslatableUrlRule\TranslatableUrlRule',
        'patterns' => [
            'en' => '/signup',
            'nl' => '/aanmelden',
            'de' => '/registrieren',
        'route' => 'user/signup',
        'class' => 'geertw\Yii2\TranslatableUrlRule\TranslatableUrlRule',
        'patterns' => [
            'en' => '/user/<username>',
            'nl' => '/gebruiker/<username>',
            'de' => '/benutzer/<username>',
        'route' => 'user/view',

The route parameter remains the same for all rules. patterns is an array of all patterns, the key must equal the Yii language identifier. If there is no pattern for a language, the first configured pattern will be used.

You may omit the class configuration in your URL rules when you configure a ruleConfig in UrlManager:

return [
    'components' => [
        'urlManager'   => [
            'ruleConfig' => [
                'class' => 'geertw\Yii2\TranslatableUrlRule\TranslatableUrlRule'
            'rules' => $rules,
            // Additional UrlManager configuration


0 0
1 follower
Yii Version: 2.0
License: MIT
Category: Others
Developed by: djiwie
Created on: Apr 17, 2017
Last updated: 7 years ago

Related Extensions