Controllers and Views
This article assumes that you’ve read all installation articles, as well as all MVC articles lead up to this one.
Now that we know how to setup controllers and use the dispatcher to invoke them, we’re now going to learn how to require a view file, as well as prepare data for that view file.
Loading the View File
By default, it is the dispatcher that loads our view file and provides it with data, but it is the controller that prepares that data, and it is the controller that instructs the dispatcher in which view file to load.
To specify which view file to load, we use the controller’s SetView method:
<?php class UsersController extends Controller { public function index() { $this->SetView( "users-index.php" ); } } ?>
This method stores the supplied file name, and provides it to the dispatcher. The dispatcher then attemps to load the view file. The view file must exist somewhere in the include path.
If we store the following file in our views/ directory as users-index.php:
<h1>Welcome</h1>
We should see “Welcome” in our browser when we navigate to /users. But instead we get an error because the dispatcher cannot find our header and footer files.
Headers and Footers
By default, OpenAvanti assumes that you have header.php and footer.php view files. These files are automatically loaded with each view file UNLESS the current request is via AJAX.
We can change the names of the header and footer files OpenAvanti looks for, or even instruct the system not to look for a header and footer file:
<?php // instruct the Dispatcher to use my-header.php and my-footer.php: Dispatcher::SetHeaderFile( "my-header.php" ); Dispatcher::SetFooterFile( "my-footer.php" ); // instruct the Dispatcher not to look for a header or footer: Dispatcher::SetHeaderFile( "" ); Dispatcher::SetFooterFile( "" ); ?>
If we create a default header.php and footer.php, and place them with users-index.php, our page should load now without error.
Supplying Data to the View
The controller is responsible for two things: loading data through models, and specifying the view file to display. The data loaded by the controller is displayed in the view file, so we have to have a mechanism for loading and retrieving that data. To do this, we use the controller’s SetData() method:
<?php class UsersController extends Controller { public function index() { $this->SetData( "users", array( 1 => array( "user_id" => 1, "first_name" => "Hal", "last_name" => "Jordan", "email" => "hal.jordan@jla.net" ), 2 => array( "user_id" => 1, "first_name" => "Barry", "last_name" => "Allen", "email" => "barry.allen@jla.net" ), 3 => array( "user_id" => 1, "first_name" => "Oliver", "last_name" => "Queen", "email" => "oliver.queen@jla.net" ) ) ); $this->SetView( "users-index.php" ); } } ?>
The Dispatcher class takes the Controller’s data and sets up a variable called $aData, which is global to all view files.
If we restructure our users-index.php view file, we can access this data using the global $aData array:
<h2> View Users </h2>
<table>
<tr>
<th>Name</th>
<th>E-mail</th>
<th> </th>
</tr>
<?php foreach( $users as $iUserID => $aUser ): ?>
<tr>
<td><?php echo $aUser[ "first_name" ] . " " . $aUser[ "last_name" ]; ?></td>
<td><?php echo $aUser[ "email" ]; ?></td>
<td><a href="/users/edit/<?php echo $iUserID; ?>">edit</td>
</tr>
<?php endforeach; ?>
</table>The data setup through the Controller would normally come from the database, by using either the CRUD object or Models. In the next section, we’ll expore using Models, but it may be worthwhile to view the articles on database interaction, especially CRUD, first, as the Model class extends the CRUD class in OpenAvanti.

