Chapter 3.6: Before

before runs just before calling action on the model. Similar to preset, you can process incoming data here. The difference is that in before, you have the initial model object which has the database handler, as the first argument. So you can process database data, which you can’t do it in preset.

The second and third arguments extra and nextextras are optional. You can set up extra for use in model’s action method. If you are going to run trigger in model, you may need nextextras as an array reference, in which each element is a hash reference for the corresponding page. The order of the array should match the way the pages are called.

sub before {
  my $self = shift;
  my $err = $self->SUPER::before(@_);
  return $err if $err;

  my $ARGS = $self->{ARGS};
  my $reqs = $self->{R};
  my $action = $ARGS->{g_action};
  my $role = $ARGS->{g_role};

  my ($model, $extra, $nextextras) = @_;
  my $dbh = $model->{DBH};

  do_something

  return;
}

Note that if you have enabled no_db, $model will be defined but not $model->{DBH}. If you have no_method, then $model will be undef.