Hi,
I need to perform certain businness validation in several controllers but not all them. The validation is a simple query which results are used in the controller's actions. Right now, I'm performing that query in the initialize() method of every chosen controller, this looks something like this:
class EvaluacionController extends ControllerBase
{
private $evaluacionActiva;
public function initialize()
{
$this->view->setTemplateAfter('main');
Tag::setTitle('Evaluaciones');
parent::initialize();
$conditions = "EvaluacionIni <= :fecha: AND EvaluacionFin >= :fecha:";
$parameters = array("fecha"=>date('Y-m-d'));
$this->evaluacionActiva = Evaluacion::findFirst(array($conditions,"bind"=>$parameters));
}
}
Then $this->evaluacionActiva variable is used in every action on that controller. I am copy-pasting this same code on every controller where I need that business validation but it seems these aren't best pratices nor using the right framework's components.
Is there a way to put this on some kind of event listener which only works for certain controllers? Additionally, how would I throw an error when the validation is not met (empty query results) without copy-pasting code among controllers?
Any feedback would be truly appreciated, thanks.