2017-02-24 6 views
0

Dies ist eine Folgefrage zu meiner vorherigen, ich muss die index() -Methode eines Controllers mit einem Loader aufrufen. Das ist, was ich mir im Lader ausgedacht habe. Das funktioniert, aber ich weiß nicht, ob das der richtige Weg ist. Ich suchte google ohne Glück damit gezwungen zu fragen.Aufrufmethode eines Controllers im Lader

public function controller($controller) 
{ 
    $file = 'controller/' . $controller . '.php'; 
    $class = $controller; 

    if (file_exists($file)) { 
     include_once($file); 
     $controller = new $class($this->registry); 
     $controller->index(); 
    } else { 
     echo 'Controller ' . $controller . ' not found'; 
    } 
} 

Gleich nach Instanziierung der $ Controller habe ich diese $controller->index(); den Index-Methode aufzurufen.

Auch überprüfen, ob die Funktion aufrufbar ist oder nicht las ich über is_callable und call_user_func aber bin nicht sicher, wie man sie dankbar Wird

public function controller($controller) 
{ 
    $file = 'controller/' . $controller . '.php'; 
    $class = $controller; 

    if (file_exists($file)) { 
     include_once($file); 
     $controller = new $class($this->registry); 

     if (is_callable($controller)) { 

     } 

    } else { 
     echo 'Controller ' . $controller . ' not found'; 
    } 
} 

zu verwenden, wenn Sie helfen könnten.

Antwort

-1

Okay, ich habe das und hier ist was ich getan habe.

public function controller($controller) 
{ 
    $file = 'controller/' . $controller . '.php'; 
    $class = $controller; 

    if (file_exists($file)) { 
     include_once($file); 
     $controller = new $class($this->registry); 

     if (is_callable(array($controller, 'index'))) { 
      return call_user_func(array($controller, 'index')); 
     } else { 
      echo 'Function not callable'; 
     } 

    } else { 
     echo 'Controller ' . $controller . ' not found'; 
    } 
} 
Verwandte Themen