Yii 2:将模块的视图加载为另一个应用程序视图的部分视图

Yii 2: Load a module's view as a partial view on another application view

我需要加载一个模块的视图作为应用程序另一个视图中的部分视图。我在手册中找不到关于如何执行此操作的线索。

视图完全独立于模块:

<?php
    // This is the module's class. Do I need it here?
    use vendor\xxx\cropk\CropK;

/* @var $this yii\web\View */
    $this->title = 'Cropping Test';
?>
<div class="site-index">
    <p>Cropping Test</p>
    <?php
        // ...
    ?>
</div>

我怎样才能做到这一点?

查看 render's 文档,您有几个选择:

The view to be rendered can be specified in one of the following formats:

  • path alias (e.g. "@app/views/site/index");
  • absolute path within application (e.g. "//site/index"): the view name starts with double slashes. The actual view file will be looked for under the view path of the application.
  • absolute path within module (e.g. "/site/index"): the view name starts with a >single slash. The actual view file will be looked for under the view path of $module.
  • relative path (e.g. "index"): the actual view file will be looked for under $viewPath.

根据这些选择,您似乎将在应用程序中指定绝对路径,或者创建路径别名并使用该语法(应用程序?主站点视图?无论它位于何处。)

所以,如果你想渲染 {basePath}/views/site/my_partial.php 你会在你的视图中做类似 $this->renderPartial('//site/my_partial.php'); 的事情。