[英]PHP-DI inject parameter by name
I am using php-di and Doctrine together. 我正在一起使用php-di和Doctrine。 To use Doctrine there is a bootstrap.php
file which constructs the $entityManager
object. 要使用Doctrine,有一个bootstrap.php
文件可构造$entityManager
对象。 The $entityManager
object is defined globally in that file so to use it in my classes I have to inject it. $entityManager
对象是在该文件中全局定义的,因此要在我的类中使用它,我必须注入它。
For example assume the class below: 例如,假设下面的类:
<?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;
}
}
}
?>
But the type of $entityManager
is not well defined and either when I call echo gettype($entityManager);
但是$entityManager
的类型$entityManager
不正确,或者当我调用echo gettype($entityManager);
it prints "object"
as result. 它打印"object"
作为结果。 So I think I need to inject this parameter by its name instead of its type. 因此,我认为我需要通过其名称而不是其类型来注入该参数。 I mean something like this: 我的意思是这样的:
$container->set('$entityManager', $entityManager);
But this does not work. 但这是行不通的。 What's the solution and the best way? 什么是解决方案和最佳方法?
Can you show how are you injecting EntityManager now? 您能显示出现在如何注入EntityManager吗?
Also, it's a good practice to use type-hinting: 另外,使用类型提示也是一个好习惯:
public function __construct(EntityManager $entityManager) {
$this->entityManager = $entityManager;
}
UPDATE: 更新:
Ok, I usually use PHP-DI with a PHP configuration file ( http://php-di.org/doc/php-definitions.html ). 好的,我通常将PHP-DI与PHP配置文件( http://php-di.org/doc/php-definitions.html )一起使用。 It would look something like this: 它看起来像这样:
return [
AccountService::class => DI\object(AccountService::class)->constructor("here goes EntityManager object")
];
The problem have solved after facing this link which shows the namespace and class name of $entityManager
but the question of injecting according to variable name is still open. 面对显示$entityManager
的名称空间和类名称的链接后,问题已解决,但根据变量名进行注入的问题仍然存在。 By now, the new source code of mine is like this: 现在,我的新源代码如下:
AccountService.php AccountService.php
<?php
interface IAccountService{
function login(string $username, string $password);
}
class AccountService implements IAccountService {
private $entityManager;
public function __construct(Doctrine\ORM\EntityManagerInterface $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;
}
}
}
?>
routes.php routes.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 = DI\ContainerBuilder::buildDevContainer();
$container->set('IAccountService', \DI\object('AccountService'));
$container->set('Doctrine\ORM\EntityManagerInterface', $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');
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.