WebArtisan
WebArtisan

Reputation: 4227

Yii2 Links between Frontend and Backend (advanced template)

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

Upvotes: 12

Views: 17057

Answers (2)

WebArtisan
WebArtisan

Reputation: 4227

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];

Upvotes: 3

Harutyun Abgaryan
Harutyun Abgaryan

Reputation: 2023

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();

Upvotes: 23

Related Questions