2017-09-02 1 views
0

Ich verwende PHP-Di in Abhängigkeit von der Injektion zu tun. Die Anfragen kommen zuerst in der Datei route.php an, die php-di konfiguriert (zB um AccountService zu verwenden, wenn IAccountService erforderlich ist) und dann wird der Container aufgefordert, den entsprechenden Controller zu erstellen. Der Quellcode von route.php ist wie folgt:PHP-DI injiziert nicht die erforderliche Klasse

<?php 
    spl_autoload_register(function ($class_name) { 
    switch ($class_name){ 
     case 'AccountController': 
     require_once 'controllers/account_controller.php'; 
     break; 
     case 'AccountService': 
     case 'IAccountService': 
     require_once 'services/account_service.php'; 
     break; 
     case 'URLHELPER': 
     require_once 'helpers/URLHELPER.php'; 
     break; 
     case 'STRINGHELPER': 
     require_once 'helpers/STRINGHELPER.php'; 
     break; 
     case 'STRINGHELPER': 
     require_once "helpers/HASHHELPER.php"; 
     break; 
    } 
    }); 

    function call($controller, $action) { 
    $container = DI\ContainerBuilder::buildDevContainer(); 
    $container->set('IAccountService', \DI\object('AccountService')); 

    // require the file that matches the controller name 
    require_once('controllers/' . $controller . '_controller.php'); 

    // create a new instance of the needed controller 
    switch($controller) { 
     case 'home': 
     $controller = $container->get('HomeController'); 
     case 'account': 
     $controller = $container->get('AccountController'); 
     break; 
    } 

    // call the action 
    $controller->{ $action }(); 
    } 

    // just a list of the controllers we have and their actions 
    // we consider those "allowed" values 
    $controllers = array(
    'home' => ['index', 'error'] 
    ,'account' => ['login'] 
); 

    // check that the requested controller and action are both allowed 
    // if someone tries to access something else he will be redirected to the error action of the pages controller 
    if (array_key_exists($controller, $controllers)) { 
    if (in_array($action, $controllers[$controller])) { 
     call($controller, $action); 
    } else { 
     call('home', 'error'); 
    } 
    } else { 
    call('home', 'error'); 
    } 
?> 

jetzt erwähnen diese Zeile $controller = $container->get('AccountController'); Wo AccountController ausgewählt ist. Danach habe ich den Fehler bekommen.

Der Quellcode von AccountController:

<?php 
    class AccountController { 

    private $accountService; 

    public function __construct(IAccountService $accountService) { 
     echo $accountService == null; 
     $this->accountService = $accountService; 
    } 

    public function login() { 
     if(isset($_POST) && isset($_POST["email"]) && isset($_POST["password"])){ 
      $loginResult = $this->$accountService->login($_POST["email"], $_POST["password"]); 
      if($loginResult){ 
       $this->URLHELPER::redirectToAction("home", "index"); 
       die; 
      } 
      else{ 
       echo "Oops! this Email and Password combination not found"; 
      } 
     } 
     require_once('views/account/login.php'); 
    } 
    } 
?> 

Es scheint, dass DI injizieren nicht die AccountService und der Fehler ist, wie folgend:

Notice: Undefined variable: accountService in /opt/lampp/htdocs/UMTest/controllers/account_controller.php on line 13

Notice: Undefined property: AccountController::$ in /opt/lampp/htdocs/UMTest/controllers/account_controller.php on line 13

Fatal error: Uncaught Error: Call to a member function login() on null in /opt/lampp/htdocs/UMTest/controllers/account_controller.php:13 Stack trace: #0 /opt/lampp/htdocs/UMTest/routes.php(40): AccountController->login() #1 /opt/lampp/htdocs/UMTest/routes.php(54): call(Object(AccountController), 'login') #2 /opt/lampp/htdocs/UMTest/views/layout.php(10): require_once('/opt/lampp/htdo...') #3 /opt/lampp/htdocs/UMTest/index.php(36): require_once('/opt/lampp/htdo...') #4 {main} thrown in /opt/lampp/htdocs/UMTest/controllers/account_controller.php on line 13

Wo ist mein falsch?

Vielen Dank im Voraus. TG.

+0

folgende Zeile $this->accountService nicht $this->$accountService Änderung zu benutzen Es ist nur ein Test zu überprüfen, ob '$ ist accountService' null oder nicht. Die Zuweisungsanweisung ist die nächste Anweisung, die lautet: $ this -> accountService = $ accountService; ' – ConductedClever

Antwort

0

Für Eigenschaft Zugriff Sie haben in login()

$loginResult = $this->$accountService->login($_POST["email"], $_POST["password"]); 

zu

$loginResult = $this->accountService->login($_POST["email"], $_POST["password"]); 
+0

Oh. Was für ein schlechter Fehler. Vielen Dank. – ConductedClever

+0

Froh, @ConductedClever :) zu helfen –

Verwandte Themen