【问题标题】:YII 2.0.3 view does not render. It goes just blank. Shows only 1 when index is calledYII 2.0.3 视图不渲染。它只是空白。调用 index 时仅显示 1
【发布时间】:2015-03-15 12:05:12
【问题描述】:

我正在经历 制作自定义应用程序 Web 应用程序开发的 Yii 2 章节 Mark Safronov 和 Jeffrey Winesett 使用 Yii 2 和 PHP。但是,我被困得很糟糕! 当我尝试(在 localhost wamp 服务器上运行高级模板)时,视图不会呈现... http://localhost/furni/frontend/web/index.php?r=customers 它触发的动作是..

 class CustomersController extends Controller{ 
      public function actionIndex() {   
         $records = $this->findRecordsByQuery();
         $this->render('index', compact('records'));
         return true; 
      }
      .......
      .......
 }

请注意我的模型文件夹。它是客户,而视图/控制器文件夹/命名空间是客户s,带有 s。表是客户。 我的模型在 project-folder\frontend\models\customer
我在 project-folder\frontend\views\customers 中有 index.php 布局。 控制器位于 project-folder\frontend\controllers 中。我在视图文件中几乎什么都没有。..

<?php
   $this->title = 'Index for customers';
?>
<div class="site-index">
Echo Out Loud
</div>

它在空白页中显示 1!

如果我把代码改成这个..

<?php
   $this->title = 'Index for customers';
?>
<div class="site-index">
Echo Out Loud
</div>
<?php
die();

它呈现视图,但不包括页眉和页脚等的布局。没有头部标签,没有脚本,样式......什么都没有。

但是,站点控制器索引和其他预建视图可以正常渲染!没问题。拔头发时我错过了什么?

【问题讨论】:

    标签: php yii yii2


    【解决方案1】:

    您的操作将显示操作返回的任何内容。在您的情况下,您的操作返回“真”,因此显示为 1(表示真)。如果你想让它显示视图,那么你需要返回它,所以

     class CustomersController extends Controller{ 
      public function actionIndex() {   
         $records = $this->findRecordsByQuery();
         return $this->render('index', compact('records'));
         }
      .......
      .......
     }
    

    【讨论】:

    • ..但在 SiteController.php 中 actionIndex 方法也返回 $this->render('index'); return $this-&gt;render('index');我应该假设视图返回 true 吗?
    • 没关系,我只是愚蠢!感谢您在最后指出额外的return true;!我没有注意到。呸!
    猜你喜欢
    • 1970-01-01
    • 2022-10-17
    • 2013-06-10
    • 1970-01-01
    • 2015-05-15
    • 1970-01-01
    • 1970-01-01
    • 2013-02-17
    • 1970-01-01
    相关资源
    最近更新 更多