Determine name of the end

Hi,

I have class in common directory that is used in many ends (e.g. frontend/backend/console/common).

Is there helper method to get name of the end? e.g. "frontend", or "backend"?

maybe something like this: Yii::$app->getRequestEndName()

You can get it by session identity cookie. Check this application’s configs

Also you can make constants in [backend || frontend]/web/index.php

Code example?

1 variant:

Yii::$app->response->cookies->has(’_identity-frontend’);

Or

Yii::$app->response->cookies->has(’_identity-backend’); respectively

Note. Check your cookie names in config

2 way (I think better):

in index.php (main entry point) declare const, for example

defined(‘YII_APP’) or define(‘YII_APP’, ‘frontend’);

Or

defined(‘YII_APP’) or define(‘YII_APP’, ‘backend’); respectively for backend application


$currentApp = Yii::$app->getAlias('@app');

if(strpos($currentApp, 'frontend') !==false) {

   //Do frontend stuff

} elseif (strpos($currentApp, 'backend') !==false) {

   //Do backend stuff

} else {

   //throw error

}

I maybe wrong on which alias to use, but may help.

Thanks, umneeq and jkofsky I try it.

Had a couple of more thoughts.

1: use the params[]


$currentApp = Yii::$app->params('appType');

if($currentApp == 'IAmFrontend') {

   //Do frontend stuff

} elseif ($currentApp == 'IAmBackend') {

   //Do backend stuff

} else {

   //throw error

}

In params config add one of the below


'appType' => 'IAmFrontend', //In frontend

'appType' => 'IAmBackend', //In backend

2: In the advanced template the main config there is an id property. Just check it


if(Yii::$app->id == 'app-frontend') {

}elseif  (Yii::$app->id == 'app-backend') {

}else {}