jeudi 28 avril 2016

PHP reuse a function in across MVC

My MVC could be described this way:

  • application.php will handle all routing to call controllers accordingly to the requests
  • controller.php is the base controller
  • view.php is the base view, and it is rendered by the controller

So, I have this in my controller:

  private $this->security;

  // Will be done by application
  public function set_security();

  private function is_authorized($area, $action)
  {
    if($this->security[$area][$action]==1)
    {
      return true;
    }
    else
    {
      return false;
    }
  }

The question is how do I re-use the method is_authorized in my controller in view class. It has to have the same value of $security variable which could be done as bellow:

  $this->view->security = $this->security;
  $this->view->render();

So, in my view, I can check if a user is authorized with the same security in my controller without replicating the method.

Aucun commentaire:

Enregistrer un commentaire