简体   繁体   中英

Yii2 render frontend view from backend controller

My task is simple. I have to render frontend view from backend controller. What i've done so far is to create urlManagerFrontendView path in my main.php :

'urlManagerFrontendView' => [
    'class' => 'yii\web\urlManager',
    'baseUrl' => $_SERVER['DOCUMENT_ROOT'] . '/frontend/view/',
    'enablePrettyUrl' => true,
    'showScriptName' => false,
],

This is how i am trying to render it(from the backend):

return $this->render(''.Yii::$app->urlManagerFrontendView->baseUrl.'page/product', [
            'model' => $product,
            'product_rel' => $product_rel,
            'thispage' => $thispage,
        ]);

But what i get is an error:

The view file does not exist: /home/projects/tashev-online/htdocs/backend/views/home/projects/tashev-online/htdocs/frontend/viewpage/product.php

Obviously i didn't create it right :) So can you guys help my with my path? What is the right way to reach the frontend? Thank you in advance!

You don't have to do anything with UrlManager in this case. Simply call the view using relative path (like this or similar):

return $this->render('../../../frontend/views/page/product', ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM