php - How to disable locales redirect in yii2-localeurls for specific controller/module -
i'm using yii2-localeurls along locales in yii2. working pretty fine. reading documentation, see, redirecting configured language or default language happens automatically (even if enablelanguagedetection
false
). created ticket@github ensure functionality coming soon.
so here 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 $language :
'language' => 'en-gb',
now, created module api case route following. api
route-param mapping 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 yii2-localeurls redirect locale if api module called. can't find information disable redirect specific module
, controller
or route
.
at point doesn't component mention supports kind of behavior. i'm afraid there's no easy answer question (at least not can think of).
however...
since url parsing done in entire process leaves bit in pickle. there 1 spot can hook before parsing: "beforerequest".
so way out see here add second (regular) urlmanager
in configuration works api , swap them out whenever detect api request.
the way +- this:
'components' => [ 'apiurlmanager' => [ 'class' => '\yii\web\urlmanager', ... ] ]
also add in configuration (top level):
'on beforerequest' => function($event) { if (substr($_server['request_uri'], 0, 5) == '/api/') \yii::$app->set('urlmanager', \yii::$app->get('apiurlmanager')); },
whenever request comes in api, use regularly configured component url parsing , skip language detection etc.
it works, whether it's best method? i'll leave you.
or add feature request add ignore routes support localeurls-component :)
Comments
Post a Comment