php - 致命错误:未捕获的 ArgumentCountError:函数 App\Models\Model 的参数太少
问题描述
我正在制作一个简单的应用程序并学习 Slim 3 框架。因为我想要 MVC 设计并使用 DIC,所以我尝试了一个简单的教程并将我的工作放在上面。
- 目标是使用 PDO 在视图上显示我的数据库中的数据。
这在理论上非常简单。
问题,我不知道如何让我的模型依赖于我的模型以调用 PDO,并且我遇到了这个错误。
如果没有在我的模型上传递我的 DIC,设计和应用程序就可以正常工作。我可以从我的控制器调用我的模型并在我的视图上传递任何内容而不会出现任何错误。
以下是我的代码,在此先感谢您的启发。
从我得到的路线开始:
$app->get('/content', \App\Controllers\ContentController::class);
控制器
<?php
namespace App\Controllers;
class ContentController
{
protected $container;
// Passes the DIC to get the model.
function __construct($container)
{
$this->container = $container;
}
function __invoke($request, $response, $args)
{
$datas = $this->container->get('contentModel');
$args['content'] = $datas->testContent();
// get the template renderer and pass response and datas to the template file.
return $this->container->get('renderer')->render($response, 'content.php', $args);
}
这是我的模型:
<?php
namespace App\Models;
class Model
{
// Passes the DIC to get db later.
function __construct($container)
{
$this->container = $container;
}
protected function executeQuery($sql, $params = null) {
if ($params == null)
{
$result = $this->container->get('db')->query($sql); // direct execution
} else {
$result = $this->container->get('db')->prepare($sql); // prepared execution
$result->execute($params);
}
return $result;
}
}
我的内容模型
<?php
namespace App\Models;
Class ContentModel extends Model
{
public function testContent()
{
$testDatas = "Hello world";
return $testDatas;
}
public function getContent()
{
$sql = 'SELECT * FROM posts';
$posts = $this->executeQuery($sql);
return $posts;
}
}
这是我的依赖项:
<?php
use Slim\App;
return function (App $app) {
$container = $app->getContainer();
// View renderer
$container['renderer'] = function ($c) {
$settings = $c->get('settings')['renderer'];
return new \Slim\Views\PhpRenderer($settings['template_path']);
};
// Database
$container['db'] = function ($c) {
$db = $c['settings']['db'];
$pdo = new PDO('mysql:host=' . $db['host'] . ';dbname=' . $db['dbname'],
$db['user'], $db['pass']);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
return $pdo;
};
// Model data stored
$container['contentModel'] = new \App\Models\ContentModel();
};
每当我想在模型中传递 DIC 时,我都会收到此错误,但是构造函数在我的控制器上工作得很好。
( ! ) 致命错误:未捕获的 ArgumentCountError:函数 App\Models\Model::__construct() 的参数太少,第 35 行的 C:\wamp\www\slimappmvc\src\dependencies.php 中传递了 0,而预期的正是 1 C:\wamp\www\slimappmvc\src\Classes\Models\Model.php 在第 8 行
( ! ) ArgumentCountError: 函数 App\Models\Model::__construct() 的参数太少,第 35 行的 C:\wamp\www\slimappmvc\src\dependencies.php 中传递了 0,而 C:\wamp 中预期的正是 1 \www\slimappmvc\src\Classes\Models\Model.php 在第 8 行
解决方案
ContentModel 使用与您的模型类相同的构造函数,因为您在扩展它时没有覆盖它,并且模型的构造函数在您实例化类时需要 1 个参数,“$container”。因此,当您创建实例 ContentModel 时,您还需要向它传递一个参数:
$container['contentModel'] = new \App\Models\ContentModel($container);
我假设 $container 变量是 Model 类所期望的。
推荐阅读
- javascript - 访问同名的不同节点
- javascript - Avoid duplicates from random selection
- python-3.x - 如何提取具有相同元素值的列表?
- tensorflow - Albert TF Hub 模型(多 GPU)上的 Tf 2.0 MirroredStrategy
- perl - 如何创建对常量散列的祝福引用?
- java - 在Java中模拟静态方法
- html - 隐藏 HTML Tampermonkey 的按钮
- javascript - python django 只有第一个语句语句可以访问
- react-native - 反应原生屏幕不重定向
- clojure - Chan of chars to chan of words