HTML and CSS Reference
In-Depth Information
Adding Form Handlers and Data Access Methods to the Controller
The last bit of the question segment of the app is to add the action array, model, and action handler classes to the
Question controller.
Start by updating the constructor with the model declaration and action array. Add the following to system/
controllers/ :
public function __construct( $options )
$this->model = new Question_Model;
// Checks for a form submission
$this->actions = array(
'ask' => 'create_question',
'vote' => 'vote_question',
'answer' => 'answer_question',
if (array_key_exists($options[0], $this->actions)) {
} else {
$this->room_id = isset($options[0]) ? (int) $options[0] : 0;
if ($this->room_id===0) {
throw new Exception("Invalid room ID supplied");
This loads the Question_Model class for data access and then declares three possible form actions and their
required action handler methods.
Saving New Questions
The action handler for saving new questions is rather complex because it's built with the next chapter—in which we
start adding realtime functionality—in mind. As a result, it not only stores the new question but also generates a new
question view for return so that the markup doesn't need to be rendered client-side later. It also adds a cookie for
users to indicate that they've already voted for the question.
Add the following bold code to the Question class:
protected function get_questions( )
return $this->model->get_room_questions($this->room_id);
* Adds a new question to the database
* @return array Information about the updated question
Search WWH ::

Custom Search