如何在yii2

编程入门 行业动态 更新时间:2024-10-09 20:26:49
如何在yii2-localeurls中禁用特定控制器/模块的语言环境重定向(How to disable locales redirect in yii2-localeurls for specific controller/module)

我正在使用yii2-localeurls与Yii2中的语言环境相处。 一切都很好。 通过阅读文档,您会看到,重定向到已配置的语言或默认语言会自动发生(即使enableLanguageDetection为false )。 我创建了一个@ github票证,以确保即将推出此功能。

所以这是我的精细main.php配置:

'urlManager' => [ 'class' => 'codemix\localeurls\UrlManager', 'languages' => [ 'en' => 'en-gb', 'de' => 'de-de' ], 'enableLanguageDetection' => false, 'enablePrettyUrl' => true, 'showScriptName' => false, 'rules' => [ ], ],

根据$ language , main.php默认语言配置:

'language' => 'en-gb',

现在,我为API案例创建了一个模块,其路由如下。 api route-param映射到我的API模块的位置:

API模块路由

application.com/api/<controller>/<action>

模块类

//namespace define namespace app\modules\api; use Yii; /** * Class api * * @package app\modules\api */ class api extends \yii\base\Module { // ####################################### Class attributes // ##################################################### /** * Controller namespace * @var string */ public $controllerNamespace = 'app\modules\api\controllers'; // ########################################## Class methods // ##################################################### /** * Init API module */ public function init() { //call parent class init parent::init(); } }

如果调用API模块,我不希望yii2-localeurls重定向到语言环境。 找不到有关特定module , controller或route禁用重定向的任何信息。

I'm using yii2-localeurls to get along with locales in Yii2. All is working pretty fine. By reading the documentation, you see, redirecting to a configured language or to default language happens automatically (even if enableLanguageDetection is false). I created a ticket@github to ensure this functionality is coming soon.

So here is my fine main.php configuration:

'urlManager' => [ 'class' => 'codemix\localeurls\UrlManager', 'languages' => [ 'en' => 'en-gb', 'de' => 'de-de' ], 'enableLanguageDetection' => false, 'enablePrettyUrl' => true, 'showScriptName' => false, 'rules' => [ ], ],

Default language configuration in main.php according to $language :

'language' => 'en-gb',

Now, I created a module for a API case which route like the following. Where api route-param is mapping to my API Module:

API module Route

application.com/api/<controller>/<action>

Module class

//namespace define namespace app\modules\api; use Yii; /** * Class api * * @package app\modules\api */ class api extends \yii\base\Module { // ####################################### Class attributes // ##################################################### /** * Controller namespace * @var string */ public $controllerNamespace = 'app\modules\api\controllers'; // ########################################## Class methods // ##################################################### /** * Init API module */ public function init() { //call parent class init parent::init(); } }

I don't want to yii2-localeurls redirect to a locale if API module is called. Can't find any information about disable redirect for a specific module, controller or a route.

最满意答案

在这一点上,它看起来不像你提到的组件实际上支持这种行为。 所以我担心这个问题没有简单的答案(至少不是我能想到的)。

然而... 由于URL解析在整个过程中很早就完成了,所以它会让你有点蠢蠢欲动。 在进行解析之前,只有一个地方可以挂钩:“beforerequest”。

因此,我在这里看到的唯一方法是在配置中添加第二个(常规) urlManager ,该API适用于API,并在您检测到API请求时将其交换出来。

这样做的方法是+ - 像这样:

'components' => [ 'apiUrlManager' => [ 'class' => '\yii\web\UrlManager', ... ] ]

还要在配置中添加此项(顶级):

'on beforeRequest' => function($event) { if (substr($_SERVER['REQUEST_URI'], 0, 5) == '/api/') \Yii::$app->set('urlManager', \Yii::$app->get('apiUrlManager')); },

每当请求进入api时,它将使用定期配置的组件进行URL解析并跳过语言检测等。

它有效,但它是否是最好的方法? 我会把它留给你。 或者你总是可以添加一个功能请求来向localeurls-component添加忽略路由支持:)

Update / Answer:

yii2-localurl now allows to define ignoreLanguageUrlPatterns to exclude routes from locale matching. See the Documentation.

In my case, i'd like to ignore /api from being transformed by localurl's. My yii2 config looks like following. It works like a charm.

'components' => [ 'urlManager' => [ 'class' => 'codemix\localeurls\UrlManager', 'languages' => [ 'en' => 'en-gb', 'de' => 'de-de' ], 'enableLanguageDetection' => false, 'enablePrettyUrl' => true, 'showScriptName' => false, 'rules' => [ ], // Ignore / Filter route pattern's 'ignoreLanguageUrlPatterns' => [ '#^api/#' => '#^api/#', ], ], ],

更多推荐

本文发布于:2023-08-07 12:42:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464315.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!