我同时使用php-di和Doctrine。要使用 Doctrine,有一个bootstrap.php
文件来构造$entityManager
对象。$entityManager
对象在该文件中全局定义,因此要在类中使用它,我必须注入它。
例如,假设以下类:
<?php
interface IAccountService{
function login(string $username, string $password);
}
class AccountService implements IAccountService {
private $entityManager;
public function __construct($entityManager) {
$this->entityManager = $entityManager;
}
public function login(string $email, string $password){
$q = $this->entityManager->createQueryBuilder()
->select('us.id, us.name, us.email, us.passwordHashed')
->from('User', 'us')
->where('us.email = ?1 AND us.passwordHashed = ?2')
->setMaxResults( '1' )
->setParameter(1,$email)
->setParameter(2, HASHHELPER::hashPasswordSHA512($password, $email))
->getQuery();
// echo $q->getSql();
$users = $q->getResult();
// print_r($users);
if(!empty($users) && count($users) > 0){
$_SESSION["USER"] = $users[0];
return true;
}
else{
return false;
}
}
}
?>
但是$entityManager
的类型没有明确定义,当我调用echo gettype($entityManager);
时,结果它会打印"object"
。所以我认为我需要通过它的名称而不是它的类型来注入这个参数。我的意思是这样的:
$container->set('$entityManager', $entityManager);
但这行不通。解决方案和最佳方法是什么?
你能展示一下你现在是如何注入实体管理器的吗?
此外,使用类型提示也是一种很好的做法:
public function __construct(EntityManager $entityManager) {
$this->entityManager = $entityManager;
}
更新:
好的,我通常将 PHP-DI 与 PHP 配置文件 (http://php-di.org/doc/php-definitions.html) 一起使用。它看起来像这样:
return [
AccountService::class => DIobject(AccountService::class)->constructor("here goes EntityManager object")
];
面对此链接后,问题已经解决,该链接显示了$entityManager
的命名空间和类名,但是根据变量名称注入的问题仍然悬而未决。到目前为止,我的新源代码是这样的:
帐户服务.php
<?php
interface IAccountService{
function login(string $username, string $password);
}
class AccountService implements IAccountService {
private $entityManager;
public function __construct(DoctrineORMEntityManagerInterface $entityManager) {
$this->entityManager = $entityManager;
}
public function login(string $email, string $password){
$q = $this->entityManager->createQueryBuilder()
->select('us.id, us.name, us.email, us.passwordHashed')
->from('User', 'us')
->where('us.email = ?1 AND us.passwordHashed = ?2')
->setMaxResults( '1' )
->setParameter(1,$email)
->setParameter(2, HASHHELPER::hashPasswordSHA512($password, $email))
->getQuery();
// echo $q->getSql();
$users = $q->getResult();
// print_r($users);
if(!empty($users) && count($users) > 0){
$_SESSION["USER"] = $users[0];
return true;
}
else{
return false;
}
}
}
?>
路线.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 'HASHHELPER':
require_once "helpers/HASHHELPER.php";
break;
case 'User':
require_once "models/entities/user.php";
break;
}
});
function call($controller, $action) {
global $entityManager;
$container = DIContainerBuilder::buildDevContainer();
$container->set('IAccountService', DIobject('AccountService'));
$container->set('DoctrineORMEntityManagerInterface', $entityManager);
// 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');
break;
case 'account':
$controller = $container->get('AccountController');
break;
default:
$controller = 'home';
$action = 'index';
}
// 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');
}
?>