cetver / yii2-language-url-manager
Parses and creates URLs containing languages for the Yii framework 2.0
Installs: 98 229
Dependents: 0
Suggesters: 0
Security: 0
Stars: 6
Watchers: 1
Forks: 3
Open Issues: 1
Type:yii2-extension
Requires
- yiisoft/yii2: ~2.0.0
Requires (Dev)
- codeception/base: ^2.2.3
- codeception/specify: ~0.4.3
- codeception/verify: ~0.3.1
- satooshi/php-coveralls: ^1.0
This package is auto-updated.
Last update: 2025-02-16 04:39:10 UTC
README
Parses and creates URLs containing languages
Installation
The preferred way to install this extension is through composer.
Either run
composer require --prefer-dist cetver/yii2-language-url-manager
or add
"cetver/yii2-language-url-manager": "^1.0"
to the require section of your composer.json
file.
Usage
Update the web-application configuration file
return [ 'components' => [ 'urlManager' => [ 'class' => 'cetver\LanguageUrlManager\UrlManager', 'enablePrettyUrl' => true, 'showScriptName' => false, /* * The list of available languages. */ 'languages' => ['en', 'ru'], /* or 'languages' => function () { return \app\models\Language::find()->select('code')->column(); }, */ /* * - true: processes the URL like "en.example.com" * - false: processes the URL like "example.com/en" * NOTE: If this property set to true, the domain containing a language, must be the first on the left side, * for example: * - en.it.example.com - is valid * - it.en.example.com - is invalid */ 'existsLanguageSubdomain' => false, /* * The regular expression patterns list, applied to path info, if there are matches, the request, * containing a language, will not be processed. * For performance reasons, the blacklist does not applied for URL creation (Take a look at an example). * @see \yii\web\Request::getPathInfo() * An example: * ```php * [ * '/^api.*$/' * ] * ``` * - Requesting the blacklisted URL * - $existsLanguageSubdomain = true * - en.example.com/api (404 Not Found) * - en.example.com/api/create (404 Not Found) * - $existsLanguageSubdomain = false * - example.com/en/api (404 Not Found) * - example.com/en/api/create (404 Not Found) * - Creating the blacklisted URL * - echo \yii\helpers\Html::a('API', ['api/index', Yii::$app->urlManager->queryParam => null]); */ 'blacklist' => [], /* * The query parameter name that contains a language. */ 'queryParam' => 'language' ], ] ];
Tests
Run the following commands
composer create-project --prefer-source cetver/yii2-language-url-manager
cd yii2-language-url-manager
vendor/bin/codecept run unit
For I18N support, take a look at
- https://github.com/cetver/yii2-languages-dispatcher - Sets the web-application language
- https://github.com/cetver/yii2-language-url-manager - Parses and creates URLs containing languages
- https://github.com/cetver/yii2-language-selector - Provides the configuration for the language selector
- https://github.com/creocoder/yii2-translateable - The translatable behavior (Active Record)