The controller is the most important part in this ZF tutorial. This is where most of the PHP code resides. It handles all input – coming thru the file receiving the Ajax rating – as well as all output – e. g. updating the rating scores. The only liberty I took not following the ZF way is to integrate the view scripts (a.k.a. HTML output) into classes in this controller, but you can put these classes anywhere you want if you are using ZF as an application.

If you are familiar with Zend’s naming conventions, and check the name of the class called in Part IV, you should be able to make an educated guess as to the name of the controller file, and the folder to store it. If not, read the ZF manual on „autoloading“ 😉 The class name Mylib_Rating_Controller expects a file named „Controller.php“ in your „library/Mylib“ folder, subfolder „Rating“ (you already told Zend to check „library“ in your bootstrap). Create folders and file, and add the class as well as your first method:

class Mylib_Rating_Controller
{
 public function __construct($options = null)
 {
 }

 /**
 * Get rating view
 *
 * @params int page id
 * @return string
 */
 public function getRatingView($id)
 {
 $rating = $this->getRatingById($id);
 if (!empty($rating)) {
  $average = floor($rating->rating_total / $rating->rating_votes);
 } else {
  $average = 0;
 }
 ?>
 <select id="<?php echo $id ?>">
 <option value="1"<?php if ($average == "1") echo ' selected="selected"';?>>Bad</option>
 <option value="2"<?php if ($average == "2") echo ' selected="selected"';?>>Not too bad</option>
 <option value="3"<?php if ($average == "3") echo ' selected="selected"';?>>Ok</option>
 <option value="4"<?php if ($average == "4") echo ' selected="selected"';?>>Good</option>
 <option value="5"<?php if ($average == "5") echo ' selected="selected"';?>>Terrific</option>
 </select>
 <span id="rating_value_<?php echo $id ?>">&nbsp;</span>
 <?php
 }
}