Creating a view to display a news list

Now, we will create a simple news list in a view named itemsList. We will point to this view from NewsController, so we have to:

  • Create a news folder under basic/views, that NewsController will use as the base folder to search for the views to be rendered (according to the view names' rules explained in the previous chapter)
  • Create an itemsList.php file under basic/views/news

Now, open basic/views/news/itemsList.php, create an array with a list of data and display the output with a simple table of items:

<?php $newsList = [ [ 'title' => 'First World War', 'date' => '1914-07-28' ], [ 'title' => 'Second World War', 'date' => '1939-09-01' ], [ 'title' => 'First man on the moon', 'date' => '1969-07-20' ] ]; ?> <table> ...

Get Yii2 By Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.