Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用带有Zend Diactoros和Fast Route的骰子依赖注入器_Php_Dependency Injection_Psr 7_Mezzio_Fastroute - Fatal编程技术网

Php 使用带有Zend Diactoros和Fast Route的骰子依赖注入器

Php 使用带有Zend Diactoros和Fast Route的骰子依赖注入器,php,dependency-injection,psr-7,mezzio,fastroute,Php,Dependency Injection,Psr 7,Mezzio,Fastroute,我正在为一个小项目使用Dice(一个很好的依赖注入程序)。我还使用PSR7 Zend Diactoros进行请求和nikic fast route 我使用文档中的示例让Dice为Twig、DB和所有其他类工作,但我不知道如何使用它将Request$Request传递给我的控制器 如果您至少能为我指明正确的方向,告诉我如何正确地使用依赖注入程序来处理此类用例,我将不胜感激 控制器代码: 应用初始化代码: 在路由器代码中: 其中$this->app->di是骰子,$handler是 array(2)

我正在为一个小项目使用Dice(一个很好的依赖注入程序)。我还使用PSR7 Zend Diactoros进行请求和nikic fast route

我使用文档中的示例让Dice为Twig、DB和所有其他类工作,但我不知道如何使用它将Request$Request传递给我的控制器

如果您至少能为我指明正确的方向,告诉我如何正确地使用依赖注入程序来处理此类用例,我将不胜感激

控制器代码:

应用初始化代码:

在路由器代码中:

其中$this->app->di是骰子,$handler是

array(2) {
  [0]=>
  string(30) "App\Controllers\BaseController"
  [1]=>
  string(5) "index"
}
1由于我的路由没有参数,第一个错误是我传递了几个参数,所以我尝试在路由器中这样做,它调用控制器的index方法并传递$request

$class = new \ReflectionClass($handler[0]);
$method = $class->getMethod($handler[1]);
foreach ($method->getParameters() as $param) {
            dump($param->getClass()->getName());
            dump($this->app->di->getRule($param->getClass()->getName()));
            $vars[] = $this->app->di->create($param->getClass()->getName());
        }
call_user_func_array([$this->app->di->create($handler[0]),$handler[1]],$vars);
2现在我已通过参数,但错误为

Uncaught Error: Cannot instantiate interface Psr\Http\Message\ServerRequestInterface
我觉得我用错了,可能Di需要创建
$midEngine
,因为现在我自己创建了一个响应和请求对象

任何建议都是很好的,我正试着围绕着一个迷你框架应该如何围绕一个Di来思考

谢谢, 亚历克斯

array(2) {
  [0]=>
  string(30) "App\Controllers\BaseController"
  [1]=>
  string(5) "index"
}
$class = new \ReflectionClass($handler[0]);
$method = $class->getMethod($handler[1]);
foreach ($method->getParameters() as $param) {
            dump($param->getClass()->getName());
            dump($this->app->di->getRule($param->getClass()->getName()));
            $vars[] = $this->app->di->create($param->getClass()->getName());
        }
call_user_func_array([$this->app->di->create($handler[0]),$handler[1]],$vars);
Uncaught Error: Cannot instantiate interface Psr\Http\Message\ServerRequestInterface