Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii2 Links between Frontend and Backend (advanced template)

Tags:

php

yii2

If i need add links to frontend stuff from backend part in menu(or from backend to admin), how i can do this without hardcode? This:

 \Yii::$app->request->BaseUrl 

returns path from parents dir

/sitename/backend/web
/sitename/frontend/web
like image 474
WebArtisan Avatar asked Oct 05 '14 19:10

WebArtisan


2 Answers

In your backend application config you should add additional 'UrlManager' component with different name and configuration equals to the one used at front end application:

return [
    'components' => [
        'urlManager' => [
            // here is your backend URL rules
        ],
        'urlManagerFrontEnd' => [
            'class' => 'yii\web\urlManager',
            'baseUrl' => '/a/frontend/web',
            'enablePrettyUrl' => true,
            'showScriptName' => false,
        ],

    ],
];

Then you should invoke following to compose front-end URL:

Yii::$app->urlManagerFrontEnd->createUrl();
like image 195
Harutyun Abgaryan Avatar answered Nov 09 '22 16:11

Harutyun Abgaryan


My mistake - I was incorrectly sent link value.

Wrong:

$menuItems[] = ['label'=>'frontend', 'url'=>[\Yii::$app->urlManagerFrontEnd->baseUrl]];

Thats Works:

$menuItems[] = ['label'=>'frontend', 'url'=>\Yii::$app->urlManagerFrontEnd->baseUrl];
like image 26
WebArtisan Avatar answered Nov 09 '22 14:11

WebArtisan