yii2-extensions/localeurls 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

yii2-extensions/localeurls

最新稳定版本:0.1.2

Composer 安装命令:

composer require yii2-extensions/localeurls

包简介

Automatic locale/language management for URLs.

README 文档

README

Yii Framework

Locale URLs


PHP Version Yii2 2.0.53 Yii2 22.0 PHPUnit Mutation Testing Static Analysis

A powerful URL manager extension that provides transparent language detection, persistence, and locale-aware URL generation for Yii applications.

Create SEO-friendly multilingual URLs with automatic language switching, GeoIP detection, and comprehensive fallback mechanisms.

Features

  • Automatic Language Detection - From URL, browser headers, session, or GeoIP.
  • Flexible Configuration - Supports language aliases, wildcards, and custom mappings.
  • Language Persistence - Remembers user's language choice.
  • SEO-Friendly URLs - Clean URLs like /en/about or /es/acerca.

Quick start

Installation

composer require yii2-extensions/localeurls

How it works

  1. Detects language from URL path (/es/about → Spanish).
  2. Falls back to browser headers, session, or GeoIP.
  3. Adds language prefix to all generated URLs.
  4. Remember choice in session and cookie.

Basic Configuration

Replace your urlManager component in config/web.php.

<?php

declare(strict_types=1);

use yii2\extensions\localeurls\UrlLanguageManager;

return [
    'components' => [
        'urlManager' => [
            'class' => UrlLanguageManager::class,
            'languages' => ['en', 'es', 'fr', 'de'],
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'rules' => [
                '' => 'site/index',
                '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
            ],
        ],
    ],
];

Basic Usage

Automatic URL generation

<?php

declare(strict_types=1);

use yii\helpers\Url;

// URL are automatically localized based on the current language

// /en/ (if current language is 'en')
Url::to(['site/index']);
// /es/site/about (if current language is 'es')
Url::to(['site/about']);
// Force specific language
Url::to(['site/contact', 'language' => 'fr']); // /fr/site/contact

Language switching

<?php

declare(strict_types=1);

use yii\helpers\{Html, Url};

// Create language switcher links
foreach (Yii::$app->urlManager->languages as $language) {
    echo Html::a(
        strtoupper($language),
        Url::current(['language' => $language]),
    );
}

Current language access

<?php

declare(strict_types=1);

// Get current language
$currentLang = Yii::$app->language;
// Get default language
$defaultLang = Yii::$app->urlManager->getDefaultLanguage();

Documentation

For detailed configuration options and advanced usage patterns.

Quality code

Latest Stable Version Total Downloads codecov phpstan-level StyleCI

Our social networks

X

License

License

Fork

This package is a fork of https://github.com/codemix/yii2-localeurls with some corrections.

统计信息

  • 总下载量: 6.57k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2023-10-31