How the controller sends data to view
In the previous paragraph, we have seen how to display the content view. However, the view should only be responsible for displaying data, and not for manipulation. Consequently, any work on data should be done in controller action and then passed to view.
The render()
method in the action of the controller has a second parameter, which is an array whose keys are names of variables, and values are the content of these variables available in view context.
Now, let's move all data manipulation of our itemsList
example in controller, leaving out just the code to format the output (such as HTML).
The following is the content of the actionItemsList()
controller:
public function actionItemsList() { $newsList = [ [ 'title' => 'First World War', 'date' => '1914-07-28' ], [ 'title' => 'Second World War', 'date' => '1939-09-01' ], [ 'title' =>...