How to disable redirecting locales in yii2-localeurls for a specific controller / module

I am using yii2-localeurls to get along with locales in Yii2. Everything works really well. After reading the documentation, you will see that redirection to the preset language or default language happens automatically (even if enableLanguageDetection

- false

). I created a ticket @github to ensure this feature is coming soon.

So here's my lovely config main.php

:

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

    ],
],

      

Default language configuration main.php

according to $ language :

'language' => 'en-gb',

      

Now I have created a module for the API case that looks like this. Where api

route-param is the mapping to my module api

:

Route API Module

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 the yii2-localeurls to redirect to the language if the API module is called. Unable to find information about disabling redirection for certain module

, controller

or route

.

+3


source to share


2 answers


Update / Answer:

yii2-localurl now allows you to define ignoreLanguageUrlPatterns

to exclude routes from locale matching. See Documentation Documentation .



In my case, I would like to ignore /api

localurl's from transforming. Mine yii2 config

looks like this. Works great.

'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/#',
        ],
    ],
],

      

+2


source


At the moment, it doesn't seem like the component you mention actually supports this behavior. So I'm afraid there is no simple answer to this question (at least I don't think so).

However ...
Since URL parsing is done quite early throughout the process, it leaves you a little marinated. There is only one place you can connect before going on to parse: "beforrequest".

So the only way out I can see here is to add a second (normal) one urlManager

to your config that works for APIs and replaces them whenever you encounter an API request.

The way to do it + is like this:

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

      



Also add this to config (top level):

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

      

Whenever a request comes to the api it will use a regularly configured component to parse urls and skip language detection etc.

It works, but is it the best? I'll leave it up to you.
Or you can always add a feature request to add support for ignoring routes to the localeurls component :)

+2


source







All Articles