Skip to content
jetviper21 edited this page Sep 13, 2010 · 5 revisions

Before and After Filters

  • Before Filters
    • Useful for authentication
    • Preload data
  • After Filters
    • Useful for sending emails
    • Useful for logging
    • Useful for page counting

Order or execution

  1. Before Filter
  2. Action
  3. After Filter

Example:

class MyController extends Controller {

  public function __construct() {
   /* run only for index */
    $this->add_before_filter('my_filter', array('only' => 'index'));
   /* run on everything but index */
    $this->add_before_filter('my_other_filter', array('except' => index));
  /* global method  runs on all methods */
   $this->add_before_filter('my_global_filter');
  /* note after filters work the same way */
  $this->add_after_filter('my_after_filter');
  }

  public function index() {
    $this->render('views/index.php');
  }

  public function show($id) {
    $this->render('views/show.php');
  }

 protected function my_filter() {
   /* do something cool */
  }

  protected function my_other_filter() {
   /* do something neat */
  }

  protected function my_global_filter() {
   /* do something godly */
  }

  protected function my_after_filter() {
    /* run this after page is rendered */
  }

}

Clone this wiki locally