Slim 控制器问题:必须是 ContainerInterface 的实例,给定的 Slim\\Container 实例

Slim controller issue : must be an instance of ContainerInterface, instance of Slim\\Container given

我正在尝试在 Slim 中使用控制器,但一直收到错误

PHP 可捕获的致命错误:参数 1 传递给
TopPageController::__construct() 必须是 ContainerInterface 的一个实例,
Slim\Container 实例

我的index.php

<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

require '../vendor/autoload.php';
require 'settings.php';

spl_autoload_register(function ($classname) {
    require ("../classes/" . $classname . ".php");
});

$app = new \Slim\App(["settings" => $config]);
$app->get('/', function (Request $request, Response $response) {
    $response->getBody()->write("Welcome");
    return $response;
});
$app->get('/method1', '\TopPageController:method1');
$app->run();
?>

我的TopPageController.php

<?php
class TopPageController {
   protected $ci;
   //Constructor
   public function __construct(ContainerInterface $ci) {
       $this->ci = $ci;
   }

   public function method1($request, $response, $args) {
        //your code
        //to access items in the container... $this->ci->get('');
        $response->getBody()->write("Welcome1");
        return $response;
   }

   public function method2($request, $response, $args) {
        //your code
        //to access items in the container... $this->ci->get('');
        $response->getBody()->write("Welcome2");
        return $response;
   }

   public function method3($request, $response, $args) {
        //your code
        //to access items in the container... $this->ci->get('');
        $response->getBody()->write("Welcome3");
        return $response;
   }
}
?>

谢谢。我正在使用 Slim 3.

您的代码似乎基于 http://www.slimframework.com/docs/objects/router.html 上的 Slim 3 文档,其中不包含避免抛出异常所需的所有代码。

你基本上有两种选择来让它工作。

选项 1:

在您的 index.php 中导入命名空间,就像为 RequestResponse 所做的那样:

use \Interop\Container\ContainerInterface as ContainerInterface;

选项 2:

将 TopPageController 的构造函数更改为:

public function __construct(Interop\Container\ContainerInterface $ci) {
    $this->ci = $ci;
}

TL;DR

抛出异常的原因是 Slim\Container class 使用了 Interop\Container\ContainerInterface 接口(查看源代码):

use Interop\Container\ContainerInterface;

由于 Slim\Container 是对 Pimple\Container 的扩展,因此对于您的控制器方法,以下内容应该都是有效的(有效的)类型声明:

public function __construct(Pimple\Container $ci) {
    $this->ci = $ci;
}

...甚至...

public function __construct(ArrayAccess $ci) {
    $this->ci = $ci;
}

只需将以下代码粘贴到您的控制器中

use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
use \Interop\Container\ContainerInterface as ContainerInterface;

您的控制器构造应如下所示

public function __construct(ContainerInterface $container) {
    parent::__construct($container);
}

我认为您在控制器中为 ContainerInterface 提供命名空间是错误的。

基于后来的 change in Slim3(从版本 3.12.2 到 3.12.3),需要一个略有不同的 ContainerInterface。这会将 \Interop\ 更改为 \Psr\。在您的代码之上添加以下内容或更改现有的 use:

use Psr\Container\ContainerInterface;

或更改构造函数:

public function __construct(\Psr\Container\ContainerInterface $container)
{
    $this->container = $container;
}

container-interop/container-interop is deprecated, simply replace it with psr/container (Psr\Container\ContainerInterface).