Yii2 Links between Frontend and Backend (advanced

2019-01-15 00:06发布

问题:

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

回答1:

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


回答2:

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


标签: php yii2