Yii 2.0 Use UrlManage generate front-end URL in backend

In Yii 2.0 Framework, sometime need generate front-end url in backend, the solution is simple, only need config and use urlManage.

 

{ 1. edit backend/config/main.php }

// backend
'urlManager' => [
    'class' => 'yii\web\urlManager',
    'enablePrettyUrl' => true,
    'showScriptName' => true,
    'rules' => [
        ...
    ],
],
    
// front-end
'urlManagerF' => [
    'class' => 'yii\web\urlManager',
    'baseUrl'=>'http://yiilib.com',
    'enablePrettyUrl' => true,
    'showScriptName' => false,
    'rules' => [
        ...
    ],
],

 

{ 2. Generate URL }

<?= Html::encode(Html::a('Contact YiiLIb.com', Yii::$app->urlManagerF->createUrl('site/contact'))); ?>