gpt4 book ai didi

php - 我的模型中 Slim 3 的可捕获 fatal error ,必须实现接口(interface) Interop\Container\ContainerInterface

转载 作者:搜寻专家 更新时间:2023-10-31 21:23:04 25 4
gpt4 key购买 nike

我在我的 Models 上遇到了 Slim 3 的 Catchable fatal error,我在我的 Controllers 上进行了类似的设置,并且工作正常。当我在我的 Modal class 上实现相同的功能时,我收到以下错误。

Catchable fatal error: Argument 1 passed to Base\Models\BaseModel::__construct() must implement interface Interop\Container\ContainerInterface, none given, called in \bootstrap\app.php on line 111 and defined in \Models\BaseModel.php on line 11

这是我的引导文件

use Noodlehaus\Config;

session_start();

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

$app = new App([
'settings' => [
'determineRouteBeforeAppMiddleware' => true,
'displayErrorDetails' => true,
'addContentLengthHeader' => false
],
]);

$container = $app->getContainer();

$container['config'] = function() {
return new Config(__DIR__ . '/../config/' . file_get_contents(__DIR__ . '/../env.php') . '.php');
};

$container['mailer'] = function($container) {
return new MailgunEmail; ///LINE 110
};

require __DIR__ . '/../app/routes.php';

这是 env.php

return [
'mailgun' => [
'apikey' => 'key-123456',
'domain' => 'sandbox123456.mailgun.org',
'from' => 'noreply@anydomain.com',
],
];

我的基础模型

namespace Base\Models;

use Interop\Container\ContainerInterface;

abstract class BaseModel {

protected $container;

public function __construct(ContainerInterface $container) { /// LINE 11
$this->container = $container;
}

public function __get($property) {
if($this->container->{$property}) {
return $this->container->{$property};
}
}

}

我的电子邮件模型

namespace Base\Models;

use Base\Models\BaseModel;
use Http\Adapter\Guzzle6\Client;
use Mailgun\Mailgun;

class MailgunEmail extends BaseModel {

public function sendWithApi($to, $subject, $html) {
$client = new Client();
/// INSTEAD OF HARD CODING LIKE THIS
$mailgun = new Mailgun('key-123456', $client);
/// I WANT TO DO SOMETHING LIKE THIS
$mailgun = new Mailgun($this->config->get('mailgun.apikey'), $client);

$domain = 'sandbox123456.mailgun.org';

$builder = $mailgun->MessageBuilder();
$builder->setFromAddress('noreply@anydomain.com');
$builder->addToRecipient($to);
$builder->setSubject($subject);
$builder->setHtmlBody($html);

return $mailgun->post("{$domain}/messages", $builder->getMessage());
}

}

我不确定为什么会收到此错误或如何修复它。

最佳答案

只需将 $container 变量传递给引导文件中的 MailgunEmail 构造函数。

$container['mailer'] = function($container) {
return new MailgunEmail($container); ///LINE 110
};

关于php - 我的模型中 Slim 3 的可捕获 fatal error ,必须实现接口(interface) Interop\Container\ContainerInterface,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42221267/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com