Smoking Sheriff Smoking Sheriff - 4 months ago 13
PHP Question

YII 2.0.3 view does not render. It goes just blank. Shows only 1 when index is called

I was going through the Making a Custom Application
with Yii 2
chapter of Web Application Development
with Yii 2 and PHP
by Mark Safronov and Jeffrey Winesett. However, I am stuck pretty bad!
The view does not render when I try (running the advanced template on localhost wamp server ) ...
http://localhost/furni/frontend/web/index.php?r=customers
The action it is firing is..

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


Please take note of my models folder. It's customer, while views/controller folder/namespace is customers, with an s. Table is customer.
I have my models in project-folder\frontend\models\customer

I have the index.php layout in project-folder\frontend\views\customers.
Controller is in project-folder\frontend\controllers. I have almost nothing in the view file..

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


It shows 1 in a blank page!! Here is what the view shows..

If I change the code to this..

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


It renders the view, but without including the layouts for header and footer etc. No head tag, no scripts, styles.. nothing.
enter image description here

However, site controller index and other pre-built views are rendering okay! No problem with that. What am I missing, while pulling my hair out?

Answer

Your action will display whatever the action returns. In your case, your action returns 'true', so the display is 1 (for true). If you want it to display the view, then you need to return that, so

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