Fork me on GitHub

Controller Methods

Use set() to make variables from the controller available in the view.

<?php
// controllers/venues_controller.php
class VenuesController extends MvcPublicController {
  
  public function show() {
    $object = $this->model->find_by_id($this->params['id'], array(
      'includes' => array('Event')
    ));
    $this->set('object', $object);
  }
  
}

<?php
// views/venues/show.php
?>
<h2><?php echo $object->name; ?></h2>
View alone

set_object

In an action where an id URL parameter is available (e.g. the conventional show action, where the id parameter is passed from the URL), calling $this->set_object() will find the object that has the specified id from the controller's native model and make it available to the view as $object (using the equivalent of $this->set('object', $object)).

<?php
// controllers/venues_controller.php
class VenuesController extends MvcPublicController {
  
  public function show() {
    $this->set_object();
  }
  
}
?>

<?php
// views/venues/show.php
?>
<h2><?php echo $object->name; ?></h2>
View alone

set_objects

In an action that will show a collection of objects (e.g. the conventional index action), calling $this->set_objects() will find a paginated collection of the objects from the controller's native model and make it available to the view as $objects (using the equivalent of $this->set('objects', $objects)).

set_objects() will handle pagination behind the scenes. If a URL param named page (or in an admin action a URL named page_num) is set, set_objects() will use that to properly paginate the collection. It also calls $this->set_pagination($collection);, which will allow for pagination links to be displayed in the view.

<?php
// controllers/venues_controller.php
class VenuesController extends MvcPublicController {
  
  public function index() {
    $this->set_objects();
  }
  
}

// views/venues/index.php
// This will print_r() all of the retrieve objects
print_r($objects);
?>
View alone

set_pagination

To allow for pagination links to easily be created in a view, call $this->set_pagination($collection); using a collection that is returned from a paginate() call on a model.

<?php
// controllers/venues_controller.php

class VenuesController extends PublicController {

  public function search() { 
    $collection = $this->Venue->paginate($this->params);
    $this->set('objects', $collection['objects']);
    $this->set_pagination($collection);
  }
  
}

// views/venues/search.php

echo $this->pagination();
?>

set_pagination() processes the collection and sets $this->pagination to an appropriate array that will be used in paginate_links() within $this->pagination(); in the view.

View alone

load_helper

To use a helper in a view, you'll need to call $this->load_helper() in the controller.

<?php
// controllers/venues_controller.php
class VenuesController extends MvcPublicController {
  
  public function show() {
    $this->load_helper('VenueDisplay');
    $this->set_object();
  }
  
}
?>

<?php
// views/venues/show.php
?>
<h2><?php echo $object->name; ?></h2>
<?php echo $this->venue_display->formatted_address($object); ?>

<?php
// helpers/venue_display_helper.php
class VenueDisplayHelper extends MvcHelper {
  
  public function formatted_address($object) {
    $html = '';
    $html .= '<div>'.$object->address1.'</div>';
    $html .= '<div>'.$object->address2.'</div>';
    $html .= '<div>'.$object->city.', '.$object->state.' '.$object->zip.'</div>';
    return $html;
  }

}
?>
View alone

load_model

Use load_model() if you want to use a model other than the controller's native model. To load multiple models, use load_models().

<?php
// controllers/venues_controller.php
class VenuesController extends MvcPublicController {
  
  public function show() {
    $this->load_model('Speaker');
    $speakers = $this->Speaker->find();
  }
  
}
?>
View alone

load_models

Use load_models() if you want to use models other than the controller's native model. To load a single model, use load_model().

<?php
// controllers/venues_controller.php
class VenuesController extends MvcPublicController {
  
  public function show() {
    $this->load_models('Speaker', 'Event');
    $speakers = $this->Speaker->find();
    $events = $this->Event->find();
  }
  
}
?>
View alone

redirect

To redirect to another page while inside of a controller, use redirect().

<?php
$url = MvcRouter::admin_url(array('controller' => 'venues', 'action' => 'index'));
$this->redirect($url);
?>

Use flash() to set a message or warning that will be displayed to the user. The text will be displayed by display_flash(), which is called in the default admin layout (views/admin/layouts/admin.php), but can be called elsewhere if desired.

<?php

// controllers/admin/admin_venues_controller.php

class AdminVenuesController extends AdminMvcController {

  public function edit() {
    if (!empty($this->params['data']) && !empty($this->params['data']['Venue'])) {
      $object = $this->params['data']['Venue'];
      if ($this->Venue->save($this->params['data'])) {
        $this->flash('notice', 'Successfully saved!');
        $this->refresh();
      } else {
        $this->flash('error', $this->Venue->validation_error_html);
      }
    }
    $this->set_object();
  }

}
?>

<?php
// views/admin/layouts/admin.php
?>

<div class="wrap">

<?php $this->display_flash(); ?>

<?php $this->render_main_view(); ?>

</div>
View alone

create_or_save

The add and edit actions in WP MVC's default admin controllers use create_or_save() to either create or update records using data submitted from a form. create_or_save() checks $this->params['data'] to see if data related to the controller's model is present. If it is and an ID value is set, the record with that ID is updated; if it is and an ID is not set, a record is inserted with the submitted data.

<?php

class AdminVenuesController extends MvcAdminController {

  public function add() {
    $this->create_or_save();
  }
  
  public function edit() {
    $this->verify_id_param();
    $this->create_or_save();
    $this->set_object();
  }

}

?>