Guest Book Example – Code

Initial Page Load:

  1. The Model’s __construct() runs
    • The Model’s public $user_id is set to the current user’s id
  2. The Controller’s __construct() runs
    • The Model is received
    • The Controller’s protected $model is set to point to the Model
  3. The View’s __construct runs
    • The Model is received
    • The View’s protected $model is set to point to the Model
  4. The View’s output() is called
    1. The HTML to be returned is concatenated into $output
    2. The View accesses the Model’s $user_id, via $this->model->user_id, and adds it to $output
    3. A form is added to $output so users can add their name
      • Gear::bindController(‘GuestBookExampleApplication’, ‘insertGuest’) – Binds this form to a method, or control, of the Controller
    4. A table is opened
    5. The View requests the list of guests from the Model, via $this->model->getGuests(), and stores it in $guests
      • The View does not have to be concerned with how the Model gets the data
      • The View assumes the Model is going to return an array of associative arrays
        • array(
          array(‘id’=>1, ‘guest_name’=>’Michael’),
          array(‘id’=>2, ‘guest_name’=>’Darren’)
          )
    6. The View places each array of $guests into $guest using a foreach
      • A new row is added for each $guest
      • A form is added to allow users to delete other users
        • Gear::bindController(‘GuestBookExampleApplication’, ‘deleteGuest’) – Binds this form to a method, or control, of the Controller
        • We assign the id of the $guest to the hidden input, so the controller will know who to delete later
    7. The table is closed
    8. A few of the message features are added in for example sake
    9. The $output, containing the HTML of the application, is returned to WordPress so that it can use it to replace the shortcode

 

What Happens When A Control Is Clicked:

  1. Let’s say the ‘Sign Guest Book’ was clicked, submitting the ‘insertGuest’ control
    • Remember that the ‘Sign Guest Book’ form on the View was bound to a control using Gear::bindController(‘GuestBookExampleApplication’, ‘insertGuest’)
  2. The form and all of it’s inputs will be submitted to the current page
  3. Steps 1-3, from above, happen i.e. The output function has NOT been called yet
  4. The Gear Framework plugin checks to see if any control requests were made
  5. Since the Controller’s ‘insertGuest’ control was submitted, the posted data will be sent to the Method insertGuest() in the Controller
  6. The Controller’s insertGuest() runs
    1. The posted data is requested using the Gear library, and stored in an associative array name $values
    2. $values is validated against the ‘guest_book’ table, since this is the table we intend to insert it into
    3. Once the validation passes, we pass $values to the Model via $this->model->insertGuest($values)
      • We do this because it is not the Controller’s job to run business logic
      • The Controller’s job is to handle request and validate data
    4. The Model’s insertGuest(), receiving $values
    5. Using a Gear library method, we insert the $values into the database
      • Gear::insertTableRow(‘guest_book’, $values)
  7. Steps 4-9, from above, happen
    • Essentially running the output function and display the changes made by the interaction with the control