PHP-DI不会注入所需的类

时间:2017-09-02 05:32:38

标签: php dependency-injection php-di

我根据依赖注入使用php-di。请求首先到达route.php文件,该文件配置php-di(例如,当需要AccountService时使用IAccountService),然后要求容器创建适当的控制器。 route.php的源代码如下:

<?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');
  }
?>

现在提及此行$controller = $container->get('AccountController');选择了AccountController。在此之后我得到了错误。

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');
    }
  }
?>

似乎DI没有注入AccountService,错误如下:

  

注意:未定义的变量:accountService in   第13行/opt/lampp/htdocs/UMTest/controllers/account_controller.php

     

注意:未定义的属性:AccountController :: $ in   第13行/opt/lampp/htdocs/UMTest/controllers/account_controller.php

     

致命错误:未捕获错误:在null上调用成员函数login()   在/opt/lampp/htdocs/UMTest/controllers/account_controller.php:13   堆栈跟踪:#0 /opt/lampp/htdocs/UMTest/routes.php(40):   AccountController-&gt; 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}引入   第13行/opt/lampp/htdocs/UMTest/controllers/account_controller.php

我的错误在哪里?

1 个答案:

答案 0 :(得分:0)

要访问媒体资源,您必须使用$this->accountService而不是$this->$accountService更改login()

中的后续行
$loginResult = $this->$accountService->login($_POST["email"], $_POST["password"]);

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