Perform business validation on several controllers

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.

If condition and model name Evaluacion are the same for all methods or all contrllers that extends ControllerBase try this:

class ControllerBase {
    protected $evaluacionActiva;

    public function initialize()
    {
        parent::initialize();
        $conditions = "EvaluacionIni <= :fecha: AND EvaluacionFin >= :fecha:";
        $parameters = array("fecha"=>date('Y-m-d'));
        $this->evaluacionActiva = Evaluacion::findFirst(array($conditions,"bind"=>$parameters));

        if (!$this->evaluacionActiva) {
          //validation message or something you need
        }
    }
}

class EvaluacionController extends ControllerBase
{     
    public function initialize()
    {
        $this->view->setTemplateAfter('main');
        Tag::setTitle('Evaluaciones');
        parent::initialize();
    }
 }

Thanks for you suggestion @alexcore but in fact not all of the controllers need that validation so extending ControllerBase may not be a proper solution.

If condition and model name Evaluacion are the same for all methods or all contrllers that extends ControllerBase try this:

class ControllerBase {
  protected $evaluacionActiva;

  public function initialize()
  {
      parent::initialize();
      $conditions = "EvaluacionIni <= :fecha: AND EvaluacionFin >= :fecha:";
      $parameters = array("fecha"=>date('Y-m-d'));
      $this->evaluacionActiva = Evaluacion::findFirst(array($conditions,"bind"=>$parameters));

      if (!$this->evaluacionActiva) {
        //validation message or something you need
      }
  }
}

class EvaluacionController extends ControllerBase
{     
  public function initialize()
  {
      $this->view->setTemplateAfter('main');
      Tag::setTitle('Evaluaciones');
      parent::initialize();
  }
}

Then if there controller that dont need this behaviour - just extends phalcons controller.

So the best option is just a parent controller? No need of using the event manager?

Then if there controller that dont need this behaviour - just extends phalcons controller.